rafid_shad

circular linked list.cpp

Nov 12th, 2018 (edited)
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8.  
  9. struct node *head=NULL,*temp;
  10.  
  11. void display()
  12. {
  13.     cout<<endl;
  14.     temp=head;
  15.     while(temp!=NULL)
  16.     {
  17.         cout<<temp->data<<endl;
  18.         temp=temp->next;
  19.         if(temp==head)
  20.         {
  21.             break;
  22.         }
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     struct node *n1,*n2,*n3;
  29.     n1=(struct node*) malloc (sizeof(struct node));
  30.     n2=(struct node*) malloc (sizeof(struct node));
  31.     n3=(struct node*) malloc (sizeof(struct node));
  32.  
  33.     cin>>n1->data>>n2->data>>n3->data;
  34.  
  35.     head=n1;
  36.     n1->next=n2;
  37.     n2->next=n3;
  38.     n3->next=n1;
  39.  
  40.     display();
  41.  
  42.     return 0;
  43. }
Add Comment
Please, Sign In to add comment