Advertisement
drankinatty

Singly Linked List of Integers (add, del, print)

Aug 21st, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. /*
  2.  * Linked list of integers demonstrating add node, delete node, print list, delete list
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. typedef struct client_t {
  9.     int id;
  10.     struct client_t *next;
  11. } client_t;
  12.  
  13. /** add node to list */
  14. client_t *addClient (client_t **p, int n)
  15. {
  16.     client_t *node = malloc (sizeof *node); /* allocate node */
  17.     if (!node) {                            /* validate allocation */
  18.         perror ("malloc-node");
  19.         return NULL;
  20.     }
  21.     node->id = n;                   /* initialize members values */
  22.     node->next = NULL;
  23.    
  24.     if (!*p)                        /* if 1st node, node is head */
  25.         *p = node;
  26.     else {                          /* otherwise */
  27.         client_t *iter = *p;        /* pointer to iterate */
  28.         while (iter->next)          /* iterate to end (or use a tail ptr) */
  29.             iter = iter->next;
  30.         iter->next = node;          /* add at end */
  31.     }
  32.    
  33.     return node;    /* return new node */
  34. }
  35.  
  36. /** delete node with value n from list (for loop) */
  37. void deleteClient (client_t **p, int n)
  38. {
  39.     client_t **ppn = p;         /* pointer to pointer to node*/
  40.     client_t *pn = *p;          /* pointer to node */
  41.  
  42.     for (; pn; ppn = &pn->next, pn = pn->next) {
  43.         if (pn->id == n) {
  44.             *ppn = pn->next;    /* set address to next */
  45.             free (pn);
  46.             break;
  47.         }
  48.     }
  49. }
  50.  
  51. /** delete all nodes in list */
  52. void deleteList (client_t *l)
  53. {
  54.     client_t *iter = l;
  55.     while (iter) {
  56.         client_t *victim = iter;
  57.         iter = iter->next;
  58.         free (victim);
  59.     }
  60. }
  61.  
  62. /** print all nodes in list */
  63. void prn (client_t *l)
  64. {
  65.     if (!l) {
  66.         puts ("list-empty");
  67.         return;
  68.     }
  69.     for (client_t *n = l; n; n = n->next)
  70.         printf (" %d", n->id);
  71.     putchar ('\n');
  72. }
  73.  
  74. int main (void) {
  75.    
  76.     client_t *list = NULL;              /* pointer to list initialized NULL */
  77.    
  78.     for (int i = 1; i < 11; i++)        /* add nodes clients ids 1 - 10 */
  79.         if (!addClient (&list, i))
  80.             return 1;
  81.    
  82.     fputs ("original ids   : ", stdout);    /* output original list */
  83.     prn (list);
  84.    
  85.     for (int i = 1; i < 11; i+=2)       /* delete odd client ids 1,3,5,7,9 */
  86.         deleteClient (&list, i);
  87.    
  88.     fputs ("deleted odd ids: ", stdout);    /* output modified list */
  89.     prn (list);
  90.    
  91.     deleteList (list);                  /* free all remaining nodes */
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement