You are on page 1of 8

Stack ADT(LIFO)

Stack ADT(LIFO)
Towers of Hanoi
9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )


int size;
int top;
int st[5];

myStack(); //Constructor
bool isEmpty();//returns true if stack is empty
bool isFull(); //returns true if stack is full
bool push(int elem);//insert element(elem) in stack
bool pop(); // remove top element of stack
int topElement(); // returns top element of stack


Stack ADT(LIFO)

myStack(){
size = 5;
top = 0;
}

bool isEmpty(){
if(top == 0)
return true;
return false;
}
bool isFull(){
if(top ==size)
return true;
return false;
}
bool push(int a){
if(isFull()){
cout<<Error: Stack Overflow;
return false;
}
st[top] = a;
top++;
return true;
}
bool pop(){
if(isEmpty()){
cout<<Error: Stack Underflow;
return false;
}
top--;
return true;
}
Stack ADT
int topElement(){
if(isEmpty()){
cout<<Error: Stack Empty<<endl;
exit(0);
}
return st[top-1];
}
9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )
9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )

class myStack{
int size;
int top;
int st[5];
public:
myStack(); //Constructor
bool isEmpty();//returns true if stack is empty
bool isFull(); //returns true if stack is full
bool push(int elem);//insert element(elem) in stack
bool pop(); // remove top element of stack
int topElement(); // returns top element of stack
};

Stack ADT(LIFO)

myStack::myStack(){
size = 5;
top = 0;
}

bool myStack::isEmpty(){
if(top == 0)
return true;
return false;
}
bool myStack::isFull(){
if(top ==size)
return true;
return false;
}
bool myStack::push(int a){
if(isFull()){
cout<<Error: Stack Overflow;
return false;
}
st[top] = a;
top++;
return true;
}
bool myStack::pop(){
if(isEmpty()){
cout<<Error: Stack Underflow;
return false;
}
top--;
return true;
}
Stack ADT
int myStack::topElement(){
if(isEmpty()){
cout<<Error: Stack Empty<<endl;
exit(0);
}
return st[top-1];
}
9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )

class myStack{
int size;
int top;
int *st;
public:
myStack();
bool isEmpty();
bool isFull();
bool push(int elem);
bool pop();
int topElement();
};

Stack ADT

myStack::myStack(){
size = 5;
top = -1;
st = new int[size];
}

9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )
9
2
7
4
0
1
2
3
4
top
push ( 5 )
5
push ( 9 ) push ( 2 ) push ( 7 ) push ( 4 ) pop ( )
8
push ( 8 )
Stack ADT
bool isEmpty(){
if(top == -1)
return true;
return false;
}
bool isFull(){
if(top==size-1)
return true;
return false;
}
bool myStack::push(int a){
if(isFull()){
cout<<Error: Stack Overflow;
return false;
}
top++;
st[top] = a;
return true;
}
bool myStack::pop(){
if(isEmpty()){
cout<<Error: Stack Underflow;
return false;
}
top--;
return true;
}
int myStack::topElement(){
if(isEmpty()){
cout<<Error: Stack Empty<<endl;
exit(0);
}
return st[top];
}

You might also like