C++ Program to Implement Queue

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.

  1. /*
  2.  * C++ Program To Implement Queue using Linked List
  3.  */
  4. #include<iostream>
  5. #include<cstdlib>
  6. using namespace std;
  7.  
  8. /*
  9.  * Node Declaration
  10.  */
  11. struct node
  12. {
  13.     int info;
  14.     struct node *link;    
  15. }*front, *rear;
  16.  
  17. /*
  18.  * Class Declaration
  19.  */
  20. class queue_list
  21. {
  22.     public:
  23.         void insert(int);
  24.         void display();
  25.         void del();
  26.         queue_list()
  27.         {
  28.             front = NULL;
  29.             rear = NULL;
  30.         }               
  31. };
  32.  
  33. /*
  34.  * Main: Contains Menu
  35.  */
  36. int main()
  37. {
  38.     int choice, item;
  39.     queue_list ql;
  40.     while (1)
  41.     {
  42.         cout<<"\n-------------"<<endl;
  43.         cout<<"Operations on Queue"<<endl;
  44.         cout<<"\n-------------"<<endl;
  45.         cout<<"1.Insert Element into the Queue"<<endl;
  46.         cout<<"2.Delete Element from the Queue"<<endl;
  47.         cout<<"3.Traverse the Queue"<<endl;
  48.         cout<<"4.Quit"<<endl;
  49.         cout<<"Enter your Choice: ";
  50.         cin>>choice;
  51.         switch(choice)
  52.         {
  53.         case 1:
  54.             cout<<"Enter value to be inserted into the queue: ";
  55.             cin>>item;
  56.             ql.insert(item);
  57.             break;
  58.         case 2:
  59.             ql.del();
  60.             break;
  61.         case 3:
  62.             ql.display();
  63.             break;
  64.         case 4:
  65.             exit(1);
  66.             break;
  67.         default:
  68.             cout<<"Wrong Choice"<<endl;
  69.         }
  70.     }
  71.     return 0;
  72. }
  73.  
  74. /*
  75.  * Insert Element into the Queue
  76.  */
  77. void queue_list::insert(int item)
  78. {
  79.     node *tmp;
  80.     tmp = new (struct node);
  81.     tmp->info = item;
  82.     tmp->link = NULL;
  83.     if (front == NULL)
  84.         front = tmp;
  85.     else
  86.         rear->link = tmp;
  87.     rear = tmp;
  88. }
  89.  
  90. /*
  91.  * Delete Element from the Queue
  92.  */
  93. void queue_list::del()
  94. {
  95.     node *tmp;
  96.     if (front == NULL)
  97.         cout<<"Queue Underflow"<<endl;
  98.     else
  99.     {       
  100.         tmp = front;
  101.         cout<<"Element Deleted: "<<tmp->info<<endl;
  102.         front = front->link;
  103.         free(tmp);
  104.     }
  105. }
  106.  
  107. /*
  108.  * Traversing the Stack
  109.  */
  110. void queue_list::display()
  111. {       
  112.     node *ptr;
  113.     ptr = front;
  114.     if (front == NULL)
  115.         cout<<"Queue is empty"<<endl;
  116.     else
  117.     {
  118.         cout<<"Queue elements :"<<endl;
  119.         while (ptr != NULL)
  120.         {
  121.             cout<<ptr->info<<" ";
  122.             ptr = ptr->link;
  123.         }
  124.         cout<<endl;
  125.     }
  126. }

$ 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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.