#include <cstdlib>
#include <iostream>
#define maks 6
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)
{out << "\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]='O';
}
}
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 x){
cout << "\nElemen : " << x << " masuk antrian";
if(penuh(banyak)){
cout << "Antrian 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 << "\nDequeue elemen --> " << temp;
A[banyak]='O';
return temp;
}
int main(int argc, char *argv[])
{
Queue q;
char c;
for(int i=0; i<=7; i++){
cout <<endl<<"Masukan Inputan huruf : ";cin>>c;
q.enqueue(c);
cout << q;
if(i==6){
char p=q.dequeue();
q.cetak();
cout << endl;
}
}
//char p=q.dequeue();
//q.cetak();
//q.enqueue(c);
// q.cetak();
cout << endl;
cout << "Cetak menggunakan Overloading : " <<q<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Tidak ada komentar:
Posting Komentar