-
Notifications
You must be signed in to change notification settings - Fork 0
/
5linearqueue_array.cpp
138 lines (120 loc) · 2.92 KB
/
5linearqueue_array.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 5. Implement Linear Queue ADT using array.
// Theory:
// This code implements a queue data structure using an array. It
// provides functionalities to enqueue elements into the queue,
// dequeue elements from the queue, and display the elements currently
// in the queue. The main function offers a menu-driven interface for
// users to interact with the queue.
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Queue
{
private:
int front, rear;
int arr[MAX_SIZE];
public:
Queue()
{
front = -1;
rear = -1;
}
bool isEmpty()
{
return (front == -1 && rear == -1);
}
bool isFull()
{
return (rear == MAX_SIZE - 1);
}
void enqueue(int data)
{
if (isFull())
{
cout << "queue is full" << endl;
return;
}
else if (isEmpty())
{
front = rear = 0;
}
else
{
rear++;
}
arr[rear] = data;
cout << data << " enqueued to queue" << endl;
}
void dequeue()
{
if (isEmpty())
{
cout << "queue is empty" << endl;
return;
}
else if (front == rear)
{
cout << arr[front] << " dequeued from queue" << endl;
front = rear = -1;
}
else
{
cout << arr[front] << " dequeued from queue." << endl;
front++;
}
}
void display()
{
if (isEmpty())
{
cout << "queue is empty" << endl;
return;
}
cout << "elements in the queue: ";
for (int i = front; i <= rear; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
};
int main()
{
Queue q;
int choice, data;
do {
cout << "\n1. enqueue\n";
cout << "2. dequeue\n";
cout << "3. display\n";
cout << "4. exit\n";
cout << "choose an option : ";
cin >> choice;
switch(choice)
{
case 1:
cout << "enter elements to enqueue: ";
cin >> data;
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
cout << "exited!" << endl;
break;
default:
cout << "invalid choice." << endl;
}
}
while (choice != 4);
return 0;
}
// Conclusion:
// The code offers a basic implementation of a queue using an array,
// providing essential operations such as enqueue, dequeue, and
// display. It offers a user-friendly menu interface for interacting
// with the queue, allowing users to enqueue elements, dequeue
// elements, and view the elements currently in the queue.