Top Menu

 CONTOH PROGRAM QUEUE

 #include <stdio.h>

#include <conio.h>

#define MAX 8

typedef struct{

int data[MAX];

int head;

int tail;

} Queue;

Queue antrian;

void Create(){

antrian.head=antrian.tail=-1;

}

int IsEmpty(){

if(antrian.tail==-1)

return 1;

else

return 0;

}

int IsFull(){

if(antrian.tail==MAX-1) return 1;

else return 0;

}

void Enqueue(int data){

if(IsEmpty()==1){

antrian.head=antrian.tail=0;

antrian.data[antrian.tail]=data;

printf(“%d masuk!”,antrian.data[antrian.tail]);

} else

if(IsFull()==0){

antrian.tail++;

antrian.data[antrian.tail]=data;

printf(“%d masuk!”,antrian.data[antrian.tail]);

}

}

int Dequeue(){

int i;

int e = antrian.data[antrian.head];

for(i=antrian.head;i<=antrian.tail-1;i++){

antrian.data[i] = antrian.data[i+1];

}

antrian.tail–;

return e;

}

void Clear(){

antrian.head=antrian.tail=-1;

printf(“data clear”);

}

void Tampil(){

if(IsEmpty()==0){

for(int i=antrian.head;i<=antrian.tail;i++){

printf(“%d “,antrian.data[i]);

}

}else printf(“data kosong!n”);

}

int main(){

int pil;

int data;

Create();

do{

printf(“1. Enqueuen”);

printf(“2. Dequeuen”);

printf(“3. Tampiln”);

printf(“4. Clearn”);

printf(“5. Exitn”);

printf(“Pilihan = “);scanf(“%d”,&pil);

switch(pil){

case 1: printf(“Data = “);scanf(“%d”,&data);

Enqueue(data);

break;

case 2: printf(“Elemen yang keluar : %d”,Dequeue());

break;

case 3: Tampil();

break;

case 4: Clear();

break;

}

getch();

} while(pil!=5);

}

Outputnya di coba sndiri aja yah….. 

Penulis : Agus Darmawan

About The Author

2 Comments

  1. aku ada tugas kaya gini nich,,,bantuin ngerjain dunkk…

    Buatlah program antrian persewaan VCD,
    dengan ketentuan:
    1. Gunakan record VCD dengan field
    Kode_VCD, Judul, Harga_Sewa
    2. Proses enqueue digunakan untuk menambah
    data antrian, setiap ada orang mengantri
    dimasukkan data vcd apa yang mau ditonton
    (memasukkan kodevcd,judul, dan harga)
    3. Proses dequeue akan mengeluarkan data,
    menampilkan isi record (kodevcd,judul, dan harga)
    serta membuat bertambahnya variabel KAS.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Close