Advertisement
pervej56

Linked list add node last

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