193030

Simple linked list

Nov 28th, 2021 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node1
  5. {
  6.     int data;
  7.     struct node1* next;
  8. };
  9.  
  10. void add(int data, struct node1**head)
  11. {
  12.     struct node1* p = *head;
  13.     if(p == NULL)
  14.     {
  15.         p = (struct node1*)malloc(sizeof(struct node1));
  16.         p->data = data;
  17.         p->next = NULL;
  18.         *head = p;
  19.         return;
  20.     }
  21.     else
  22.     {
  23.         while (p->next)
  24.             p = p->next;
  25.         struct node1 *t = (struct node1*)malloc(sizeof(struct node1));
  26.         t->data = data;
  27.         t->next = NULL;
  28.         p->next = t;
  29.     }
  30. }
  31.  
  32. void display(struct node1**head)
  33. {
  34.     struct node1* p = *head;
  35.     while (p)
  36.     {
  37.         printf("%d", p->data);
  38.         p = p->next;
  39.     }
  40. }
  41.  
  42. int main()
  43. {
  44.     struct node1* head = NULL;
  45.     add(10, &head);
  46.     add(20, &head);
  47.     add(30, &head);
  48.     display(&head);
  49.  
  50. }
Add Comment
Please, Sign In to add comment