And NO, there is no meaning to adding a special operation called ‘Create’ the queue. When you push in the first element, the queue is created. Of course I didn’t bother to check for exceptions. This is a straight off the text book program, suck on it bitches.
#include<iostream.h> #include<conio.h> struct node { int data; node *next; }*p,*front,*rear; void display(); void push(int); void pop(); int main() { int choice,item; while(choice != 4) { cout<<"\n Enter your choice : "; cout<<"\n 1. Push an element into Queue : " <<"\n 2. Pop an element. " <<"\n 3. Display elements in queue. " <<"\n 4. Exit.\n "; cin>>choice; switch(choice) { case 1: cout<<"\nEnter the element you want to insert : "; cin>>item; push(item); break; case 2: pop(); break; case 3: display(); break; case 4: break; default: cout<<"\n Invalid choice."; break; } } getch(); } void push(int x) { if(front==NULL) { node *q=new node; q->data=x; q->next=front; front=q; rear=q; front->next=rear; } else { node * t= new node; t->data=x; rear->next=t; rear=t; } } void display() { cout<<"\n\n The queue is : \n"; node* temp=new node; temp=front; if(temp==NULL) cout<<"Queue is empty!"; else { do { cout<<temp->data<<" << "; temp=temp->next; } while(temp!=rear); if(front!=rear) cout<<rear->data; } } void pop() { if(front->next==rear) { cout<<"\n The Queue is:\n"<<rear->data; } else { node* d; d=front; front=front->next; delete d; } }
Hey, you forgot to add clrscr() just after main(), should look like
Oh right. Totally forgot that.