Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- void push(int x);
- void pop();
- void search(int x);
- typedef struct alvi
- {
- int data;
- struct alvi *next;
- } node;
- node *head = NULL;
- node *tail = NULL;
- int main(void)
- {
- int choice,number;
- while(1)
- {
- cout<<"Enter 1 to PUSH , 2 to POP , 3 to SEARCH , 4 to QUIT : ";
- cin>>choice;
- switch(choice)
- {
- case 1:
- cout<<"Enter a number to insert in the Linked list : ";
- cin>>number;
- push(number);
- break;
- case 2:
- pop();
- break;
- case 3:
- cout<<"Enter a number to search in the Linked list : ";
- cin>>number;
- 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;
- newNode->next=NULL;
- if(head==NULL)
- {
- tail = newNode;
- cout<<"First node pushed"<<endl;
- }
- else
- {
- newNode->next=tail;
- node *cursor = head;
- cursor->next=newNode;
- cout<<"Another node pushed"<<endl;
- }
- head=newNode;
- }
- void pop()
- {
- node *cursor=tail;
- cout<<"Queue :";
- cout<<" "<<cursor->data;
- while(cursor!=NULL && cursor->next!=tail)
- {
- cout<<" "<<(cursor->next)->data;
- cursor=cursor->next;
- }
- cout<<endl;
- }
- void search(int x)
- {
- node *cursor=tail;
- int flag = 0 , counter = 1;
- if(head->data==x ||tail->data==x)
- {
- flag=1;
- }
- while(cursor!=NULL && cursor->next!=tail)
- {
- if(cursor->data==x)
- {
- flag=1;
- break;
- }
- counter++;
- cursor=cursor->next;
- }
- if(flag==1)
- {
- cout<<"Found . The element "<<x<<" is in the position "<<counter<<" in the queue"<<endl;
- }
- else
- {
- cout<<"The element "<<x<<" is not found in the queue"<<endl;
- }
- }
Add Comment
Please, Sign In to add comment