Queue is a list of elements in which insertions are at one end of the list called rear end
and the deletions are at the other end of the list called FRONT end.
Insertion is also known as Enqueue,deletion is also known as Dequeue.

Insertion is also known as Enqueue,deletion is also known as Dequeue.
/****************************************************************************** Author: Sunil Kumar Website: www.techdoon.com Description: Queues implementaion using link list in c++ *******************************************************************************/ #include<iostream> #include<cstdlib> #include<memory> using namespace std; void menu(); struct node{ int info; struct node *link; }; class queue{ private: struct node *front,*rear; public: queue(); //constructor void insert(); //inserting element to the que void delet(); //removing elements from the que int isEmpty(); //Checking for empty void display(); //to fatch the data }; queue::queue() //constructor initializing variables { front=rear=NULL; } int queue::isEmpty() { if(front==NULL) return 1; else return 0; } void queue::insert() { struct node *temp; int item; cout<<"\nITEM: "; cin>>item; temp=new node; temp->info=item; temp->link=NULL; if(front==NULL) { front=temp; } else { rear->link=temp; } rear=temp; cout<<"\nNew item is inserted to the Queue!!!"; } void queue::delet() { struct node *temp; int item,i; if((i=isEmpty())==1) cout<<"UNDERFLOW"; else { item=front->info; temp=front; front=temp->link; cout<<"\n "<<item<<"is Deleted."; } } int main() { menu(); cin.get(); return 0; } void menu() { queue obj; int ch; while(1) { cout<<"\n1.INSERT.\n2.DELETE.\n3.DISPLAY.\n0.EXIT."; cin>>ch; switch(ch) { case 1: obj.insert(); break; case 2: obj.delet(); break; case 3:obj.display(); case 0:exit(0); default: cout<<"INVALID."; } } } void queue:: display() { if(front==NULL){ cout<<"QUEUE is Empty."; menu(); } else{ while(front!=NULL){ cout<<front->info<<" "; front=front->link; } menu(); } }
0 comments:
Post a Comment