-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
117 lines (106 loc) · 3.1 KB
/
Queue.cpp
File metadata and controls
117 lines (106 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;
// Define a macro constant for the maximum size of the queue
#define MAX_SIZE 100
// Declare an integer array for the queue
int queue[MAX_SIZE];
// Declare variables to keep track of the front and rear of the queue
int front = 0, rear = -1;
// Function to insert a value into the queue
void insert(int value)
{
// Check if the queue is full
if (rear == MAX_SIZE - 1)
{
// If the queue is full, print an error message
cout << "Queue is full, cannot insert " << value << endl;
}
else
{
// If the queue is not full, increment the rear index
rear++;
// Insert the value at the rear of the queue
queue[rear] = value;
// Print a message indicating that the value has been inserted
cout << value << " inserted into the queue" << endl;
}
}
// Function to delete a value from the queue
void deletion()
{
// Check if the queue is empty
if (rear == -1)
{
// If the queue is empty, print an error message
cout << "Queue is empty, nothing to delete" << endl;
}
else
{
// If the queue is not empty, store the front value in a variable
int value = queue[front];
// Increment the front index
front++;
// Print a message indicating that the value has been deleted
cout << value << " deleted from the queue" << endl;
}
}
// Function to display the values in the queue
void display()
{
// Check if the queue is empty
if (rear == -1)
{
// If the queue is empty, print an error message
cout << "Queue is empty, nothing to display" << endl;
}
else
{
// If the queue is not empty, print the values in the queue
cout << "Values in the queue are: ";
for (int i = front; i <= rear; i++)
{
cout << queue[i] << " ";
}
cout << endl;
}
}
int main()
{
// Declare a variable to store the user's choice
int choice;
// Run an infinite loop until the user chooses to exit
while (true)
{
// Print the menu options
cout << "1. Insert an element into the queue" << endl;
cout << "2. Delete an element from the queue" << endl;
cout << "3. Display the elements in the queue" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
// Read the user's choice
cin >> choice;
// Perform the corresponding operation based on the user's choice
switch (choice)
{
case 1: // Insert an element into the queue
int value;
cout << "Enter the value to be inserted: ";
cin >> value;
insert(value);
break;
case 2: // Delete an element from the queue
deletion();
break;
case 3: // Display the elements in the queue
display();
break;
case 4: // Exit
exit(0);
break;
default: // Invalid choice
cout << "Invalid choice, try again" << endl;
break;
}
}
return 0;
}