Advertisement
Guest User

Insert at the end of a linked list

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