#include <iostream>
#include <conio.h>
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#define max 6 //Mendeklarasikan max data pada queue
int value[max];
int kepala = -1, ekor = -1, a;
//membuat fungsi IsEmpty
bool IsEmpty(){
if (kepala == -1 && ekor == -1)
return true;
else
return false;
}
//membuat fungsi IsFull
bool IsFull(){
if (ekor == max - 1)
return true;
else
return false;
}
//membuat enqueue, untuk memasukkan data kedalam queue / antrian
void Enqueue(){
if (IsFull()) {
cout << "Queue sudah numpuk, tunggu beberapa saat ";
}
else {
if (IsEmpty()){
kepala = ekor = 0;
cout << "Masukkan value : "; cin >> value[ekor];
}
else {
ekor++;
cout << "Masukkan value : "; cin >> value[ekor];
}
}
}
//Membuat fungsi Dequeue
void Dequeue(){
if (IsEmpty()){
cout << "Queue masih kosong ! ";
getch();
}
else {
cout << "Value yang diambil : " << value[kepala];
for (a = kepala; a <= ekor - 1; a++)
value[a] = value[a + 1];
ekor--;
if (ekor == -1)
kepala = -1;
getch();
}
}
//Membuat Prosedur clear
void Clear(){
kepala = ekor = -1;
cout << "Queue sudah dikosongkan ! "; getch();
}
//Membuat Prosedur View
void View(){
if (IsEmpty()){
cout << "Queue kosong ! ";
getch();
}
else {
for (a = kepala; a <= ekor; a++)
cout << "Value pada queue ke " << a << " = " << value[a] << endl;
getch();
}
}
int main(){
int jawab;
do{
if (system("CLS")) system("clear");
cout << "**********Program Queue**********" << endl;
cout << "1. Enqueue " << endl;
cout << "2. Dequeue " << endl;
cout << "3. Clear " << endl;
cout << "4. View " << endl;
cout << "5. Exit " << endl;
cout << "Masukkan pilihan Anda : ";
cin >> jawab;
switch (jawab){
case 1:
Enqueue(); break;
case 2:
Dequeue(); break;
case 3:
Clear(); break;
case 4:
View(); break;
}
} while (jawab != 5);
}