Our Blog

Queue with Comment

QUEUE MENU

Source Code Queue Menu

struct Queue //struct with named queue and all data in struct have data type int
{
    int data[max];
   int head;
   int tail;
};

Queue antrian; //queue stack's name

void create()
{
 antrian.head=antrian.tail=-1;//for indexing
}

int IsEmpty()//function IsEmpty
{
 if(antrian.tail==-1)
   return 1;// if data is empty
   else
   return 0;// if data have value
}

int IsFull()//function IsFull
{
 if(antrian.tail==max-1)//if queue.tail same 9
   return 1;
   else
   return 0;
}

void Enqueue(int data)//enqueue is used for adding data
{
 if(IsEmpty()==1)//this is for the data logic that beginning,not become the 0 indexing of data
   {
      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 contain 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 value from tail before
      }
      antrian.data[antrian.tail]=NULL;//to erase 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 print()//function that can used for show the data or to print 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()//main function
{
 int pil;//declaration variable pil and data
   int data;
   create();
   do//looping do-while
   {
    system("cls");//to erase or clean screen before
      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);//to called enqueue function
            break;
         }
         case 2:
         {
          cout<<endl;
            Dequeue();//to called dequeue function
            break;
         }
         case 3:
         {
          cout<<endl;
            print();//to called print function
            break;
         }
         case 4:
         {
          cout<<endl;
            clear();
            break;
         }
      }
      getch();
   }
   while(pil!=5);
}

No comments:

Post a Comment

All about Math Designed by Templateism | MyBloggerLab Copyright © 2014

Theme images by richcano. Powered by Blogger.