Hindustan Institute of Management & Computer Studies
Data Structures and Analysis of Algorithms (BMC205)
Unit-2
(Stack)
Abstract Data Structure
Concentrates on the essential properties of data, their structure and operations
Not concentrates on the representation and implementation details i.e. concrete
realizations
Advantage
Simplifying the study of the data structure
Maintainability i.e. function details can be changed at any time
The implementer can design the function in such a way that memory space is not
wasted and operations are performed simply & efficiently.
Stack
The stack is a simple, but important example of an abstract data type, consisting of a list
of items and a pointer to the "top" item in the list.
Items can be inserted or removed from the list only at the top, i.e. the list is ordered in the
sequence of entry of items.
Insertions and removals proceed in the "LIFO" last-in-first-out order. So elements may
be popped from the stack only in the reverse order of that in which they were pushed into
the stack.
Three Basic Operations
Push i.e. the term used to insert an element into a stack
Pop i.e. the term used to delete an element from a stack.
Display i.e. traversing the each stack elements once.
Other Operations
Overflow i.e. insert an element into an already full stack depend on the reserve
space in memory
Underflow i.e. delete an element from an empty stack
IsEmpty i.e. True/False, stack is empty or not
IsFull i.e. True/False, stack is full or not
Example
Let there are 6 elements to pushed, in order, into the empty stack
AA, BB, CC, DD, EE, FF
8
1 2 3 4 5 6 7 8 7
AA BB CC DD EE FF 6 FF Top = 6
Top = 6 5 EE
4 DD
3 CC
2 BB
1 AA
Application of Stack
Used to indicate the order of the processing of data when certain steps of the
processing must be postponed until other conditions are fulfilled.
Recursion
Reverse the string
Undo operation in Documentation creation Software
Help computers in unfolding their recursive jobs;
Used in converting an expression to its postfix form;
Used in Graphs to find their traversals;
Helps in non-recursive traversal of binary trees and so on....
Expression:-collection of operators and operands is called expression.
i.e. in A+B A, B are operands and” +” is operator.
There are three types of Expression
1. Infix
2. Postfix
3. Prefix
I) Infix:
In which operator is used between the operand
a+b
It is used in our common mathematical expressions.
II) Postfix:
In which operator is used after the operands.
i.e. ab+
III) Prefix:
In which the operator is used before the operands.
I .e. +ab
We use infix type in our daily life but the computer use the postfix or prefix, because it is
easy for computer to calculate by using postfix or prefix.
Without the conversion into postfix and prefix, it is not possible
Examples:
Consider the precedence of the operator, the precedence is decreasing downwardly.
()
$ or (this symbol is used for power operator)
/, * (both have same precedence)
+, _ (both have same precedence)
if there two operators with same precedence then computer start to solve the expression
from left to right.
Examples of Postfix:
1)
a+b-c
ab+ - c
ab+c-
2)
a+b*c
a + bc* (we consider the ‘bc*’ one term and ’a’ other term and ‘+’ is operator between
them and apply the postfix method.
abc* +
3)
(a+b)/ (e-f*c)
ab+ / (e-f*c)
ab+ / ( e-fc*)
ab+ / efc*-
ab+ efc*-/
4)
(a+b)/ (e$f*c)
ab+ / (ef$ * c)
ab+ / ef$c*
ab+ef$c*/
5)
a+b/d*e$f+g
a+b/d*ef$ +g
a+ bd/ *ef$+g
a+ bd/ef$ *+g
abd/ef$* + + g
abd/ef$*+g+
6)
(a+b)/ (d*e$f+g)
This is exercise for you; its prefix solution will be given at the end.
Example of Prefix:
1)
a+b === +ab
2)
a+b-c
+ab - c
-+abc
3)
a+b/d*e$f+g
a+b/d* $ef + g
a+/bd* $ef + g
a + */bd$ef + g
+a*/bd$ef + g
++a*/bd$efg
4)
(a+b)/ (d*e$f+g)
+ab/(d*$ef + g)
+ab /( *d$f + g)
+ab/ +*d$fg
/+ab+*d$fg
Algorithms
Convert Infix to Postfix
Infix : A+(B*C–(D/E^F)*G)*H
Symbol
STACK Expression P
Scanned
(1) A ( A
(2) + ( + A
(3) ( ( + ( A
(4) B ( + ( A B
(5) * ( + ( * A B
(6) C ( + ( * A B C
(7) - ( + ( - A B C *
(8) ( ( + ( - ( A B C *
(9) D ( + ( - ( A B C * D
(10) / ( + ( - ( / A B C * D
(11) E ( + ( - ( / A B C * D E
(12) ^ ( + ( - ( / A B C * D E
(13) F ( + ( - ( / A B C * D E F
(14) ) ( + ( - A B C * D E F ^ /
(15) * ( + ( - * A B C * D E F ^ /
(16) G ( + ( - * A B C * D E F ^ / G
(17) ) ( + A B C * D E F ^ / G * -
(18) * ( + * A B C * D E F ^ / G * -
(19) H ( + * A B C * D E F ^ / G * - H
(20) ) A B C * D E F ^ / G * - H*+
Note: - Try yourself the conversion from infix to prefix.