You are on page 1of 6

3.

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:

 push - add a new item on top of the stack


 pop - remove the top item from the stack
 isEmpty - check if the stack is empty
 size - count the number of items on the stack

3.2 Stack ADT

template <class T>


class Stack {
public:
bool isEmpty() const;
int size() const;
T pop();
void push(const T& x);
T Peek();
...
private:
int top;
};

3.2.1 Algorithm for push()

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.

1. If TOP = SIZE – 1, then:


(a) Display “The stack is in overflow condition”
(b) Exit
2. TOP = TOP + 1
3. STACK [TOP] = ITEM
4. Exit

Vnktrmnb/Data structures/Stacks Page 1


3.2.2 Algorithm for pop

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.

1. If TOP ==-1 then


(a) Display “The Stack is empty”
(b) Exit
2. Else remove the Top most element
3. DATA = STACK[TOP]
4. TOP = TOP – 1
5. Exit

3.3 STACK IMPLEMENTATION

Stack can be implemented in two ways:

1. Static implementation (using arrays)


2. Dynamic implementation (using pointers or LinkedList )

Stack Errors

Two things can go wrong with stacks:

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.

3.4 Algorithm for infix notation to postfix

Step1: Initialize the stack to be empty.

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

Vnktrmnb/Data structures/Stacks Page 2


operator, pop the all the operators which are of higher equal precedence or priority than
the incoming operator and append them (in the same order) to the o/p. After poping up
all operators, push the new operator onto the stack.

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.

3.4.1 Consider the conversion of the infix expression, A*B/(C-D)+E*(F-G), to its


equivalent postfix notation:

Input String Stack Operation Postfix Notation


A Empty A
* * A
B * AB
/ / AB*
( /( AB*
C /( AB*C
- /(- AB*C
D /(- AB*CD
) / AB*CD-
+ + AB*CD-/
E + AB*CD-/E
* +* AB*CD-/E
( +*( AB*CD-/E
F +*( AB*CD-/EF
- +*(- AB*CD-/EF
G +*(- AB*CD-/EFG
) Empty AB*CD-/EFG-*+

3.5 Evaluation of Postfix Expression

Stack are used to evaluate the postfix expression. To evaluate the postfix expression,
consider the following steps:

It the input string is an operand, then push in onto the stack

Vnktrmnb/Data structures/Stacks Page 3


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

3.5.1 Consider the evaluation of postfix expression 68+92-/

2
8 9 9 7
6 6 14 14 14 14 2

3.6 Algorithm for infix notation to prefix

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.

If the input string is operand, append to the output.

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.

Vnktrmnb/Data structures/Stacks Page 4


3.6.1 Consider the conversion of the infix expression, A*B/(C-D)+E*(F-G), to its
equivalent prefix notation. First, to convert the infix expression to the prefix notation,
reverse the given input string as follows )G-F(*E+)D-C(/B*A

Input String Stack Operation Postfix Notation


) )
G ) G
- )- G
F )- GF
( Empty GF-
* * GF-
E * GF-E
+ + GF-E*
) +) GF-E*
D +) GF-E*D
- +)- GF-E*D
C +)- GF-E*DC
( + GF-E*DC-
/ +/ GF-E*DC-
B +/ GF-E*DC-B
* +/* GF-E*DC-BA
A +/* GF-E*DC-BA
Empty GF-E*DC-BA*/+

3.7 Evaluation of Prefix Expression

Stacks are also used to evaluate a prefix expression. To Evaluate a prefix expression,
Consider the following steps:

Reverse the given input string

If the input string is an operand, then push it onto the stack.

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.

Vnktrmnb/Data structures/Stacks Page 5


3.7.1 Consider the evaluation of a prefix expression. +63/*-432. To do this, reverse the
input string as 234-*36 shown in fig.

4 6
3 1 3 3 2
2 2 2 2 2 2 4

3.8 Application of Stacks

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)

Vnktrmnb/Data structures/Stacks Page 6

You might also like