Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int element=0;
- void push(int x);
- void pop();
- void search(int x);
- typedef struct alvi
- {
- int data;
- struct alvi *prev;
- struct alvi *next;
- }node;
- node *head = NULL;
- node *tail = NULL;
- int main(void)
- {
- int choice,push_number,search_number,remove_number,olds,news;
- while(1)
- {
- cout<<"Choose : 1 to push,2 to pop,3 to search,4 to quit"<<endl;
- cin>>choice;
- switch(choice)
- {
- case 1:
- cout<<"Enter number to push : ";
- cin>>push_number;
- push(push_number);
- break;
- case 2:
- pop();
- break;
- case 3:
- cout<<"Enter number to search : ";
- cin>>search_number;
- search(search_number);
- break;
- case 4:
- return 0;
- default:
- cout<<"Wrong input"<<endl;
- break;
- }
- }
- return 0;
- }
- void push(int x)
- {
- node *newNode = new node();
- newNode->data = x;
- if(head==NULL)
- {
- cout<<"First node pushed"<<endl;
- newNode->prev=NULL;
- newNode->next=NULL;
- head=newNode;
- tail=newNode;
- }
- else
- {
- cout<<"other node pushed"<<endl;
- head->prev=newNode;
- newNode->next=head;
- head=newNode;
- //make the list circular
- newNode->prev=tail;
- tail->next=head;
- }
- element++;
- //head->next=tail;
- }
- void pop()
- {
- node *cursor;
- cursor = tail;
- while(cursor!=NULL && cursor!=head)
- {
- cout<<cursor->data<<" ";
- cursor=cursor->prev;
- }
- cout<<head->data;
- cout<<endl;
- }
- void search(int x)
- {
- int b[element],j=0,k=0,flag=0;
- node *cursor;
- cursor = tail;
- if(head->data==x )
- {
- b[element-1]=x;
- flag=1;
- }
- while(cursor!=NULL && cursor!=head)
- {
- b[j]=cursor->data;
- j++;
- //cout<<cursor->data<<" ";
- cursor=cursor->prev;
- }
- while(k<element+1)
- {
- cout<<b[k]<<" ";
- if(b[k]==x)
- {
- flag=1;
- break;
- }
- k++;
- }
- if(flag==1)
- {
- cout<<"found"<<endl;
- }
- else
- {
- cout<<"not found"<<endl;
- }
- }
Add Comment
Please, Sign In to add comment