#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 dequeue(); |
private : |
char A[maks]; |
int banyak; |
}; |
|
ostream& operator<<(ostream& out,const Queue& s){ |
cout<<"\nIsi Queue sebanyak :"<<s.banyak<<"yaitu :"; |
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==maks?1:0; |
} |
void Queue::cetak(){ |
cout<<"\nIsi Queue :"; |
for(int i=0;i<banyak;i++) |
cout<<A[i]<<" "; |
} |
void Queue::enqueue(){ |
cout<<"\nElemen :"; |
char x; |
cin>>x; |
cout<<" masuk antrian"; |
if(penuh(banyak))cout<<"queue penuh"; |
else if(A[0]=='0'){ |
A[0]=x; |
banyak++; |
} |
else{ |
int tempat=0; |
while(A[tempat]>x)tempat++; |
if(banyak!=tempat) |
for(int i=banyak;i>=tempat;i--) |
A[i+1]=A[i]; |
A[tempat]=x; |
banyak++; |
} |
} |
char Queue::dequeue(){ |
char temp=A[--banyak]; |
cout<<"\nDequeue elemen-->"<<temp; |
A[banyak]='0'; |
return temp; |
} |
int main(int argc, char *argv[]) |
{ |
Queue p,a; |
for(int i=0;i<maks;i++){cout<<'0';} |
p.enqueue(); |
cout<<p; |
p.enqueue(); |
cout<<p; |
p.enqueue(); |
cout<<p; |
p.enqueue(); |
cout<<p; |
p.enqueue(); |
cout<<p; |
char x; |
p.dequeue(); |
cout<<"elemen yang di-Dequeue"<<x<<endl; |
cout<<p; |
p.dequeue(); |
cout<<"elemen yang di-Dequeue"<<x<<endl; |
cout<<p; |
p.dequeue(); |
cout<<"elemen yang di-Dequeue"<<x<<endl; |
cout<<p; |
p.dequeue(); |
cout<<"elemen yang di-Dequeue"<<x<<endl; |
cout<<p; |
p.dequeue(); |
cout<<"elemen yang di-Dequeue"<<x<<endl; |
cout<<p; |
cout<<endl; |
|
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
Minggu, 23 Januari 2011
Praktikum Struktur Data 9
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar