Skip to content

POSTEST PRAKTIKUM VII

January 12, 2011

#include <cstdlib>
#include <iostream.h>
#define maks 20
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 << “\nElement masuk :” << x<<endl;
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++;}
}

char Stack::pop()
{
cout << “\nPop stack, element 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 x;
x.push(‘)’);
x.push(‘)’);
x.push(‘s’);
x.push(‘/’);
x.push(‘s’);
x.push(‘*’);
x.push(‘t’);
x.push(‘(‘);
x.cetak();
cout<<“\n\nkelebihan kurung tutup”<<endl;
cout<<endl;
char p = x.pop();
cout<<endl;
x.cetak();

/*for (char c=’a’;c<=’d’;c++){
x.push(c);
x.cetak();
}
char p = x.pop();
x.cetak();
cout<< “\nCetak pakai overloading :” << x;
*/

cout<<endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}


From → struktur data

Leave a Comment

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: