Queue Uts
#include<iostream>#include <cstdlib>
#include<conio.h>
#define max 10 //THAT define the data there are 10 data max
using namespace std;
struct Queue //struct for queue for all data types in it integer
{
int data[max];
int head;
int tail;
};
Queue antrian; //stack queue with the queue name
void create()
{
antrian.head=antrian.tail=-1;//for indexing
}
int IsEmpty()
{
if(antrian.tail==-1)
return 1;// If empty data
else
return 0;// have a data
}
int IsFull()
{
if(antrian.tail==max-1)//when the tail together with the data that is to max 9
return 1;
else
return 0;
}
void Enqueue(int data)
{
if(IsEmpty()==1)//for logic data are still preliminary , not so the data to 0
{
antrian.head=antrian.tail=0;
antrian.data[antrian.tail]=data;
cout<<"Data "<<antrian.data[antrian.tail]<<"Masuk !!!";
}
else if(IsFull()==0)
{
antrian.tail++;
antrian.data[antrian.tail]=data;
cout<<"Data "<<antrian.data[antrian.tail]<<"Masuk !!!";
}
else if(IsFull()==1)
{
cout<<"Ruangan Penuh !!"<<endl;
cout<<data<<"Ga Bisa Masuk !!!";
}
}
void Dequeue()
{
int i;
int e = antrian.data[antrian.head];//to hold the value of the first data
if(antrian.tail==-1)
{
cout<<"Ga Ada antrian... Data Kosong"<<endl;
}
else
{
for(i=antrian.head;i<antrian.tail;i++)
{
antrian.data[i]=antrian.data[i+1];//to overwrite the previous value of the tail
}
antrian.data[antrian.tail]=NULL;//to remove data
antrian.tail--;
cout<<"Data yang keluar lebih dulu = "<<e<<endl;
}
}
void clear()
{
for(int i=antrian.head;i<=antrian.tail;i++)
{
antrian.data[i]=NULL;
}
antrian.head=antrian.tail=-1;
cout<<"Data Clear";
}
void tampil()
{
if(IsEmpty()==0)
{
cout<<"Data Dalam Antrian"<<endl;
cout<<"=====================================";
cout<<endl;
for(int i=antrian.head;i<=antrian.tail;i++)
{
cout<<"| " <<antrian.data[i]<<" |";
}
}
else
{
cout<<"Ga ada antrian... Data kosong";
}
}
int main()
{
int pil;
int data;
create();
do
{
system("cls");
cout<<"Implementasi Antrian dengan Struct"<<endl;
cout<<"==========================================";
cout<<endl;
cout<<"1. Enqueue"<<endl;
cout<<"2. Dqueue "<<endl;
cout<<"3. Print "<<endl;
cout<<"4. Clear "<<endl;
cout<<"5. Exit "<<endl;
cout<<"Masukkan pilihan anda : ";
cin>>pil;
switch(pil)
{
case 1:
{
cout<<endl;
cout<<"Data = ";
cin>>data;
Enqueue(data);
break;
}
case 2:
{
cout<<endl;
Dequeue();
break;
}
case 3:
{
cout<<endl;
tampil();
break;
}
case 4:
{
cout<<endl;
clear();
break;
}
}
getch();
}
while(pil!=5);
}
0 komentar:
Posting Komentar