#include <cstdlib> |
#include <iostream> |
#define maks 10 |
using namespace std; |
class Stack{ |
friend ostream& operator<<(ostream&,const Stack&); |
public : |
Stack(); |
int penuh(int); |
int kosong(int); |
void cetak(); |
void push(char); |
void pop(char); |
private : |
char A[maks]; |
int banyak; |
}; |
ostream& operator<<(ostream& out,const Stack& s){ |
cout<<"\nIsi Stack :"; |
for(int i=0;i<s.banyak;i++) |
out<<s.A[i]<<" "; |
} |
Stack::Stack(){ |
banyak=0; |
for(int i=0;i<maks;i++) |
A[i]='0'; |
} |
int Stack::penuh(int s){ |
return s==maks?1:0; |
} |
int Stack::kosong(int s){ |
return s==0?1:0; |
} |
void Stack::cetak(){ |
cout<<"\nIsi Stack :"; |
for(int i=0;i<banyak;i++) |
cout<<A[i]<<" "; |
} |
void Stack::push(char x){ |
cout<<"\nElemen masuk :"<<x; |
if(penuh(banyak))cout<<"Stack 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++; |
} |
} |
void Stack::pop(char v){ |
cout<<"\nPop stack,elemen yang di-pop :"<<A[0]; |
char temp=A[0]; |
for(int i=0;i<banyak;i++)A[i]=A[i+1]; |
A[banyak]='0'; |
banyak--; |
// return temp; |
} |
int main(int argc, char *argv[]) |
{ |
Stack stack; |
stack.push(']'); |
stack.push('b'); |
stack.push('+'); |
stack.push('a'); |
stack.push('['); |
stack.push('['); |
|
|
cout<<"\nCetak pakai overloading :"<<stack; |
|
for(int i='a';i<='e';i++){stack.pop('i');stack.cetak();} |
cout<<endl; |
cout<<"kelebihan ]"; |
cout<<endl; |
|
|
|
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
Minggu, 12 Desember 2010
Praktikum Struktur Data 7
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar