MODULE-2
Data Structes
Structuring or organizing the data in a specififc
manner so that the different operations on data can
be performed easily.
For Example:
If the student details in the college are not arranged
or organized then getting the details of a student
takes lot of time.
If the students details are organized according to
branch and year we get the details of a student
immediately.
Types of Data Structes
Primitive data structures: They only store the data
no facility to organized. Example: int, char, float
Non-Primitive data structures: These are again
classified into two
i. Linear data structures: They organize the data in a
linear manner. Ex: Linked List, Stack and Queue
ii. Non-Linear data Structures: They don’t organize the
data in linear manner. Ex: Trees and Graphs.
Stacks
A stack is a linear data structure that only allows items
to be inserted and removed at one end
We call this end the top of the stack
The other end is called the bottom
Access to other items in the stack is not allowed
A LIFO (Last In First Out) data structure i.e the last
inserted element is deleted first. PUSH POP
S[top]
S[3]
S[2]
S[1]
S[0]
Using a Stack
What Are Stacks Used For?
Most programming languages use a “call stack” to
implement function calling
When a method is called, its line number and other useful
information are pushed (inserted) on the call stack
When a method ends, it is popped (removed) from the call
stack and execution restarts at the indicated line number in
the method that is now at the top of the stack
Stack Implementation
The stack ADT can be implemented using
a variety of data structures. We will look
at two:
Arrays / static representation
Linked Lists /dynamic reprsentation
Basic Conditions: Stack
top point to the top of the stack
When stack is empty top=-1
Stack Operations
A stack should implement (at least) these
operations:
Creation of stack
push – insert an item at the top of the stack
pop – remove and return the top item
display– display the elements of the stack
Creation of Stack
int a[SIZE];
4
int top=-1; 3
top=-1
Algorithm for PUSH() operation in Stack using Array:
Step 1: Start
Step 2: Declare a[SIZE]; //Maximum size of Stack
Step 3: Check if the stack is full or not by comparing top with
(SIZE-1) If the stack is not full, Increment top by 1 and Set,
a[top] = item which pushes the element item into the address
pointed by top. // The element item is stored in a[top]
Step 4: Else, Then print "Stack Overflow" i.e, stack is full and
cannot be pushed with another element
Step 5: Stop
Push In push operation top is incremented by 1.
void push(int item)
{ 4
if(top!=SIZE-1)
{ 3
top++;
a[top]=item; 2
}
else 1
Printf(“\nStack Overflow“);
} 0
top=-1
Push In push operation top is incremented by 1.
if(top!=SIZE-1)
{ 4
top=-1
Push In push operation top is incremented by 1.
if(top!=SIZE-1)
{ 4
top++;
3
top=0 0
Push In push operation top is incremented by 1.
if(top==SIZE-1)
{ 4
top++;
a[top]=item; 3
}
2
top=0 0 100
Push In push operation top is incremented by 1.
Push(200)
4
top=1 1 200
0 100
Push In push operation top is incremented by 1.
Push(300)
4
top=2 2 300
1 200
0 100
Push In push operation top is incremented by 1.
Push(400)
4
top=3 3 400
2 300
1 200
0 100
Push In push operation top is incremented by 1.
Push(500)
top=4 4 500
3 400
2 300
1 200
0 100
Push In push operation top is incremented by 1.
top=5
Push(600)
4 500
3 400
STACK OVERFLOW
2 300
1 200
0 100
Algorithm for POP() operation in
Stack using Array
Step 1: Start
Step 2: Declare a[SIZE]
Step 3: Push the elements into the stack
Step 4: Check if the stack is empty or not by comparing top
with base of array i.e 0 If top is less than 0, then stack is
empty, print "Stack Underflow"
Step 5: Else, If top is greater than zero the stack is not empty,
then store the value pointed by top in a variable x=a[top] and
decrement top by 1. The popped element is x.
Pop In pop operation top is decremented by 1.
void pop( )
{ top=4 4 500
if(top==-1)
printf("\nStack Underflow“); 3 400
else
{ 2 300
printf("\nDeletedElement is %d”,a[top]);
top--; 1 200
}
} 0 100
Pop In pop operation top is decremented by 1.
else
{ 4
printf("\nDeletedElement is %d”,a[top]);
3 400
top--; top=3
}
2 300
1 200
0 100
Pop In pop operation top is decremented by 1.
Pop()
4
top=2 2 300
1 200
0 100
Algorithm for display() operation
in Stack using Arrays
Step 1: Start
Step 2:Check stack top is -1 then print stack is empty.
then stack is empty, print "Stack empty”.
Step 3: Else, Print the values stored in the stack pointed
by top one by one till top=0.
Step 4: Stop
Display
void display()
{ 4
if(top==-1)
printf(”Stack is Empty”); 3
else
for(int i=top;i>=0;i--) top=2 2 300
printf(“%d”,a[i]);
} 1 200
0 100
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
i=2 top=2 2 300
1 200
0 100
300
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
top=2 2 300
i=1 1 200
0 100
300 200
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
top=2 2 300
1 200
i=0 0 100
300 200 100
Applications of Stack
1. Infix to postfix conversion
2. Expression evaluation
3. Balancing parenthesis
4. Function calls
5. Towers of Hanoi
6. Back Tracking
Infix to Postfix or Prefix
conversion
An infix expression in a programming statement
is converted to either postfix or prefix
expression by the compiler. As evaluating a
postfix or prefix occupies less memory space.
For example
A+B is infix expression
AB+ is postfix expression
+AB is prefix expression
Infix to postfix conversion
Algorithm:
1. Scan the infix string by each character from left to right.
2. if the scanned character is an operand add it to the output
string(postfix string)
3. else if the scanned character is opening brace “(“ push it on
to the stack.
4. else if the scanned character is closing brace “)“ pop the
stack and add to post fix string until opening brace “(“on stack
5. else if the scanned character is an operator and the stack is
empty push the character to stack.
Algorithm:
6. else if if the scanned character is an operator and the
stack is not empty,
If top of the stack has high precedence or equal
over the scanned character pop the stack
else push the scanned character to stack. Repeat this
step as long as stack is not empty and topstack has
precedence over the character.
7. After all characters are scanned, If stack is not empty
add topstack to postfix string and pop the stack. Repeat
this step as long as stack is not empty.
Operator Precedence
Example
1. a+b*c
2. a*(b+c)*d
3. a+b*c-d
4. a+b*(c-d)
5. (a+b-c)*d-(e+f)
Example
1. a+b*c //abc*+
2. a*(b+c)*d //abc+*d*
3. a+b*c-d //abc*+d-
4. a+b*(c-d) //abcd-*+
5. (a+b-c)*d-(e+f) //ab+c-d*ef+-
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. Stack top +
character is + which is having less
priority than scanned character *. So
push to stack.
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. Stack top +
character is + which is having less
priority than scanned character *. So
push to stack.
5 c Its an operand so add to postfix * abc
+
Convert the infix expression a+b*c-d into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. Stack top +
character is + which is having less
priority than scanned character *. So
push to stack.
5 c Its an operand so add to postfix * abc
+
6 - Its an operator and stack is not empty. *
So check the precedence. +
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. - abc*+
So check the precedence.
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. - abc*+
So check the precedence.
8 d Its an operand so add to postfix - abc*+d
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. - abc*+
So check the precedence.
8 d Its an operand so add to postfix - abc*+d
9 Stack not empty. So pop the satck and empty abc*+d-
add to post fix until stack is empty
10 No cheaters to scan and stack is empty abc*+d-
empty. So we stop.
abc*+d-
Convert the infix expression a+b*(c-d) into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
Convert the infix expression a+b*(c-d) into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
Convert the infix expression a+b*(c-d) into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
Convert the infix expression a+b*(c-d) into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. +
Convert the infix expression a+b*(c-d) into postfix
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. +
5 ( Its an opening brace, so push it on to ( ab
stack *
+
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. +
5 ( Its an opening brace, so push it on to ( ab
stack *
+
6 c Its an operand so add to postfix ( abc
*
+
Step Scanned Remarks Stack Postfix
character Contents string
1 a Its an operand so add to postfix empty a
2 + Its an operator and stack is empty so + a
push the character on to stack
3 b Its an operand so add to postfix + ab
4 * Its an operator and stack is not empty. * ab
So check the precedence. +
5 ( Its an opening brace, so push it on to ( ab
stack *
+
6 c Its an operand so add to postfix ( abc
*
+
7 - Its an operator and stack is not empty. -
So check the precedence. (
*
+
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. -
So check the precedence. (
*
+
8 d Its an operand so add to postfix - abcd
(
*
+
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. -
So check the precedence. (
*
+
8 d Its an operand so add to postfix - abcd
(
*
+
9 ) Its closing brace, so pop until opening * abcd-
brace add to postfix +
Step Scanned Remarks Stack Postfix
character Contents string
7 - Its an operator and stack is not empty. -
So check the precedence. (
*
+
8 d Its an operand so add to postfix - abcd
(
*
+
9 ) Its closing brace, so pop until opening * abcd-
brace add to postfix +
10 Stack not empty abcd-*+
11 No characters to scan and stack is
empty
abcd-*+
Convert into Postfix
1. (a/b)/(d%e)-f*g
2. (A+B)*(C-D)^(E*F)
3. ((A+B)+C*D)/((E+F)-G)
4. A+B*C+(D*E+F)*G
Convert into Postfix
1. (a/b)/(d%e)-f*g = ab/de%/fg*-
2. (A+B)*(C-D)^(E*F)=AB+CD-EF*^*
3. ((A+B)+C*D)/((E+F)-G)=AB+CD*+EF+G-/
4. A+B*C+(D*E+F)*G=ABC*+DE*F+G*+
Expression evaluation
Algorithm
1. Use a stack to evaluate an expression in postfix
notation.
2. The postfix expression to be evaluated is scanned
from left to right.
3. Variables or constants are pushed onto the stack.
4. When an operator is encountered, the indicated action
is performed using the top elements of the stack, and
the result replaces the operands on the stack.
1.) 423*+1-
2.)( a + b - c ) * d – ( e + f )
a b + c – d * e f + -
A=b=c=d=e=f=1
3.)62/3-41*+
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
3 3 Its an operand so push to stack 3
2
4
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
3 3 Its an operand so push to stack 3
2
4
4 * Its an operator, so apply on top two 3*2 6
elements of stack and store the result 4
in stack
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
3 3 Its an operand so push to stack 3
2
4
4 * Its an operator, so apply on top two 3*2 6
elements of stack and store the result 4
in stack
5 + Its an operator, so apply on top two 6+4 10
elements of stack and store the result
in stack
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
3 3 Its an operand so push to stack 3
2
4
4 * Its an operator, so apply on top two 3*2 6
elements of stack and store the result 4
in stack
5 + Its an operator, so apply on top two 6+4 10
elements of stack and store the result
in stack
6 1 Its an operand so push to stack 1
Step Scanned Remarks Operation Stack
character Content
s
1 4 Its an operand so push to stack 4
2 2 Its an operand so push to stack 2
4
3 3 Its an operand so push to stack 3
2
4
4 * Its an operator, so apply on top two 3*2 6
elements of stack and store the result 4
in stack
5 + Its an operator, so apply on top two 6+4 10
elements of stack and store the result
in stack
6 1 Its an operand so push to stack 1
10
7 - Its an operator, so apply on top two 10-1 9
elements of stack and store the result
in stack