Advertisement
Guest User

Link list-3 (Insert at the end of a linked list)

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