#include<iostream> #include<iomanip> using namespace std; class node { public: int info; node *next; node *prev; }; class Queue { public: node *rear; node *front; Queue() { rear = NULL; front = NULL; cout<<"\n\t\tQUEUES USING DOUBLE LINKED LIST\n"; } void enque(); void deque(); void display(); }; void Queue::enque() { int data; node *temp = new node; cout<<"Enter the data to enque: "; cin>>data; temp->info = data; temp->next = NULL; temp->prev=NULL; if(front == NULL) { front = temp; } else { rear->next = temp; temp->prev=rear; } rear = temp; } void Queue::deque() { node *temp = new node; if(front == NULL) { cout<<"\nQueue is Emtpty\n"; } else if(front->next==NULL) { temp = front; cout<<"\nThe data REMOVED is "<<front->info<<"\n"; front=NULL; } else { temp = front; front = front->next; front->prev=NULL; cout<<"\nThe data REMOVED is "<<temp->info<<"\n"; delete temp; } } void Queue::display() { node *p = new node; p = front; if(front == NULL) { cout<<"\nNothing to Display\n"; } else { cout<<"\nTHE ELEMENTS ARE : "; while(p!=NULL) { cout<<p->info<<" "; p = p->next; } } cout<<"\n"; } int main() { Queue queue; int choice; while(1) { cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit"; cout<<"\nEnter your choice: "; cin>>choice; switch(choice) { case 1: queue.enque(); break; case 2: queue.deque(); break; case 3: queue.display(); break; case 4: exit(0); break; default: cout<<"\nInvalid Input. Try again! \n"; break; } } return 0; }
Labels
Popular Posts
-
Binary Tree: A Tree in which each node has a degree of atmost 2. i.e. it can have either 0,1 or 2 children. Here, leaves a...
-
Recurrence relation : A recurrence relation is an equation that defines a sequence based on a rule that gives the next term as a funct...
-
BUBBLE SORT : Bubble sort is a simple sorting algorithm ...
Blog Archive
- March 2017 (1)
- March 2016 (1)
- October 2015 (1)
- September 2015 (1)
- August 2015 (5)
- July 2015 (2)
- June 2015 (4)
- January 2015 (4)
- December 2014 (5)
- November 2014 (1)
- October 2014 (1)
- September 2014 (3)
Powered by Blogger.