Simple Queue Program in C++ Programming
Definition
In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue ?dequeue? function. Every time another object or customer enters the line to wait, they join the end of the line and represent the ?enqueue? function. The queue ?size? function would return the length of the line, and the ?empty? function would return true only if there was nothing in the line.
Example Program
/* Simple Queue Program in C++*/
/* Data Structure C++ Programs,C++ Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 100
using namespace std;
int main() {
int item, choice, i;
int arr_queue[MAX_SIZE];
int rear = 0;
int front = 0;
int exit = 1;
cout << "\nSimple Queue Example - Array";
do {
cout << "\n\n Queue Main Menu";
cout << "\n1.Insert \n2.Remove \n3.Display \nOthers to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
if (rear == MAX_SIZE)
cout << "\n## Queue Reached Max!!";
else {
cout << "\nEnter The Value to be Insert : ";
cin>>item;
cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;
arr_queue[rear++] = item;
}
break;
case 2:
if (front == rear)
cout << "\n## Queue is Empty!";
else {
cout << "\n## Position : " << front << " , Remove Value :" << arr_queue[front];
front++;
}
break;
case 3:
cout << "\n## Queue Size : " << (rear - front);
for (i = front; i < rear; i++)
cout << "\n## Position : " << i << " , Value : " << arr_queue[i];
break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}
Sample Output
Simple Queue Example - Array
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 100
## Position : 1 , Insert Value : 100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 200
## Position : 2 , Insert Value : 200
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 300
## Position : 3 , Insert Value : 300
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 400
## Position : 4 , Insert Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 0 , Value : 100
## Position : 1 , Value : 200
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 0 , Remove Value : 100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 1 , Value : 200
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 1 , Remove Value : 200
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 4
## Position : 2 , Value : 300
## Position : 3 , Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 1100
## Position : 5 , Insert Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3
## Queue Size : 5
## Position : 2 , Value : 300
## Position : 3 , Value : 400
## Position : 4 , Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 2 , Remove Value : 300
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 3 , Remove Value : 400
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Position : 4 , Remove Value : 1100
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2
## Queue is Empty!
Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 4
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming