Advertisement
rana1704

Linked List

Jul 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<time.h>
  3.  
  4. struct node
  5. {
  6.     int info;
  7.     struct node *next;
  8. };
  9. struct node *head = NULL;
  10.  
  11. void add_node(int x)
  12. {
  13.     struct node *travel = head;
  14.     struct node *temp;
  15.  
  16.     temp = malloc (sizeof(struct node));
  17.     temp->info = x;
  18.     temp->next=NULL;
  19.  
  20.     if(head==NULL)
  21.     {
  22.  
  23.         head = temp;
  24.     }
  25.     else
  26.     {
  27.         while(travel->next!=NULL)
  28.         {
  29.             travel=travel->next;
  30.         }
  31.         travel->next = temp;
  32.     }
  33. }
  34.  
  35. void print()
  36. {
  37.     struct node* temp= head;
  38.     while(temp!=NULL)
  39.     {
  40.         printf("%d  ", temp->info);
  41.         temp=temp->next;
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     int i, prev=0;
  48.  
  49.     add_node(5);
  50.     add_node(6);
  51.  
  52.     print();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement