--------------------------
Program UASQueueNonCircular;
uses crt;
const Nmax = 5;
type Queue = record
isi : array[1..Nmax] of integer;
head : integer;
tail : integer;
end;
function QueueKosong (Q : Queue) : boolean;
var
kosong : boolean;
begin
if (Q.head = 0) AND (Q.tail = 0) then
kosong := true
else
kosong := false;
QueueKosong := kosong;
end;
function QueuePenuh (Q : Queue) : boolean;
var
penuh : boolean;
begin
if (Q.head = 1) AND (Q.tail = Nmax) then
penuh := true
else
penuh := false;
QueuePenuh := penuh;
end;
procedure ADD (var Q : Queue; angka : integer);
begin
if not QueuePenuh(Q) then
if QueueKosong(Q) then
begin
Q.head := 1;
Q.tail := 1;
Q.isi[Q.tail] := angka;
end
else
begin
Q.tail := Q.tail+1;
Q.isi[Q.tail] := angka;
end;
end;
procedure REMOVE (var Q : Queue; var x : integer);
var
i : integer;
begin
if not QueueKosong(Q) then
if (Q.head = 1) AND (Q.tail = 1) then
begin
Q.head := 0;
Q.tail := 0;
end
else
begin
x := Q.isi[Q.head];
for i := 1 to Q.tail do
begin
Q.isi[i-1] := Q.isi[i];
end;
Q.tail := Q.tail - 1;
end;
end;
procedure tampil(Q : Queue);
var
i : integer;
begin
for i := 1 to 5 do
begin
write(Q.isi[i], ' ');
end;
writeln;
end;
// Contoh Program Utama
var
Q : Queue;
x,i : integer;
begin
tampil(Q);
ADD(Q,2);
tampil(Q);
ADD(Q,3);
tampil(Q);
ADD(Q,5);
tampil(Q);
ADD(Q,6);
tampil(Q);
ADD(Q,1);
tampil(Q);
REMOVE(Q,x);
writeln('Nilai X : ', x);
writeln('Tail : ', Q.tail);
writeln('Head : ', Q.head);
readln;
end.
Queue Circular
--------------------
Program QueueAntrian;
uses crt;
const Nmax = 100;
type Queue = record
isi : array[1..Nmax] of integer;
head : integer;
tail : integer;
end;
function QueueKosong(Q : Queue) : boolean;
var
kosong : boolean;
begin
if (Q.head = 0) and (Q.tail = 0) then
kosong := true
else
kosong := false;
QueueKosong := kosong;
end;
function QueuePenuh(Q : Queue) : boolean;
var
penuh : boolean;
begin
if Q.tail = Nmax then
penuh := true
else
penuh := false;
QueuePenuh := penuh;
end;
function QueueSatu(var Q :Queue) : boolean;
var
isisatu : boolean;
begin
if (Q.head=1) and (Q.tail=1) then
isisatu := true
else
isisatu := false;
QueueSatu:=isisatu;
end;
procedure ADD(var Q : Queue; x : integer);
begin
if not QueuePenuh(Q) then
if QueueKosong(Q) then
begin
Q.head := 1;
Q.tail := 1;
Q.isi[Q.tail] := x;
end
else
begin
Q.tail := Q.tail + 1;
Q.isi[Q.tail] := x;
end;
end;
Procedure REMOVE(var Q : Queue; var x : integer);
var
i:integer;
begin
if not QueueKosong(Q) then
if QueueSatu(q) then
begin
Q.head :=0;
Q.tail :=0;
end
else
begin
x := Q.isi[Q.head];
{alogoritma geser kiri}
for i:= 2 to Q.tail do
begin
Q.isi[i-1] := Q.isi[i];
end;
Q.tail:=Q.tail-1;
end;
end;
var
Q : Queue;
x,i : integer;
begin
add(Q,2);
add(Q,3);
add(Q,5);
add(Q,1);
add(Q,9);
remove(Q,x);
for i:=1 to Q.tail do
begin
writeln(Q.isi[i]);
end;
begin;
writeln('Nilai x adalah : ',x);
writeln('Nilai tail adalah : ', Q.tail);
writeln('Nilai head adalah : ', Q.head);
end;
readln;
end.
Related Post :