Implement delete 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. Delete" << endl;cout << "3. Display" << endl;cout << "4. 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 deleted element is: " << queue[front]<< endl;front++;if(front > rear){front = rear = -1;}}}else if(choice == 3){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 != 4);}
Output
Enter the size of queue: 5
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the element to insert: 33
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the element to insert: 66
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the element to insert: 66
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
The elements in the queue are: 33 66 66
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
The deleted element is: 33
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
The elements in the queue are: 66 66
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
0 Comments
Post a Comment