Skip to content

POSTEST PRAKTIKUM VIII

January 12, 2011

#include <cstdlib>
#include <iostream>
#define maks 5

using namespace std;

class queue{
friend ostream& operator <<(ostream&, const queue&);

public :
queue();
int penuh(int);
int kosong(int);
void cetak ();
void enqueue(char);
char dequeue();
private:
char A[maks];
int banyak;

};

ostream& operator <<(ostream& out, const queue& s){
cout<<“\nIsi Queue : “;
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<“”;
}

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 : “<<endl;
for(int i=0;i<banyak;i++){
cout<<A[i]<<“”;
}
}

void queue:: enqueue(char x){
cout<<“\nElelment  : “<< x <<” masuk antrian “;
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++;
}
}
char queue::dequeue(){

char temp=A[–banyak];
cout<<“\n Dequeue elelment–> “<<temp;
A[banyak]=’0′;
return temp;
}

int main(int argc, char *argv[])
{

queue q;
char a;

for(char c=1; c<4;c++){
cout<<” input jumlah antrean : “;
cin >> a;
q.enqueue(a);
cout<<q;
}
char p=q.dequeue();
q.cetak();
cout<<“\n Cetak pakai overloading : “<<q;

system(“PAUSE”);
return EXIT_SUCCESS;
}

From → struktur data

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: