Advertisement
rootUser

doublystack.cpp

May 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. void push(int x);
  4. void pop();
  5. void search(int x);
  6. using namespace std;
  7. typedef struct alvi
  8. {
  9.     int data;
  10.     struct alvi *prev;
  11.     struct alvi *next;
  12. }node;
  13. node *head=NULL;
  14. node *tail=NULL;
  15. int main(void)
  16. {
  17.  
  18.     push(1);
  19.     push(2);
  20.     push(3);
  21.     search(10);
  22.     //search(4);
  23.     pop();
  24.     return 0;
  25. }
  26. void push(int x)
  27. {
  28.     node *newNode = new node();
  29.     newNode->data = x;
  30.     if(head==NULL)
  31.     {
  32.         cout<<"First node pushed"<<endl;
  33.         newNode->prev=NULL;
  34.         newNode->next=NULL;
  35.         head=newNode;
  36.         tail=newNode;
  37.  
  38.     }
  39.     else
  40.     {
  41.         cout<<"other node pushed"<<endl;
  42.         head->prev=newNode;
  43.         newNode->next=head;
  44.         head=newNode;
  45.  
  46.         //head= newNode;
  47.     }
  48. }
  49. void pop()
  50. {
  51.     node *cursor;
  52.     cursor = tail;
  53.     while(cursor!=NULL)
  54.     {
  55.         cout<<cursor->data;
  56.         cursor=cursor->prev;
  57.     }
  58.     cout<<endl;
  59.     //cout<<cursor->prev->data;
  60. }
  61.  
  62. void search(int x)
  63. {
  64.     node *cursor1;
  65.     cursor1 = head;
  66.     int flag=0;
  67.     int counter = 1;
  68.     while(cursor1!=NULL)
  69.     {
  70.         if(cursor1->data==x)
  71.         {
  72.             flag=1;
  73.             cout<<"found"<<endl;;
  74.             cout<<"element number : "<<counter<<endl;
  75.             break;
  76.         }
  77.         cursor1=cursor1->next;
  78.         counter++;
  79.     }
  80.     if(flag==0)
  81.     {
  82.         cout<<"not found"<<endl;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement