GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
linkm/try.c
Go to the documentation of this file.
1 /*
2  ** Written by David Gerdes US Army Construction Engineering Research Lab
3  ** April 1992
4  ** Copyright 1992 USA-CERL All rights reserved.
5  **
6  */
7 
8 /*
9  ** takes 1st command line argument and stuffs each letter of it into
10  ** a linked list. then prints it back out to stdout.
11  ** If a second argument is specified, the first argument is put in the
12  ** list backwards.
13  */
14 #include <stdio.h>
15 #include <grass/linkm.h>
16 
17 struct link
18 {
19  char let;
20  struct link *next;
21 };
22 
23 int main(int argc, char *argv[])
24 {
25  register int i;
26  VOID_T *head;
27  struct link List, *tmp, *p;
28  int rev = 0;
29 
30  if (argc < 2)
31  fprintf(stderr, "Usage: %s str [rev]\n", argv[0]), exit(1);
32 
33  if (argc > 2)
34  rev = 1;
35 
36 
37  List.next = NULL;
38  List.let = ' ';
39 
40 
41  head = (VOID_T *) link_init(sizeof(struct link));
42 
43  for (i = 0; argv[1][i]; i++) {
44  tmp = (struct link *)link_new(head);
45  tmp->let = argv[1][i];
46  if (rev)
47  add_link_rev(&List, tmp);
48  else
49  add_link(&List, tmp);
50  }
51 
52  dumplist(&List);
53 
54  p = List.next;
55  while (p->next != NULL) {
56  tmp = p->next;
57  link_dispose(head, p);
58  p = tmp;
59  }
60 
61  link_cleanup(head);
62 
63  exit(0);
64 }
65 
66 int add_link_rev(struct link *List, struct link *link)
67 {
68  struct link *p;
69 
70  p = List->next;
71  List->next = link;
72  link->next = p;
73 }
74 
75 int add_link(struct link *List, struct link *link)
76 {
77  struct link *p;
78 
79  p = List;
80  while (p->next != NULL)
81  p = p->next;
82  p->next = link;
83  link->next = NULL;
84 }
85 
86 int dumplist(struct link *List)
87 {
88  struct link *p;
89 
90  p = List->next;
91  while (p != NULL) {
92  putchar(p->let);
93  p = p->next;
94  }
95  putchar('\n');
96 }
#define VOID_T
Definition: qtree.h:17
struct link_head * link_init(int size)
Definition: linkm/init.c:40
struct link_head * link_new(struct link_head *Head)
Definition: new.c:12
void link_cleanup(struct link_head *Head)
Definition: linkm/init.c:64
int main(int argc, char *argv[])
Definition: gem/main.c:302
int add_link(struct link *List, struct link *link)
Definition: linkm/try.c:75
void link_dispose(struct link_head *Head, VOID_T *ptr)
Definition: dispose.c:10
return NULL
Definition: dbfopen.c:1394
int add_link_rev(struct link *List, struct link *link)
Definition: linkm/try.c:66
int dumplist(struct link *List)
Definition: linkm/try.c:86