nil NULL
info(P) (P)->info
next(P) (P)->next
firstQueue(Q) ((Q).firstQueue)
lastQueue(Q) ((Q).lastQueue)
} Queue;
bool queueEmpty(Queue Q);
void CreateQueue(Queue *Q);
void BacaQueue(infotype *x);
void TulisQueue(Queue Q);
address Alokasi(infotype x);
void Dealokasi(address P);
void Enqueue(Queue *Q,infotype x);
void Dequeue(Queue *Q);
bool queueEmpty(Queue Q)
{
return ((firstQueue(Q) == nil)&&(lastQueue(Q) == nil));
}
void CreateQueue(Queue *Q)
{
firstQueue(*Q)=nil;
lastQueue(*Q)=nil;
}
void BacaQueue(infotype *x)
{
printf(\n\nMasukkan data: );scanf(%d,&(*x));
}
void TulisQueue(Queue Q)
{
address P;
P=firstQueue(Q);
printf(Data Queue : );
while(P!=nil)
{
printf(%d ,info(P));
P=next(P);
}
}
address Alokasi(infotype x)
{
address P;
P=(address)malloc(sizeof(ElmtQueue));
if (P!=nil)
{
info(P)=x;
next(P)=nil;
}
return P;
}
void Dealokasi(address P)
{
free(P);
}
void Enqueue(Queue *Q,infotype x)
{
address P;
P=Alokasi(x);
if(queueEmpty(*Q))
{
firstQueue(*Q)=P;
lastQueue(*Q)=P;
}
else
{
next(lastQueue(*Q))=P;
lastQueue(*Q)=P;
}
printf(angka %d dimasukkan (enqueue)\n,x);
}
void Dequeue(Queue *Q)
{
address P;
P=firstQueue(*Q);
if(!queueEmpty(*Q))
{
if(P==lastQueue(*Q))
{
Dealokasi(P);
firstQueue(*Q)=nil;
lastQueue(*Q)=nil;
}
else
{
firstQueue(*Q)=next(P);
Dealokasi(P);
}
}
printf(\nangka pertama dlm queue terhapus (dequeue)\n\n);
}
#endif
end
file:testqueue.cpp
begin-
#include queue.h
#include stdio.h
#include iostream.h
main(){
Queue Q;
infotype x;
CreateQueue(&Q);
BacaQueue(&x);
Enqueue(&Q,x);
TulisQueue(Q);
BacaQueue(&x);
Enqueue(&Q,x);
TulisQueue(Q);
BacaQueue(&x);
Enqueue(&Q,x);
TulisQueue(Q);
BacaQueue(&x);
Enqueue(&Q,x);
TulisQueue(Q);
Dequeue(&Q);
TulisQueue(Q);