[+] Post Title :
[+] Date : Sunday, 20 October 2013
[+] Author : Prudhvi raj
[+] Type : Queue L L
Data Structure Queue Implementation of Linked List in C++
[+] Date : Sunday, 20 October 2013
[+] Author : Prudhvi raj
[+] Type : Queue L L
Algorithm
1. Declare and initialize necessary variables such as struct node *front, *rear etc 2. For enqueue operation, -take input data to be inserted -create an empty node and assign data to its into field i.e. p->info=data p->next = NULL if front = NULL front = p else rear->next = p -rear = p 3. For next enqueue operation, goto step 2 4. For dequeue operation, if front = NULL print "Queue is empty" else -create a node pointer temp -temp = front -front = front->next -display dequeued item as temp->data -delete temp 5. For dequeue of next data item, goto step 4
Program:-
#include<iostream>#include<cstdlib>using namespace std;struct node{int info;struct node *next;};class Queue{private:node *rear;node *front;public:Queue();void enqueue();void dequeue();void display();};Queue::Queue(){rear = NULL;front = NULL;}void Queue::enqueue(){int data;node *temp = new node;cout<<"Enter the data to enqueue: ";cin>>data;temp->info = data;temp->next = NULL;if(front == NULL){front = temp;}else{rear->next = temp;}rear = temp;}void Queue::dequeue(){node *temp = new node;if(front == NULL){cout<<"\nQueue is Emtpty\n";}else{temp = front;front = front->next;cout<<"The data Dequeued is "<<temp->info;delete temp;}}void Queue::display(){node *p = new node;p = front;if(front == NULL){cout<<"\nNothing to Display\n";}else{while(p!=NULL){cout<<endl<<p->info;p = p->next;}}}int main(){Queue queue;int choice;while(true){cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";cout<<"\nEnter your choice: ";cin>>choice;switch(choice){case 1:queue.enqueue();break;case 2:queue.dequeue();break;case 3:queue.display();break;case 4:exit(0);break;default:cout<<"\nInvalid Input. Try again! \n";break;}}return 0;}
