GRASS Programmer's Manual  6.4.2(2012)
try2.c
Go to the documentation of this file.
00001 /*
00002  **  Written by David Gerdes  US Army Construction Engineering Research Lab
00003  **     April 1992
00004  **  Copyright 1992 USA-CERL   All rights reserved.
00005  **
00006  */
00007 
00008 
00009 /*
00010  **  read from stdin and each line into a linked list of chars
00011  **  then print it back out.   if there is any argument specified
00012  **  the lines will be printed out reversed.
00013  */
00014 
00015 #include <stdio.h>
00016 #include <grass/linkm.h>
00017 
00018 struct link
00019 {
00020     char let;
00021     struct link *next;
00022 };
00023 
00024 int main(int argc, char *argv[])
00025 {
00026     register int i;
00027     VOID_T *head;
00028     struct link List, *tmp, *p;
00029     int rev = 0;
00030     char buf[4096];
00031 
00032     if (argc == 2)
00033         rev = 1;
00034 
00035 
00036     List.next = NULL;
00037     List.let = ' ';
00038 
00039 
00040     link_set_chunk_size(1);
00041     head = (VOID_T *) link_init(sizeof(struct link));
00042 
00043 
00044     while (NULL != gets(buf)) {
00045         for (i = 0; buf[i] != '\0'; i++) {
00046             tmp = (struct link *)link_new(head);
00047             tmp->let = buf[i];
00048             if (rev)
00049                 add_link_rev(&List, tmp);
00050             else
00051                 add_link(&List, tmp);
00052         }
00053 
00054         dumplist(&List);
00055 
00056         p = List.next;
00057 
00058         while (p != NULL && p->next != NULL) {
00059             tmp = p->next;
00060             link_dispose(head, p);
00061             p = tmp;
00062         }
00063         List.next = NULL;
00064     }
00065 
00066     link_cleanup(head);
00067 
00068     exit(0);
00069 }
00070 
00071 int add_link_rev(struct link *List, struct link *link)
00072 {
00073     struct link *p;
00074 
00075     p = List->next;
00076     List->next = link;
00077     link->next = p;
00078 }
00079 
00080 int add_link(struct link *List, struct link *link)
00081 {
00082     struct link *p;
00083 
00084     p = List;
00085     while (p->next != NULL)
00086         p = p->next;
00087     p->next = link;
00088     link->next = NULL;
00089 }
00090 
00091 int dumplist(struct link *List)
00092 {
00093     struct link *p;
00094 
00095     p = List->next;
00096     while (p != NULL) {
00097         putchar(p->let);
00098         p = p->next;
00099     }
00100     putchar('\n');
00101 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines