#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
Praktikum Struktur Data 6
#include <cstdlib> |
#include <iostream> |
|
|
//template <class T> |
class Node{ |
friend class List; |
friend ostream& operator<<(ostream&, const List&); |
public: |
Node(char& t,Node* p) : info(t), berikut(p){} |
protected: |
char info; |
Node *berikut; |
}; |
|
//template <class T> |
class List{ |
friend ostream& operator<<(ostream&, const List&); |
public: |
List() : kepala(0){} |
~List(); |
void sisip(char t); |
char hapus(char& t); |
char kosong() {return (kepala == 0);} |
void cetak(); |
void sisipTengah(); |
protected: |
Node* kepala; |
Node* nodeBaru(char& t,Node* p) |
{ Node* q = new Node(t,p); return q;} |
}; |
//template <class T> |
ostream& operator<<(ostream& out, const List& k) |
{ |
for (Node* p = k.kepala; p; p=p->berikut) |
out << p->info << "->"; |
out << "*\n"; |
return out; |
} |
//template <class T> |
List::~List() |
{ |
Node* temp; |
for (Node* p = kepala; p;) |
{ |
temp = p; |
p = p->berikut; |
delete temp; |
} |
} |
//template <class T> |
void List::sisip(char t) |
{ |
cout << t <<" "<< "masuk list :"; |
Node* p = nodeBaru(t,kepala); |
kepala = p; |
} |
|
void List::sisipTengah(){ |
char a; |
cout<<"Masukan list :"; |
cin>>a; |
|
|
} |
//template <class T> |
char List::hapus(char& t) |
{ |
if (kosong()) return 0; //penghapusan gagal |
t = kepala->info; |
Node* p = kepala; |
kepala = kepala->berikut; |
delete p; |
return 1; //penghapusan sukses |
} |
//template <class T> |
void List::cetak() |
{ |
for (Node* p = kepala; p; p=p->berikut) |
cout << p->info << "->"; |
cout << "*\n"; |
} |
|
int main() |
{ |
List x; |
char data; |
x.sisip('a'); |
cout << x; |
x.sisip('b'); |
cout << x; |
x.sisip('c'); |
cout << x; |
x.sisip('d'); |
cout << x; |
cout<<endl; |
cout<<"cetak list :"; |
x.cetak(); |
cout<<endl; |
x.sisipTengah(); |
|
cout<<endl; |
for (int i=0; i<2; i++){ |
x.hapus(data); |
cout << data <<" "<< "dihapus dari depan list :"; |
cout << x; |
cout<<endl; |
|
} |
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
Langganan:
Postingan (Atom)