#include <stdio.h>
#include <conio.h>
typedef struct
{
int data [6];
int kepala;
int ekor;
}
Queue;
Queue antrian;
void Create()
{
antrian.kepala=antrian.ekor=-1;
}
int kosong()
{
if(antrian.ekor==-1)
return 1;
else
return 0;
}
int penuh()
{
if(antrian.ekor==6-1)
return 1;
else
return 0;
}
void Enqueue(int data)
{
if(kosong()==1)
{
antrian.kepala=antrian.ekor=0;
antrian.data[antrian.ekor]=data;
void Tampil();
{
if(kosong()==0)
{
for(int i=antrian.kepala;i<=antrian.ekor;i++)
{
cout<<antrian.data[i];
}
}
else
cout<<"Data Kosong !\n";
}
}
else
if(penuh()==0)
{
antrian.ekor++;
antrian.data[antrian.ekor]=data;
}
}
int Dequeue()
{
int i;
int e=antrian.data[antrian.kepala];
for(i=antrian.kepala;i<=antrian.ekor-1;i++)
{
antrian.data[i]=antrian.data[i+1];
}
antrian.ekor--;
return e;
}
void hancur()
{
antrian.kepala=antrian.ekor=-1;
cout<<"Data sudah dibongkar";
}
void Tampil()
{
if (kosong()==0)
{
for (int i=antrian.kepala;i<=antrian.ekor; i++)
{ cout<<antrian.data[i]<<" ";
}
}
else
{
cout<<"Data Kosong\n";
}
}
void main()
{
int pil;
int data;
Create();
do
{
clrscr();
printf ("\n============MENU PILIHAN============\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Tampil\n");
printf ("4. Destroy\n");
printf ("5. Keluar\n");
printf ("--------------------------------------\n");
printf ("Masukkan Pilihan Anda -> ");
cin>>pil;
switch(pil){
case 1:
cout<<"Data : ";
cin>>data;
Enqueue(data);
break;
case 2:
cout<<"Elemen yang keluar : "<< Dequeue();
break;
case 3:
Tampil();
break;
case 4:
hancur();
break;
case 5:
clrscr();
break;
}
getch();
} while(pil!=5);
}
0 komentar:
Posting Komentar