Skip to content

program enqueu

January 16, 2011

#include <iostream.h>
#include <conio.h>
#define maks 14
#define Que1 6
#define Que2 4

class Queue{
friend ostream& operator<<(ostream&, const Queue&);
public:
Queue();
int penuh(int);
int kosong(int);
void cetak();
char enqueue(char);
char dequeue();
void pecah(Queue &, Queue&);
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out, const Queue& s)
{
out << “\nisi Queue:”;
for (int i=0;i<s.banyak;i++)
out << s.A[i] << “  “;
return out;
}

Queue::Queue()
{
banyak=0;
for (int i=0; i<maks; i++)
A[i]=’0′;
}

int Queue::penuh(int s)
{ return s==maks?1:0;}

int Queue::kosong(int s)
{ return s==0?1:0; }

void Queue::cetak (){
cout<<”\nIsi Queue : “;
for(int i=0;i<banyak;i++)
cout<<A[i]<<” “;
}

char Queue::enqueue(char x)
{
if (penuh(banyak)) cout << “Queue penuh”;
else if (A[0]==’0′){
A[0]=x;
banyak++;
}
else{
for (int i=banyak; i>=0; i–)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
return banyak;
}
char Queue::dequeue()
{
char temp=A[–banyak];
A[banyak]=’0′;
return temp;  }

void Queue::pecah(Queue &q1, Queue &q2){
int i;
char a[Que1],b[Que2];
Queue q3;
cout<<”setelah digabung \n”;
for (i=0;i<Que1;i++){
a[i] = q1.dequeue();
q3.enqueue(a[i]);
}
for (i=0;i<Que2;i++){
b[i] = q2.dequeue();
q3.enqueue(b[i]);
}
cout<<q3<<endl;
}

void main(){
Queue p,q,r;
char z[6] = {87,73,66,79,87,79};
char y[4] = {72,65,68,73};
cout<<”Antrian I :\n”;
for(int i=Que1-1;i>=0;i–){
p.enqueue(z[i]);
cout << p<<endl;
}
cout << “____________________________________”;
cout << endl<<endl;
cout<<”Antrian II : \n”;
for(int j=Que2-1;j>=0;j–){
q.enqueue(y[j]);
cout << q<<endl;
}
cout << “_______________________”;
cout<<endl<<endl;
r.pecah(p,q);
cout<<endl;
getch();
}

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: