Problem Solving With ADT - Stacks
Problem Solving With ADT - Stacks
--Stacks
Definition of Stack
ADT level Abstract Data Type
• A stack is an ordered group
of homogeneous items, in
which the removal and
addition of stack items can
take place only at the top
of the stack
• A limited access data
structure
A stack is a LIFO: last
item in, first one out
Checking for Balanced Braces
Balanced: a+{c*{a+b}*d}+{d+e}
Unbalanced: {asdf{df}{dfa{df}
Pseudocode
char/int stack[size]
bool stackfull();
bool stackempty();
void push(stack, item);
char pop(stack);
char gettopitem(stack);
itemtype stack[MAX];
int tos; //top of stack
};
Push – item on Stack
push (stack, item) item is of type char
if (not stackfull()) stack must not be full
stack[tos] = item add item
tos = tos + 1 increment top-of-stack
else
write(“Stack is full, can’t add item”)
end if
end push
Pop – item of Stack
char pop (stack ) popped item is of type char
if (not stackempty() ) stack must not be empty
tos = tos – 1 decrement the top-of-stack
item = stack[tos] item on top of stack
return item
else
write(“Stack is empty, can’t pop item”)
end if
end pop
Compare Three Implementations
Will do this later.