Minggu, 31 Oktober 2010

Program Stack

#include <cstdlib>
#include <iostream>
using namespace std;
class Stack{
public:
Stack(){
jum = 5;
last = 0;
}
void push(int data){
if(jum > last){
a[last] = data;
last = last + 1;
}
}
int pop(){
if(last > 0){
int temp = a[last - 1];
a[last - 1] = 0;
last = last – 1;
return temp;
}


}
void print(){
for(int i = 0; i < last; i++){
cout << a[i] << ” “;
}
}
private:
int a[5];
int jum;
int last;
};
int main(int argc, char *argv[])
{
Stack x;
x.push(2);
x.push(4);
x.push(11);
x.push(5);
x.push(7);
x.push(9);
x.push(30);
x.print();
cout<<endl;
x.pop();
x.pop();
x.pop();
x.pop();
//x.pop();
//x.pop();
x.print();
cout<<endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More