This C++ Program demonstrates operations on Queue.
Here is source code of the C++ Program to demonstrate Queue operations using linked list. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program To Implement Queue using Linked List
*/
#include<iostream>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *link;
}*front, *rear;
/*
* Class Declaration
*/
class queue_list
{
public:
void insert(int);
void display();
void del();
queue_list()
{
front = NULL;
rear = NULL;
}
};
/*
* Main: Contains Menu
*/
int main()
{
int choice, item;
queue_list ql;
while (1)
{
cout<<"\n-------------"<<endl;
cout<<"Operations on Queue"<<endl;
cout<<"\n-------------"<<endl;
cout<<"1.Insert Element into the Queue"<<endl;
cout<<"2.Delete Element from the Queue"<<endl;
cout<<"3.Traverse the Queue"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted into the queue: ";
cin>>item;
ql.insert(item);
break;
case 2:
ql.del();
break;
case 3:
ql.display();
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
/*
* Insert Element into the Queue
*/
void queue_list::insert(int item)
{
node *tmp;
tmp = new (struct node);
tmp->info = item;
tmp->link = NULL;
if (front == NULL)
front = tmp;
else
rear->link = tmp;
rear = tmp;
}
/*
* Delete Element from the Queue
*/
void queue_list::del()
{
node *tmp;
if (front == NULL)
cout<<"Queue Underflow"<<endl;
else
{
tmp = front;
cout<<"Element Deleted: "<<tmp->info<<endl;
front = front->link;
free(tmp);
}
}
/*
* Traversing the Stack
*/
void queue_list::display()
{
node *ptr;
ptr = front;
if (front == NULL)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements :"<<endl;
while (ptr != NULL)
{
cout<<ptr->info<<" ";
ptr = ptr->link;
}
cout<<endl;
}
}
$ g++ queue.cpp $ a.out ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue is empty ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 2 Queue Underflow ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 1 Enter value to be inserted into the queue: 100 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 100 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 1 Enter value to be inserted into the queue: 200 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 100 200 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 1 Enter value to be inserted into the queue: 150 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 100 200 150 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 1 Enter value to be inserted into the queue: 50 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 100 200 150 50 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 2 Element Deleted: 100 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 200 150 50 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 2 Element Deleted: 200 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 150 50 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 1 Enter value to be inserted into the queue: 1010 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 3 Queue elements : 150 50 1010 ------------- Operations on Queue ------------- 1.Insert Element into the Queue 2.Delete Element from the Queue 3.Traverse the Queue 4.Quit Enter your Choice: 4 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Computer Science MCQs
- Check Data Structure Books
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice Design & Analysis of Algorithms MCQ