Idanref

Regular Linked List

Apr 21st, 2021 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. typedef struct Node
  6. {
  7.     int value;
  8.     struct Node *next
  9. } node;
  10.  
  11. node* create_node(int data)
  12. {
  13.     node *temp = (node *)malloc(sizeof(node));
  14.  
  15.     temp->value = data;
  16.     temp->next = NULL;
  17.  
  18.     return temp;
  19. }
  20.  
  21. void insert(node **head, node *link)
  22. {
  23.     if(*head == NULL)
  24.     {
  25.         *head = link;
  26.         return;
  27.     }
  28.  
  29.     node *temp = *head;
  30.  
  31.     while(temp->next != NULL)
  32.     {
  33.         temp = temp->next;
  34.     }
  35.  
  36.     temp->next = link;
  37. }
  38.  
  39. void print_list(node *head)
  40. {
  41.     node *temp = head;
  42.  
  43.     while(temp != NULL)
  44.     {
  45.         printf("%d-> " , temp->value);
  46.         temp = temp->next;
  47.     }
  48. }
  49.  
  50.  
  51. void main()
  52. {
  53.     node *head = NULL;
  54.  
  55.     node *n1 = create_node(3);
  56.     node *n2 = create_node(6);
  57.     node *n3 = create_node(1);
  58.     node *n4 = create_node(9);
  59.     node *n5 = create_node(8);
  60.     node *n6 = create_node(4);
  61.     node *n7 = create_node(5);
  62.  
  63.     insert(&head, n1);
  64.     insert(&head, n2);
  65.     insert(&head, n3);
  66.     insert(&head, n4);
  67.     insert(&head, n5);
  68.     insert(&head, n6);
  69.     insert(&head, n7);
  70.  
  71.     print_list(head);
  72. }
Add Comment
Please, Sign In to add comment