rafid_shad

doubly linked list.cpp

Nov 12th, 2018 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     struct node *pre,*next;
  7. };
  8.  
  9. struct node *head=NULL,*temp,*cur,*pre,*last;
  10.  
  11. void insert(int v)
  12. {
  13.     pre=head;
  14.     cur=head->next;
  15.     struct node *newnode;
  16.     newnode= (struct node*)malloc(sizeof(struct node));
  17.     cout<<"Enter information for newnode"<<endl;
  18.     cin>>newnode->data;
  19.     newnode->next=NULL;
  20.     newnode->pre=NULL;
  21.  
  22.     if(pre->data==v)
  23.     {
  24.         newnode->next=pre;
  25.         pre->pre=newnode;
  26.         head=newnode;
  27.     }
  28.     else
  29.     {
  30.         while(cur->data!=v && cur->next!=NULL)
  31.         {
  32.             pre=cur;
  33.             cur=cur->next;
  34.         }
  35.         if(cur->data==v)
  36.         {
  37.             newnode->next=cur;
  38.             cur->pre=newnode;
  39.             pre->next=newnode;
  40.             newnode->pre=pre;
  41.         }
  42.         if(cur->data!=v && cur->next==NULL)
  43.         {
  44.             cout<<"Sorry!! Not found"<<endl;
  45.         }
  46.     }
  47. }
  48.  
  49. void deletedata(int v)
  50. {
  51.     pre=head;
  52.     cur=head->next;
  53.     if(pre->data==v)
  54.     {
  55.         cur->pre=NULL;
  56.         head=cur;
  57.         delete(pre);
  58.     }
  59.     else
  60.     {
  61.         while(cur->data!=v && cur->next!=NULL)
  62.         {
  63.             pre=cur;
  64.             cur=cur->next;
  65.         }
  66.         if(cur->data==v && cur->next!=NULL)
  67.         {
  68.             cur->next->pre=pre;
  69.             pre->next=cur->next;
  70.             delete(cur);
  71.         }
  72.         if(cur->data==v && cur->next==NULL)
  73.         {
  74.             pre->next=NULL;
  75.             delete(cur);
  76.         }
  77.         else
  78.         {
  79.             cout<<"OPPS!! Not found"<<endl;
  80.         }
  81.     }
  82. }
  83.  
  84. void display()
  85. {
  86.     temp=head;
  87.  
  88.     while(temp!=NULL)
  89.     {
  90.         cout<<temp->data<<endl;
  91.         temp=temp->next;
  92.     }
  93. }
  94.  
  95. void revdisplay()
  96. {
  97.     temp=last;
  98.     while(temp!=NULL)
  99.     {
  100.         cout<<temp->data<<endl;
  101.         temp=temp->pre;
  102.     }
  103. }
  104.  
  105. int main()
  106. {
  107.     cout<<"Enter how many node you want"<<endl;
  108.     int n;
  109.     cin>>n;
  110.     struct node *s;
  111.     s= (struct node*)malloc(sizeof(struct node));
  112.     for(int i=1; i<=n; i++)
  113.     {
  114.         struct node *newnode;
  115.         newnode= (struct node*)malloc(sizeof(struct node));
  116.  
  117.         cin>>newnode->data;
  118.         newnode->next=NULL;
  119.         newnode->pre=NULL;
  120.  
  121.         if(head==NULL)
  122.         {
  123.             head=newnode;
  124.             s=head;
  125.             last=head;
  126.         }
  127.         else
  128.         {
  129.             s->next=newnode;
  130.             newnode->pre=s;
  131.             newnode->next=NULL;
  132.             s=newnode;
  133.             last=newnode;
  134.         }
  135.     }
  136.     cout<<endl;
  137.  
  138.     display();
  139.     cout<<endl;
  140.     revdisplay();
  141.  
  142.     int v;
  143.     cout<<"Enter a data where u want to insert"<<endl;
  144.     cin>>v;
  145.  
  146.     insert(v);
  147.  
  148.     cout<<endl;
  149.     display();
  150.  
  151.     cout<<"Enter a data which you want to delete"<<endl;
  152.     cin>>v;
  153.    
  154.     deletedata(v);
  155.    
  156.     cout<<endl;
  157.     display();
  158.     return 0;
  159. }
Add Comment
Please, Sign In to add comment