amanjain110893

Stack operation using Doubly LinkedList

Sep 14th, 2014
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct ListNode{
  6.     int val;
  7.     ListNode *next,*prev;
  8.     ListNode(int v){
  9.         val=v;
  10.         next=prev=NULL;
  11.     }
  12. }ListNode;
  13.  
  14. class Stack {
  15.     public:
  16.     ListNode *head,*tail,*middle;
  17.     int co;
  18.     Stack(){
  19.         head=tail=middle=NULL;
  20.         co=0;
  21.     }
  22.     void push(int v){
  23.         if(head==NULL){
  24.             head=new ListNode(v);
  25.             tail=head;
  26.         }
  27.         else{
  28.             ListNode* t=new ListNode(v);
  29.             t->next=head;
  30.             head->prev=t;
  31.             head=t;
  32.         }
  33.         ++co;
  34.         if(co==1){
  35.             middle=head;
  36.         }
  37.         else{
  38.             if(!(co&1)){
  39.                 middle=middle->prev;
  40.             }
  41.         }
  42.     }
  43.  
  44.     void pop(){
  45.         if(co==0){
  46.             cout<<"stack is empty\n";
  47.             return;
  48.         }
  49.         ListNode* t=head;
  50.         head=head->next;
  51.         free(t);
  52.         --co;
  53.         if(co==0){
  54.             head=tail=middle=NULL;
  55.             return;
  56.         }
  57.         if(co==1){
  58.             middle=head;
  59.         }
  60.         else{
  61.             if(co&1){
  62.                 middle=middle->next;
  63.             }
  64.         }
  65.     }
  66.     int getmiddle(){
  67.         if(co==0){
  68.             cout<<"stack is empty\n";
  69.             return -1;
  70.         }
  71.         else{
  72.             return middle->val;
  73.         }
  74.     }
  75.     void popmiddle(){
  76.         if(co==0){
  77.             cout<<"stack is empty\n";
  78.             return;
  79.         }
  80.         --co;
  81.         ListNode* t=middle;
  82.         if(co==0){
  83.             free(t);
  84.             head=tail=middle=NULL;
  85.             return;
  86.         }
  87.         if((co&1)){
  88.             middle=middle->next;
  89.             if(t->prev==NULL){
  90.                 head=middle;
  91.                 if(middle)middle->prev=NULL;
  92.                 free(t);
  93.             }
  94.             else{
  95.                 t->prev->next=middle;
  96.                 if(middle)middle->prev=t->prev;
  97.                 free(t);
  98.             }
  99.         }
  100.         else{
  101.             middle=middle->prev;
  102.             middle->next=t->next;
  103.             if(t->next!=NULL){
  104.                 t->next->prev=middle;
  105.             }
  106.             free(t);
  107.         }
  108.     }
  109.     void print(){
  110.         if(head==NULL){
  111.             cout<<"stack is empty\n";
  112.             return;
  113.         }
  114.         for(ListNode* t=head;t!=NULL;t=t->next){
  115.             cout<<t->val<<" ";
  116.         }
  117.         cout<<"\n";
  118.     }
  119. };
  120.        
  121. int main(){
  122.     Stack s;
  123.     while(1){
  124.         int op;
  125.         cin>>op;
  126.         switch(op){
  127.             case 0:
  128.             int x;
  129.             cin>>x;
  130.             s.push(x);
  131.             break;
  132.             case 1:
  133.             s.pop();
  134.             break;
  135.             case 2:
  136.             s.print();
  137.             break;
  138.             case 3:
  139.             cout<<s.getmiddle()<<"\n";
  140.             break;
  141.             case 4:
  142.             s.popmiddle();
  143.             break;
  144.         }
  145.     }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment