Contoh queue menggunakan array bahasa c
Kasus : antrian mobil di gerbang tol
== program ==
#include <iostream>
#include <string.h>
#include <string>
#include<conio.h>
#define MAX 5
using namespace std;
struct Queue{
int front, rear, gerbang[MAX];
} Q;
struct Queue1{
int front, rear, gerbang[MAX];
} Q1;
int label;
bool isFull(int a){
if(a == 1){
return Q.rear == MAX;
} else if(a == 2){
return Q1.rear == MAX;
} else {
return false;
}
}
bool isEmpty(int a){
if(a == 1){
return Q.rear == 0;
} else if(a == 2){
return Q1.rear == 0;
} else {
return false;
}
}
void printQueue(){
cout << "+--------------------------------------+"<<endl;
cout << "| Gerbang Tol 1 Tersedia "<< MAX - Q.rear <<" Antrian |"<<endl;
cout << "+======================================+"<<endl;
if(isEmpty(1)){
cout << " Antrian kosong "<<endl;
} else {
for (int i = Q.front; i < Q.rear; i++){
cout<<"+----+ ";
}
cout<<endl;
for (int i = Q.front; i < Q.rear; i++){
cout<<"| "<<Q.gerbang[i]<<((Q.gerbang[i] < 10) ? " " : "")<<" | ";
}
cout <<endl;
for (int i = Q.front; i < Q.rear; i++){
cout<<"+----+ ";
}
cout<< endl;
}
cout << "+======================================+"<<endl<<endl;
cout << "+--------------------------------------+"<<endl;
cout << "| Gerbang Tol 2 Tersedia "<< MAX - Q1.rear <<" Antrian |"<<endl;
cout << "+======================================+"<<endl;
if(isEmpty(2)){
cout << " Antrian kosong "<<endl;
} else {
for (int i = Q1.front; i < Q1.rear; i++){
cout<<"+----+ ";
}
cout<<endl;
for (int i = Q1.front; i < Q1.rear; i++){
cout<<"| "<<Q1.gerbang[i]<<((Q1.gerbang[i] < 10) ? " " : "")<<" | ";
}
cout <<endl;
for (int i = Q1.front; i < Q1.rear; i++){
cout<<"+----+ ";
}
cout<< endl;
}
cout << "+======================================+"<<endl;
}
void enqueue(){
int pilihGerbang;
cout<<"----------------------\n"
<<"1. Gerbang Tol 1 \n"
<<"2. Gerbang Tol 2 \n"
<<"----------------------\n";
cout<<"Pilih Gerbang Tol : ";cin>>pilihGerbang;
switch(pilihGerbang){
case 1:
if(isFull(1)){
cout<<"----------------------\n"
<<" Gerbang Tol 1 Penuh \n"
<<"----------------------\n";
} else {
label = label + 1;
Q.gerbang[Q.rear] = label;
Q.rear++;
cout<<"----------------------------------\n"
<<"Mobil telah masuk ke gerbang tol 1\n"
<<"----------------------------------\n";
}
break;
case 2:
if(isFull(2)){
cout<<"----------------------\n"
<<" Gerbang Tol 2 Penuh \n"
<<"----------------------\n";
} else {
label = label + 1;
Q1.gerbang[Q1.rear] = label;
Q1.rear++;
cout<<"----------------------------------\n"
<<"Mobil telah masuk ke gerbang tol 2\n"
<<"----------------------------------\n";
}
break;
default:
cout<<"--------------------------\n"
<<"Gerbang Tol tidak tersedia\n"
<<"--------------------------\n";
break;
}
}
void dequeue(){
int pilihGerbang;
cout<<"----------------------\n"
<<"1. Gerbang Tol 1 \n"
<<"2. Gerbang Tol 2 \n"
<<"----------------------\n";
cout<<"Pilih Gerbang Tol : ";cin>>pilihGerbang;
switch(pilihGerbang){
case 1:
if(isEmpty(1)){
cout<<"------------------------------------\n"
<<" Antrian Gerbang Tol 1 Masih Kosong \n"
<<"------------------------------------\n";
} else {
cout<<"-------------------------------------------------\n"
<<" Mobil Nomor '"<< Q.gerbang[Q.front]<<"' telah selesai dari Gerbang Tol 1\n"
<<"-------------------------------------------------\n";
for(int i = Q.front; i < Q.rear; i++){
Q.gerbang[i] = Q.gerbang[i+1];
}
Q.rear--;
}
break;
case 2:
if(isEmpty(2)){
cout<<"------------------------------------\n"
<<" Antrian Gerbang Tol 2 Masih Kosong \n"
<<"------------------------------------\n";
} else {
cout<<"-------------------------------------------------\n"
<<" Mobil Nomor '"<< Q1.gerbang[Q1.front]<<"' telah selesai dari Gerbang Tol 2\n"
<<"-------------------------------------------------\n";
for(int i = Q1.front; i < Q1.rear; i++){
Q1.gerbang[i] = Q1.gerbang[i+1];
}
Q1.rear--;
}
break;
default:
cout<<"--------------------------\n"
<<"Gerbang Tol tidak tersedia\n"
<<"--------------------------\n";
break;
}
}
int main(){
int choose;
do{
system("cls");
cout<<"----------------------\n"
<<" Menu Pilihan \n"
<<"----------------------\n"
<<" [1] Enqueue \n"
<<" [2] Dequeue \n"
<<" [3] Keluar \n"
<<"----------------------\n\n";
printQueue();
cout<<"Masukkan pilihan : "; cin >> choose;
switch(choose){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
cout<<"Keluar...";
break;
default:
cout<<"----------------------\n"
<<"Pilihan tidak tersedia\n"
<<"----------------------\n";
break;
}
printf("\nPress any key to continue...");
getch();
} while(choose != 3);
return 0;
}
link terkait :
contoh queue menggunakan linked list bahasa c
contoh stack menggunakan linked list bahasa c
contoh stack menggunakan array bahasa c