Implement insert algorithms of queue using array in C++
Code
// insertion in queue#include <iostream>using namespace std;int main(){int queue[10], front=-1, rear=-1, choice, n, i;cout << "Enter the size of queue: ";cin >> n;do{cout << "1. Insert" << endl;cout << "2. Display" << endl;cout << "3. Exit" << endl;cout << "Enter your choice: ";cin >> choice;if(choice == 1){if(rear == n-1){cout << "Queue is full";}else{rear++;cout << "Enter the element to insert: ";cin >> queue[rear];if(front == -1){front = 0;}}}else if(choice == 2){if(front == -1){cout << "Queue is empty";}else{cout << "The elements in the queue are: ";for(i=front; i<=rear; i++){cout << queue[i] << " ";}cout << endl;}}}while(choice != 3);}
Output
Enter the size of queue: 5
1. Insert
2. Display
3. Exit
Enter your choice: 1
Enter the element to insert: 5
1. Insert
2. Display
3. Exit
Enter your choice: 1
Enter the element to insert: 5
1. Insert
2. Display
3. Exit
Enter your choice: 1
Enter the element to insert: 6
1. Insert
2. Display
3. Exit
Enter your choice: 2
The elements in the queue are: 5 5 6
1. Insert
2. Display
3. Exit
Enter your choice: 3
0 Comments
Post a Comment