-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue
More file actions
62 lines (49 loc) · 1 KB
/
queue
File metadata and controls
62 lines (49 loc) · 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
#include <iostream>
using namespace std;
void arrayInsertion(int a[], int &n, int i, int x) {
for (int j = n; j > i; j--) {
a[j] = a[j - 1];
}
a[i] = x;
n++;
}
void arrayDeletion(int a[], int &n, int i) {
for (int j = i; j < n - 1; j++) {
a[j] = a[j + 1];
}
n--;
}
void enqueue(int a[], int &n, int x) {
int i = 0;
while (i < n && x >= a[i]) {
i++;
}
arrayInsertion(a, n, i, x);
}
int dequeue(int a[], int &n) {
if (n == 0) {
cout << "Queue empty!\n";
return -1;
}
int x = a[0];
arrayDeletion(a, n, 0);
return x;
}
void display(int a[], int n) {
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main() {
int a[100];
int n = 0;
enqueue(a,n,45);
enqueue(a,n,30);
enqueue(a,n,25);
enqueue(a,n,20);
cout << "Queue: ";
display(a, n);
cout << "Dequeued: " << dequeue(a, n) << endl;
cout << "Queue after dequeue: ";
display(a, n);
}