Program Memisah Antrian Menjadi 2

Berikut ini adalah program memisahkan antrian menjadi dua. Dengan catatan jumlah antrian yang diinputkan adalah berjumlah genap.

#include <cstdlib>
#include <iostream>

using namespace std;
class queue{
   public:
   		queue();
         //void masuk();
         void enqueue();
         void dequeue();
         void cetak();
   private:
   		int antrian[30];
         int antrian1[15];
         int antrian2[15];
         int temp1[15];
         int temp2[15];
         int data, data2;
	};

   queue::queue(){
   	suit:
      cout<<"Masukkan jumlah antrian : ";
      cin>>data;
      if(data%2==0){
      	for(int i=1;i<=data;i++){
         	cout<<"antrian "<<i<<" : ";
         	cin>>antrian[i];
      		}
      data2 = data/2;
         }
      else goto suit;
   	}

   void queue::dequeue(){
      for(int j=1;j<=data2;j++){
         temp1[j]=antrian[j];
      	}
      for(int k=(data2+1);k<=data;k++){
         temp2[k-data2]=antrian[k];
      	}
   	}

   void queue::enqueue(){
      for(int i=1;i<=data2;i++){
         antrian1[i]=temp1[i];
         antrian2[i]=temp2[i];
      	}
   	}

   void queue::cetak(){
      cout<<"Antrian awal : ";
      for(int i=1;i<=data;i++){
         cout<<antrian[i]<<" ";
      	}
      cout<<"\nAntrian 1 : ";
      for(int i=1;i<=data2;i++){
         cout<<antrian1[i]<<" ";
      	}
      cout<<"\nAntrian 2 : ";
      for(int i=1;i<=data2;i++){
         cout<<antrian2[i]<<" ";
      	}
   	}
int main(int argc, char *argv[])
{
    
    
    
     queue x;
      //koko.masuk();
      x.dequeue();
      x.enqueue();
     x.cetak();    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s