Advertisement
Robert_JR

Linked List Append

Oct 20th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /*
  2.    Linked List
  3.    Append()
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct Node
  10. {
  11.     int data;
  12.     struct Node *next;
  13. };
  14.  
  15. typedef struct Node node;
  16.  
  17. node *head = 0;
  18.  
  19. void append(int data)
  20. {
  21.     if(head == 0)
  22.     {
  23.         head = (node*)malloc(sizeof(node));
  24.         head->data = data;
  25.         head->next = 0;
  26.     }
  27.     else
  28.     {
  29.         node *temp = (node*)malloc(sizeof(node));
  30.         temp = head;
  31.         while(temp->next != 0)
  32.         {
  33.             temp = temp->next;
  34.         }
  35.         node *new = (node*)malloc(sizeof(node));
  36.         new->data = data;
  37.         new->next = 0;
  38.         temp->next = new;
  39.     }
  40. }
  41. void display()
  42. {
  43.     node *temp = (node*)malloc(sizeof(node));
  44.     temp = head;
  45.     while(temp->next != 0)
  46.     {
  47.         printf("%d\n", temp->data);
  48.         temp = temp->next;
  49.     }
  50.     printf("%d\n", temp->data);
  51. }
  52.  
  53. int main()
  54. {
  55.     append(4);
  56.  
  57.     append(5);
  58.  
  59.     append(6);
  60.    
  61.     display();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement