01 #include <cstdlib>
02 #include <iostream>
03 #define maks 5
04 using namespace std;
05 class queue{
06 friend ostream& operator<<(ostream&, const queue&);
07 public:
08 queue();
09 int penuh(int);
10 int kosong(int);
11 void cetak();
12 void enqueue(char);
13 char dequeue();
14 private:
15 char A[maks];
16 int banyak;
17 };
18 ostream& operator<<(ostream& out,const queue& s)
19 {
20 cout<<"\nisi queue:";
21 for(int i=0;i<s.banyak;i++)
22 out<<s.A[i]<<"";
23
24 }
25 queue::queue(){
26 banyak=0;
27 for(int i=0;i<maks;i++)
28 A[i]='0';
29
30 }
31 int queue::penuh(int s){
32 return s==maks?1:0;}
33 int queue::kosong(int s){
34 return s==0?1:0;}
35 void queue::cetak(){
36 cout<<"\nIsi queue:";
37 for(int i=0;i<banyak;i++)
38 cout<<A[i]<<"";
39 }
40 void queue::enqueue(char x)
41 {
42 cout<<"\nelemen:"<<x<<"masuk antrian";
43 if(penuh(banyak))cout<<"queue penuh";
44 else if(A[0]=='0'){
45 A[0]=x;
46 banyak++;
47 }
48 else{for(int i=banyak;i>=0;i--)
49 A[i+1]=A[i];
50 A[0]=x;
51 banyak++;
52 }
53 }
54 char queue::dequeue(){
55 char temp=A[--banyak];
56 cout<<"\ndequeue elemen-->"<<temp;
57 A[banyak]='0';
58 return temp;
59 }
60
61 int main(int argc, char *argv[])
62 { queue q;
63 char a;
64
65 for(char c=1;c<7;c++){
66 cout<<"masukan antrian:"<<endl;
67 cin>>a;
68 q.enqueue(a);
69 cout<<q;
70 }
71
72 char p=q.dequeue();
73 q.cetak();
74 cout<<"\n\ncetak pakai overloading:"<<q;
75
76 system("PAUSE");
77 return EXIT_SUCCESS;
78 }
Tidak ada komentar:
Posting Komentar