Home » » UTS Queue

UTS Queue

using namespace std;
struct Queue //create a new data type
{
        int data[max];
   int head;
   int tail;
};
Queue antrian; //create a new var with queue data type
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) //If true, this function will create the first data
   {
      antrian.head=antrian.tail=0;
      antrian.data[antrian.tail]=data;
      cout<<"Data "<<antrian.data[antrian.tail]<<"Masuk !!!";
   }
   else if(IsFull()==0) //If true, this function will be input the data after the first data
   {
    antrian.tail++;
      antrian.data[antrian.tail]=data;
      cout<<"Data "<<antrian.data[antrian.tail]<<"Masuk !!!";
   }
   else if(IsFull()==1) //If True, this function warn the user that the space of data is full
   {
    cout<<"Ruangan Penuh !!"<<endl;
      cout<<data<<"Ga Bisa Masuk !!!";
   }
}
void Dequeue()
{
 int i;
   int e = antrian.data[antrian.head];
   if(antrian.tail==-1)
   {
    cout<<"Ga Ada antrian... Data Kosong"<<endl;
   }
   else // to remove the first data that has been input.
   {
    for(i=antrian.head;i<antrian.tail-1;i++)
      {
       antrian.data[i]=antrian.data[i+1];
      }
      antrian.tail--;
      cout<<"Data yang keluar lebih dulu = "<<e<<endl;
   }
}
void clear() //to clear all of the data
{
 antrian.head=antrian.tail=-1;
  
   cout<<"Data Clear";
}
void tampil() //to display the data
{
 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: