Contoh Program Queue (antrian)





code nya kira kira seperti ini:


#include <stdio.h>
#include <conio.h>
void insert(int);
int delet(int);
void display(void);
int queue[5];
int rear=-1;
int front=-1;

void main(){
int n=5;
char op;
clrscr();
do
{
printf("\n\nPilihan");
printf("\n1. Tambah Data");
printf("\n2. Hapus Data");
printf("\n3. Tampilkan Data");
printf("\nTekan e untuk keluar");
printf("\nPilihan Anda : ");

op=getche();
switch(op){
case '1' : insert(n);break;
case '2' : delet(n);break;
case '3' : display();break;
default : printf("\nWrong Operator");
}
}
while(op!='e');
getch();
}

void insert(int n){
int item;
if((front==0&&rear==n)||(front==rear+1)){
printf("\nQueue Over Flow");
return;
}
if(front==-1){
front=0;
rear=0;
}
else if(rear==n)
rear=0;
else
rear=rear+1;

printf("\nMasukkan Data : ");
scanf("%d",&item);
queue[rear]=item;
}

int delet(int n){
int item;
if(front==-1){
printf("\nQueue Is Empty");
}
printf("\nKamu menghapus : %d", queue[front]);
queue[front]=0;
if(front==rear){
front=-1;
rear=-1;
}
else if(front==n)
front=0;
else
front=front+1;
}

void display(void){
int i;
printf("\nTampilkan Queue\n");
for(i=0;i<6;i++)
printf("%d",queue[i]);

}





Comments