Queue, mirip sekali penggunaanya dengan Stack, namun metode yang digunakan pada Queue adalah metode FIFO yakni First in, First out. Dengan kata lain data yang pertama kali dimasukan ke dalam queue, akan dikeluarkan juga terlebih dahulu dibandingkan data lainya. Penggunaan queue pada data dengan tipe data array sangat bermanfaat sekali, queue dapat diaplikasikan pada berbagai macam studi kasus, misalkan studi kasus antrian pembelian tiket, antrian di kasir, dan sebagainya. Berikut contoh penggunaan queue pada data dengan tipe data array :

program aplikasi_queue;
uses crt;
const
max_queue = 5;
type
array_queue = array[1..max_queue] of integer;
var
queue:array_queue;
front, rear, i, j, k, l, m, input_data, get_data:integer;
ulang_pop, ulang:char;
procedure queue_initialization(var front, rear:integer);
begin
front := 0;
rear := 0;
end;
function empty_queue(rear:integer):boolean;
begin
empty_queue := rear = 0;
end;
function full_queue(rear:integer):boolean;
begin
full_queue := rear = max_queue;
end;
procedure enqueue(var front, rear:integer; var input_data:integer);
begin
if (not full_queue(rear)) then
begin
front := 1;
rear := rear + 1;
queue[rear] := input_data;
end
else
begin
write(‘<QUEUE PENUH>’);
end;
end;
procedure dequeue(var front, rear:integer; var get_data:integer);
var
i:integer;
begin
if (not empty_queue(rear)) then
begin
get_data := queue[front];
for i := 1 to rear do
begin
queue[i] := queue[i + 1];
end;
rear := rear – 1;
end
else
begin
write(‘<QUEUE KOSONG>’);
end;
end;
procedure show_queue(rear:integer; l:integer; var m:integer);
var
i:integer;
begin
if (not empty_queue(rear)) then
begin
for i := 1 to rear do
begin
gotoxy(39,16+j+k+l+m);write(queue[i]);
m := m + 1;
end;
end
else
begin
write(‘<QUEUE KOSONG>’);
end;
end;
begin
repeat
queue_initialization(front, rear);
i := 0;
j := 0;
k := 0;
l := 0;
m := 0;
textcolor(15);gotoxy(23,2);write(‘QUEUE LINEAR DATA ARRAY ‘);
textcolor(14);gotoxy(23,3);write(‘~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~’);
textcolor(10);gotoxy(32,5);write(‘DATA AWAL QUEUE’);
textcolor(15);gotoxy(33,7);show_queue(rear, l, m);
textcolor(10);gotoxy(33,10);write(‘ENQUEUE DATA’);
textcolor(15);
for i := 1 to max_queue do
begin
gotoxy(31,12+j);write(‘Masukan Data : ‘);readln(input_data);
enqueue(front, rear, input_data);
j := j + 1;
end;
textcolor(10);gotoxy(30,14+j);write(‘DATA DI DALAM QUEUE’);
textcolor(15);gotoxy(33,16+j);show_queue(rear, l, m);
textcolor(10);gotoxy(33,18+j+m);write(‘DEQUEUE DATA’);
repeat
textcolor(14);gotoxy(28,20+j+k+m);write(‘Dequeue Data [Y/T] : ‘);readln(ulang_pop);
if ((ulang_pop = ‘Y’) or (ulang_pop = ‘y’)) and (not empty_queue(rear)) then
begin
textcolor(15);gotoxy(25,22+j+k+m);dequeue(front, rear, get_data);
k := k + 1;
end;
until (ulang_pop = ‘T’) or (ulang_pop = ‘t’) or (empty_queue(rear));
textcolor(10);gotoxy(31,23+j+k+m);write(‘DATA AKHIR QUEUE’);
textcolor(15);gotoxy(33,25+j+k+l+m);show_queue(rear, 9, m);
textcolor(14);gotoxy(27,27+j+k+l+m);write(‘Ulangi Program [Y/T] : ‘);readln(ulang);
clrscr();
until (ulang = ‘T’) or (ulang = ‘t’);
end.

Output :

queue

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *