prak 9
#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]<<" ";
}
void queue::enqueue(char data){
if(first == NULL){
first = new node();
first->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<<" tidak bisa masuk antrian!!\nQueue penuh";
else if(a[0]=='0'){
a[0]=data;
banyak++;
}else{
int tempat=0;
while(a[tempat]>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<<"\n\nDequeue elemen --> "<<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;
No comments:
Post a Comment