#include<iostream> #include<iomanip> using namespace std; class node { public: int info; node *next; }; class Queue { public: node *rear; node *front; Queue() { cout<<"\n\t\t QUEUES USING SINGLE LINKED LIST\n"; rear = NULL; front = NULL; } void enque(); void deque(); void display(); }; void Queue::enque() // adding the elements { int data; node *temp = new node; cout<<"\n\nEnter the data to enque: "; cin>>data; temp->info = data; temp->next = NULL; if(front == NULL) { front = temp; } else { rear->next = temp; } rear = temp; } void Queue::deque() { node *temp = new node; // removing the elements if(front == NULL) { cout<<"\nQueue is Emtpty\n"; } else if(front->next==NULL) { temp = front; cout<<"\n\nThe data REMOVED is "<<front->info<<"\n"; front=NULL; } else { temp = front; front = front->next; cout<<"\n\nThe data REMOVED is "<<temp->info<<"\n"; delete temp; } } void Queue::display() { node *p = new node; p = front; if(front == NULL) { cout<<"\n\nNothing to Display\n"; } else { cout<<"\n\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...
-
BUBBLE SORT : Bubble sort is a simple sorting algorithm ...
-
Keywords – II Enum : An enum type is a special data type that enables for a variable to be a set of predefined co...
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.