Advertisement
drankinatty

C - minimum singly-linked-list (head/tail - non-circular)

May 1st, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. enum { MAXLN = 256 };
  6.  
  7. typedef struct words {
  8.     char *word;
  9.     struct words *next;
  10. } words;
  11.  
  12. words *add2list (words **list, char *word);
  13. void print_list (words *list);
  14. void free_list (words *list);
  15.  
  16. int main (int argc, char **argv) {
  17.  
  18.     words *wordlist = NULL;     /* declare list */
  19.     char line[MAXLN] = {0};     /* line buffer  */
  20.     char delim[] = " \t\n,.;";  /* delimiters   */
  21.     FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
  22.  
  23.     if (!fp) { /* validate file open */
  24.         fprintf (stderr, "error: file open failed '%s'\n", argv[1]);
  25.         return 1;
  26.     }
  27.  
  28.     while (fgets (line, MAXLN, fp)) /* read each line in file  */
  29.     {
  30.         char *p = NULL;
  31.         /* tokenize line, add each word in line to linked-list */
  32.         for (p = strtok (line, delim); p; p = strtok (NULL, delim))
  33.             add2list (&wordlist, strdup (p)); /* allocate/copy p  */
  34.     }
  35.     if (fp != stdin) fclose (fp);
  36.  
  37.     print_list (wordlist);  /* print all nodes in list  */
  38.     free_list  (wordlist);  /* free all nodes in list   */
  39.  
  40.     return 0;
  41. }
  42.  
  43. /* add nodes to list */
  44. words *add2list (words **list, char *word)
  45. {
  46.     words *node = calloc (1, sizeof *node); /* create node */
  47.     if (!node) {
  48.         fprintf (stderr, "add2list() error: virtual memory exhausted.\n");
  49.         exit (EXIT_FAILURE);
  50.     }
  51.     node->word = word;  /* assign values */
  52.     node->next = NULL;
  53.  
  54.     if (!*list) {   /* first node - add as first */
  55.         (*list) = node;
  56.     }
  57.     else {          /* second node - add as next */
  58.         if ((*list)->next == NULL) {
  59.             (*list)->next = node;
  60.         }
  61.         else {      /* remaining nodes - add at end */
  62.             words *iter = *list;
  63.             while (iter->next) iter = iter->next;
  64.             iter->next = node;
  65.         }
  66.     }
  67.     return *list;
  68. }
  69.  
  70. /* print all nodes in list */
  71. void print_list (words *list)
  72. {
  73.     const words *iter = list;  /* pointer to iterate list  */
  74.  
  75.     if (iter ==  NULL) {
  76.         fprintf (stderr,"print_list() warning: empty list.\n");
  77.         return;
  78.     }
  79.  
  80.     for (; iter; iter = (iter->next ? iter->next : NULL))
  81.         printf (" %s\n", iter-> word);
  82. }
  83.  
  84. /* free all nodes in list */
  85. void free_list (words *list)
  86. {
  87.  
  88.     words *iter = list;   /* pointer to iterate list  */
  89.     words *victim = list; /* pointer to delete    */
  90.  
  91.     if (iter ==  NULL) {
  92.         fprintf (stderr,"print_list() warning: empty list.\n");
  93.         return;
  94.     }
  95.  
  96.     while (iter) {
  97.         victim = iter;
  98.         iter = iter->next;
  99.         free (victim->word);
  100.         free (victim);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement