Professional Documents
Culture Documents
Stack
3.1 Definition
A stack is an ordered collection of items for which we can only add or remove items
from one end (the top of the stack). The stack is another container class, much like a list,
but with a much more limited set of operations:
Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will
hold the data items. TOP is the pointer that points to the top most element of the stack.
Let DATA is the data item to be pushed.
Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will
hold the data items. TOP is the pointer that points to the top most element of the stack.
DATA is the popped (or deleted) data item from the top of the stack.
Stack Errors
Underflow: an attempt was made to pop an empty stack. Just like with any container
class, it doesn't make sense to remove something from an empty collection. Stack
underflow is a common error - calls to isEmpty should be used to guard against it. Often
times an empty stack is a sign that some part (or all) of a computation is completed..
Overflow: an attempt was made to push an item onto a full stack. Just like with any
container class, we can only add items to a collection if there is enough available
memory to store the collection. Some implementations of stacks specify a particular finite
size. Others may attempt to emulate infinite stacks (of course, we know that everything
in the real world is finite) and only result in stack overflow errors when the computer
runs out of memory.
Step2:For each character in the string, if the input string is operand, then append it to
o/p. if the input string is left parenthesis, push it on to the stack. If the input string is an
Step3: If the input string is right parenthesis, pop operators from the stack and append
the operators to the o/p until a left parenthesis is encountered.
Pop the left parenthesis from the stack and discard it.
Stpe4:If the end of the input string is encountered then iterate the loop, until the stack is
not empty. Pop the stack and append the remaining input string to the o/p.
Stack are used to evaluate the postfix expression. To evaluate the postfix expression,
consider the following steps:
2
8 9 9 7
6 6 14 14 14 14 2
Step1:First, initialize the stack to be empty and reverse the given input string.
Step2:For each character in the input string, if the input string is right parenthesis, push it
on the stack.
Else
If the stack is empty or the operator has right priority than operator on the top of stack.
OR
The top of the stack is right parenthesis then push the operator on the stack
Else
Pop the operator from the stack and append to the output.
Step3: If the input string is left parenthesis, pop operator from the stack and append all
the operators to the output until the right parenthesis is encountered.
Pop the right parenthesis from the stack and discard it.
Step4:If the end of the input string is encountered, then iterate the loop until the stack is
not empty.
Step 5:Pop stack and append the remaining input string to the output and reverse the
output string.
Stacks are also used to evaluate a prefix expression. To Evaluate a prefix expression,
Consider the following steps:
If the input string is an operator, then the first two operands on the stack are evaluated
using this operator by popping them from the stack and the result is also placed onto the
stack.
4 6
3 1 3 3 2
2 2 2 2 2 2 4
The stack data structure is used in a wide range of applications. A few of them are the
following:
1. Converting infix expression to postfix and prefix expressions
2. Evaluating the postfix expression
3. Checking well-formed (nested) parenthesis
4. Reversing a string
5. Processing function calls
6. Parsing (analyse the structure) of computer programs
7. Simulating recursion
8. In computations such as decimal to binary conversion
9. In backtracking algorithms (often used in optimizations and in games)