Praktikum S.Data ke-7


STACK DENGAN ARRAY

Tujuan Praktikum
a. Mengamati penggunaan array untuk mengimplementasikan struktur data stack
b. Mempelajari operasi-operasi pada stack

#include<cstdlib>
#include<iostream>
#define maks 5

using namespace std;
class stack{
friend ostream& operator<<(ostream&, const stack&);
public:
stack();
int penuh(int);
int kosong(int);
void cetak();
void push(char);
char pop();
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<=0;i–)
a[i+1]=a[i];
a[0]=x;
banyak++;
}
}

char stack::pop(){
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;
for(char c='a';c<'d';c++){
Stack.push(c);
Stack.cetak();
}
char p=Stack.pop();
Stack.cetak();
cout<<"\n\nCetak pakai overloading "<<Stack;

cout<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

About Sintata
Seek and Share Knowledge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: