-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear-queue.cpp
102 lines (95 loc) · 1.75 KB
/
linear-queue.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
#include <iostream>
#define MAX 20
using namespace std;
struct Queue
{
int front, rear, data[MAX];
} Q;
bool isFull()
{
return Q.rear == MAX;
}
bool isEmpty()
{
return Q.rear == 0;
}
void printQueue()
{
if (isEmpty())
{
cout << "Antrian kosong"<<endl;
}
else
{
cout << "QUEUE : ";
for (int i = Q.front; i < Q.rear; i++)
cout << "[" << Q.data[i] << "]";
cout << endl;
}
}
void enqueue()
{
if (isFull())
{
cout << "Antrian penuh!"<<endl;
}
else
{
int data;
cout << "Masukkan Data : ";
cin >> data;
Q.data[Q.rear] = data;
Q.rear++;
cout << "Data ditambahkan\n";
printQueue();
}
}
void dequeue()
{
if (isEmpty())
{
cout << "Antrian masih kosong"<<endl;
}
else
{
cout << "Mengambil data [" << Q.data[Q.front] << "]" << endl;
for (int i = Q.front; i < Q.rear; i++)
Q.data[i] = Q.data[i + 1];
Q.rear--;
printQueue();
}
}
int main()
{
int choose;
do
{
cout << "-------------------" <<endl;
cout << " > Menu Pilihan" <<endl;
cout << "-------------------" <<endl;
cout << " [1] Enqueue" <<endl;
cout << " [2] Dequeue" <<endl;
cout << " [3] Keluar" <<endl;
cout << "-------------------" <<endl;
cout << "Pilih Menu : ";
cin >> choose;
if (choose==1)
{
enqueue();
}
else if (choose==2)
{
dequeue();
}
else if (choose==3)
{
return 0;
}
else
{
cout << "Input yang benar!" <<endl;
}
}
while (true);
return 0;
}