Praktikum S.Data ke-9


PRIORITY QUEUE
Tujuan Praktikum :
a. Mengamati struktur data priority queue
b. Mempelajari perbedaan queue dengan queue tanpa prioritas..

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

using namespace std;
class node{
public:
char data;
node *next;
};

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;
node *first;
node *last;
};

ostream& operator<<(ostream& out,const queue& s){
if(s.banyak==0){cout<<"\nQueue Kosong,!";}else{
cout<<"\nIsi Queue sebanyak "<<s.banyak<<", yaitu: ";
for(int i=0;i<s.banyak;i++)
out<<s.a[i];
}
}

queue::queue(){
first=NULL;
last=NULL;
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]<data = data;
last = first;
}else{
last->next = new node();
last->next->data = data;
last = last->next;
}
cout<<"\nElemen: "<<data<<" masuk antrian";
if(penuh(banyak))cout<<"\nMaaf,elemen "<<data<data)tempat++;
if(banyak!=tempat)
for(int i=banyak;i>=tempat;i–)
a[i+1]=a[i];
a[tempat]=data;
banyak++;
}
}

char queue::dequeue(){
char reval = NULL;
node *point;
point = first;
if(first!=NULL){
first = first->next;
reval = point->data;
delete point;
}

char temp=a[–banyak];
cout< “<<temp;
a[banyak]='0';
return temp;
}

int main(int argc, char *argv[])
{
queue p;
p.enqueue('b');
cout<<p;
p.enqueue('d');
cout<<p;
p.enqueue('c');
cout<<p;
p.enqueue('e');
cout<<p;
p.enqueue('a');
cout<<p;
p.enqueue('f');
cout<<p;
char x=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<x<<endl;
cout<<p;
char y=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<y<<endl;
cout<<p;
char z=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<z<<endl;
cout<<p;
char x1=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<x1<<endl;
cout<<p;
char y1=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<y1<<endl;
cout<<p;

cout<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

About Sintata
Seek and Share Knowledge

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: