Laman

Powered By Blogger

Selasa, 25 Desember 2012

Queue (antrian)

Queue (queue) is a data structure where data is first put the data first be removed. Or it could be called a data structure that uses the mechanism of FIFO (First In First Out).
queue

#include <iostream.h>
#include <conio.h>

#define maks 5

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){
cout<<”\nIsi Queue : “;
for(int i;i<s.banyak;i++)
out<<s.A[i]<<” “;
return out;}

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==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<<”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];
cout<<”\nDequeue elemen –> “<<temp;
A[banyak]=’0′;
return temp;
}

main(){
Queue q;
for(char c=’a';c<’d';c++){
q.enqueue(c);
}
q.cetak();
char p=q.dequeue();
q.cetak();
cout<<”\n\nCetak pakai overloading “<<q;
getch();
return 0;

}

Tidak ada komentar:

Posting Komentar