contoh programnya :
#include
#include
#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 :
int banyak;
char A[maks];
};
ostream& operator << (ostream& out, const Queue& s)
{
cout <<"\nIsi Queue:";
for (int i=0; i
out << s.A[i] << " ";
}
Queue::Queue(){
banyak = 0;
for (int i=0; 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
cout <<<" ";
}
void Queue::enqueue(char x)
{
cout <<"\nElemen:" <
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];
for (int i=banyak; i>=0; i--){
cout <<"\nDequeue elemenke-" <<< " ";
for (int i=banyak; i>=0; i--){
cout<< A[i]<< " ";}
A[i]='0';
}
return temp;
}
main()
{
Queue q;
for (char c='a'; c<'j'; c++)
{
q.enqueue(c);
cout <
}
char p=q.dequeue();
q.cetak();
cout <<"\nCetak pakai overloading:" <
system("PAUSE");
return EXIT_SUCCESS;
}
Tidak ada komentar:
Posting Komentar