Stack
Topics to be Covered
❑ Stack as an Abstract Data Type
❑ Representation of Stack Using Sequential Organization
❑ Applications of Stack- Expression Conversion and
Evaluation
❑ Recursion
Fundamentals of Data Structure UNIT-IV 1
Unit-III
Stacks
Fundamentals of Data Structure UNIT-IV 2
Real life Applications of Stack
Fundamentals of Data Structure UNIT-IV 3
Stack
• Stack : Special case of ordered list also called as restricted/controlled
list where insertion and deletion happens at only one end called as top
of stack (homogeneous collection of elements.)
• Elements are added to and removed from the top of the stack (the most
recently added items are at the top of the stack).
• The last element to be added is the first to be removed (LIFO: Last
In, First Out). TOP
• Only access to the stack is the top element
- consider trays in a cafeteria
--to get the bottom tray out, you must first remove all of the elements
above
Fundamentals of Data Structure UNIT-IV 4
Where stack resides in data
structure family?
Fundamentals of Data Structure UNIT-IV 5
Stack ADT
structure STACK (item)
declare CREATE() -> stack
ADD(item, stack) -> stack
DELETE(stack) -> stack
TOP(stack) -> item
ISEMPS(stack) -> boolean;
for all S ∈ stack, i ∈ item let
ISEMPS(CREATE) ::= true
ISEMPS(ADD(i,S)) ::= false
DELETE(CREATE) ::= error
DELETE(ADD(i,S)) ::= S
TOP(CREATE)
TOP(ADD(i,S)) ::= i
end
end STACK
Fundamentals of Data Structure UNIT-IV 6
Fundamentals of Data Structure UNIT-IV 7
Basic Stack Operations
Operations-
⮚ isEmpty()-Checking stack is empty
⮚ IsFull()-Checking stack is full
⮚ push()-Pushing Element on top of stack
⮚ pop() – Popping top element from stack
Fundamentals of Data Structure UNIT-IV 8
Representation of stack
Stack can be represented(implemented) using two data structures
❑ Array (Sequential Organization)
❑Linked List (Linked Organization)
Fundamentals of Data Structure UNIT-IV 9
Representation of Stack using
Sequential Organization
• Allocate an array of some size (pre-defined) 4
- Maximum N elements in stack 3
2
• Index of bottom most element of stack is 0 1 78
0 56 pop
• Increment top when one element is pushed, decrement after
• Index of the most recently added item is given by top TOP
Fundamentals of Data Structure UNIT-IV 10
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=07
3
2
1
Top = -1 Stack Empty
0
Empty Stack
Fundamentals of Data Structure UNIT-IV 11
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=06
3
2
1
Top = 0
0 50
Push (50)
Fundamentals of Data Structure UNIT-IV 12
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=05
3
2
1 25
Top = 1
0 50
Push (25)
Fundamentals of Data Structure UNIT-IV 13
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=04
3
2 78
1 25
Top = 2
0 50
Push (78)
Fundamentals of Data Structure UNIT-IV 14
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=05
3
2 Popped element : 78
1 25
Top = 1
0 50
Pop ()
Fundamentals of Data Structure UNIT-IV 15
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=06
3
2 Popped element : 25
1
Top = 0
0 50
Pop ()
Fundamentals of Data Structure UNIT-IV 16
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=07
3
2
1 Popped element : 50
0
Pop ()
Top = -1
Fundamentals of Data Structure UNIT-IV 17
Stack Operations using Array : Example
Stack
6 Stack Max Size=07
5
4 Stack Empty Locations=03
3 78
2 90
1 45
Top = 03
0 23
Push (23),Push(45),Push(90),Push(78)
Fundamentals of Data Structure UNIT-IV 18
Stack Operations using Array : Example
Stack Full
Stack
6 80 Stack Max Size=07
5 65
4 43 Stack Empty Locations=00
3 78
2 90 Top = 06
1 45
0 23
Push (43),Push(65),Push(80)
Fundamentals of Data Structure UNIT-IV 19
Operations on Stack (Algorithm and Pseudo
Code)
Push (ItemType newItem)
• Function: Adds newItem to the top of the stack.
• Preconditions: Stack has been initialized and is not full.
• Postconditions: newItem is at the top of the stack.
Fundamentals of Data Structure UNIT-IV 20
Operations on Stack (Algorithm and Pseudo
Code)
Pop ()
• Function: Removes top Item from stack and returns it .
• Preconditions: Stack has been initialized and is not empty.
• Postconditions: Top element has been removed from stack
Fundamentals of Data Structure UNIT-IV 21
Operations on Stack(Algorithm and
Pseudo Code)
Pseudo of isFull() function −
isFull()
Algorithm isFull()
Stack overflow {
The condition resulting from trying to if (top ==MAXSIZE-1)
push an element onto a full stack.
return true ;
else
return false ;
}
Fundamentals of Data Structure UNIT-IV 22
Operations on Stack (Algorithm and Pseudo
Code)
Pseudo Code for Push
Algorithm push (stack, item)
{
if ( ! isFull ())
{
top=top+1
stack[top]=item;
}
}
Fundamentals of Data Structure UNIT-IV 23
Operations on Stack (Algorithm and
Pseudo Code)
Pseudo of isEmpty() function −
isEmpty() Algorithm isEmpty( )
Stack underflow (check if stack is {
empty.) if (top = = -1)
The condition resulting from trying to pop return true ;
an empty stack. else
return false ;
}
Fundamentals of Data Structure UNIT-IV 24
Operations on Stack (Algorithm and Pseudo
Code)
Pseudo Code for Pop
Algorithm pop ()
{
if ( ! isEmpty())
{
temp=stack[top];
top=top-1;
return (temp);
}
}
Fundamentals of Data Structure UNIT-IV 25
Applications of Stacks in Computer
Science
❑Process Function Calls
❑Recursive Functions Calls
❑Converting Expressions
❑Evaluating expressions
❑String Reverse
❑ Number Conversion
❑Backtracking
❑Parenthesis Checking
Fundamentals of Data Structure UNIT-IV 26
Applications of Stacks (cont’d)
main()
{
int a=10,b=15; Stack
float avg;
avg = average(a,b,2); Local variable : sum
Pushing add() Call
printf(“%f”,avg); return address of
} add()
float average(int x,y,n) Pushing average() Call
Local variable avg,sum
{ float avg;
int sum=add(x,y); return address of
avg = sum/n; average
return avg;
} Local varibales
int add(int x,int y) Address of Main()
{
int sum=x+y;
return sum;
}
Fundamentals of Data Structure UNIT-IV 27
Applications of Stacks (cont’d)
Fundamentals of Data Structure UNIT-IV 28
Fundamentals of Data Structure UNIT-IV 29
Applications of Stacks (cont’d)
Expression Conversion: There are different types of Expressions
1) Infix expression :- It is the general notation used for representing expressions.
“In this expression the operator is fixed in between the operands”
Ex: a + b*c
2) Postfix fix expression :- (Reverse polish notation)
“In this expression the operator is placed after the operands”.
Ex : abc*+
3) Prefix expression :- (Polish notation)
“In this expression the operators are followed by operands i.e the operators are fixed before the
operands”
Ex : *+abc
All the infix expression will be converted into post fix or prefix notation with the help of
stack in any program.
Fundamentals of Data Structure UNIT-IV 30
Expression Conversion
Why to use PREFIX and POSTFIX notations when we have simple INFIX
notation?
❑ INFIX notations are not as simple as they seem specially while
evaluating them.
❑ To evaluate an infix expression we need to consider Operators’ Priority
and Associative Property
E.g. expression 3+5*4 evaluate to 32 i.e. (3+5)*4 or to 23 i.e. 3+(5*4).
To solve this problem Precedence or Priority of the operators were
defined
Fundamentals of Data Structure UNIT-IV 31
Expression Conversion
(cont’d)
Operator Precedence
❑ Operator precedence governs evaluation order. An operator with higher
precedence is applied before an operator with lower precedence.
Rank Operator
1 ^
2 */%
3 + - (Binary)
Fundamentals of Data Structure UNIT-IV 32
Expression Conversion
(cont’d)
Expression Conversion Forms
We can convert any expression to any other two forms using Stack Data Structure
Fundamentals of Data Structure UNIT-IV 33
Infix to Postfix
Expression Conversion Infix to Postfix Example
Fundamentals of Data Structure UNIT-IV 34
Infix to Postfix
Operator Precedence (In stack and Incoming precedence)
❑ Operator precedence governs evaluation order. An operator with higher
precedence is applied before an operator with lower precedence.
Operator ICP ISP
( 5 0
^ 4 3
*/% 2 2
+ - (Binary) 1 1
Fundamentals of Data Structure UNIT-IV 35
Infix to Postfix : Example 1
🟉Let the incoming the Infix expression be:
A * (B + C) – D / E
Stage 1: Stack is empty and we only have the Infix expression.
Fundamentals of Data Structure UNIT-IV 36
Infix to Postfix : Example 1
Stage 2
🟉The first token is Operand A Operands are Appended to the Output as
it is.
Fundamentals of Data Structure UNIT-IV 37
Infix to Postfix : Example 1
Stage 3
🟉Next token is * Since Stack is empty (top==-1) it is pushed into the
Stack
Fundamentals of Data Structure UNIT-IV 38
Infix to Postfix : Example 1
Stage 4
🟉Next token is ( the precedence of open-parenthesis, when it is to go inside, is
maximum.
🟉But when another operator is to come on the top of ‘(‘ then its precedence is
least.
Fundamentals of Data Structure UNIT-IV 39
Infix to Postfix : Example 1
Stage 5
🟉Next token, B is an operand which will go to the Output expression as it is
Fundamentals of Data Structure UNIT-IV 40
Infix to Postfix : Example 1
Stage 6
🟉Next token, + is operator, We consider the precedence of top element in the
Stack, ‘(‘. The outgoing precedence of open parenthesis is the least (refer point
4. Above). So + gets pushed into the Stack
Fundamentals of Data Structure UNIT-IV 41
Infix to Postfix : Example 1
Stage 7
🟉 Next token, C, is appended to the output
Fundamentals of Data Structure UNIT-IV 42
Infix to Postfix : Example 1
Stage 8
🟉Next token ), means that pop all the elements from Stack and append them
to the output expression till we read an opening parenthesis.
Fundamentals of Data Structure UNIT-IV 43
Infix to Postfix : Example 1
Stage 9
🟉Next token, -, is an operator. The precedence of operator on the top of
Stack ‘*‘ is more than that of Minus. So we pop multiply and append it to
output expression. Then push minus in the Stack.
Fundamentals of Data Structure UNIT-IV 44
Infix to Postfix : Example 1
Stage 10
🟉Next, Operand ‘D‘ gets appended to the output.
Fundamentals of Data Structure UNIT-IV 45
Infix to Postfix : Example 1
Stage 11
🟉Next, we will insert the division operator into the Stack because its
precedence is more than that of minus.
Fundamentals of Data Structure UNIT-IV 46
Infix to Postfix : Example 1
Stage 12
🟉The last token, E, is an operand, so we insert it to the output Expression as it
is.
Fundamentals of Data Structure UNIT-IV 47
Infix to Postfix : Example 1
Stage 13
🟉The input Expression is complete now. So we pop the Stack and Append it to
the Output Expression as we pop it.
Fundamentals of Data Structure UNIT-IV 48
Infix to Postfix : Example 2
infixVect
(a+b-c)*d–(e+f)
postfixVect
Fundamentals of Data Structure UNIT-IV 49
Infix to Postfix : Example 2
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
Fundamentals of Data Structure UNIT-IV 50
Infix to Postfix : Example 2
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
Fundamentals of Data Structure UNIT-IV 51
Infix to Postfix : Example 2
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Fundamentals of Data Structure UNIT-IV 52
Infix to Postfix : Example 2
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Fundamentals of Data Structure UNIT-IV 53
Infix to Postfix : Example 2
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Fundamentals of Data Structure UNIT-IV 54
Infix to Postfix : Example 2
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Fundamentals of Data Structure UNIT-IV 55
Infix to Postfix : Example 2
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Fundamentals of Data Structure UNIT-IV 56
Infix to Postfix : Example 2
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
Fundamentals of Data Structure UNIT-IV 57
Infix to Postfix : Example 2
infixVect
–(e+f)
postfixVect
ab+c-d
Fundamentals of Data Structure UNIT-IV 58
Infix to Postfix : Example 2
infixVect
(e+f)
postfixVect
ab+c–d*
Fundamentals of Data Structure UNIT-IV 59
Infix to Postfix : Example 2
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Fundamentals of Data Structure UNIT-IV 60
Infix to Postfix : Example 2
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Fundamentals of Data Structure UNIT-IV 61
Infix to Postfix : Example 2
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Fundamentals of Data Structure UNIT-IV 62
Infix to Postfix : Example 2
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Fundamentals of Data Structure UNIT-IV 63
Infix to Postfix : Example 2
infixVect
postfixVect
ab+c–d*ef+
Fundamentals of Data Structure UNIT-IV 64
Infix to Postfix : Example 2
infixVect
postfixVect
ab+c–d*ef+-
Fundamentals of Data Structure UNIT-IV 65
Infix to Postfix : Example 3
Expression Conversion Infix to Postfix
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form
Fundamentals of Data Structure UNIT-IV 66
In Stack and Incoming priorities functions
icp(ch) (Infix🡪Postfix)
{
if(ch=='+' || ch=='-')
return 1; isp( ch)
if(ch=='*' || ch=='/') {
return 2; if(ch=='+' || ch=='-')
if(ch=='^') return 1;
if(ch=='*' || ch=='/')
return 4;
return 2;
if(ch=='(')
if(ch=='^')
return 5; return 3;
else else
return 0; return 0;
} }
Fundamentals of Data Structure UNIT-IV 67
Algorithm in_post(inexp[ ])
else //3rd
{ // postexp[ ] has the postfix expression
{ while (stack not empty &&
k=0; i=0;
isp(stk[top]) >= icp(tkn) )
tkn=inexp[i];
{ postexp[k]=pop(); k++;
while ( tkn!=‘\0’)
{ if tkn is an operand }
{ postexp[k]=inexp[i]; push(tkn);
k++; } // end of 3rd else
} }//end of 2nd else
else //1st }// end of 1st else
{ if tkn==‘(‘ //open paranthesis
{ push(‘(‘); }
// read next token
else //2 nd
i++;
{
tkn=inexp[i];
if tkn==‘)‘ //open paranthesis
{ while (tkn=pop()) !=‘(‘ }//end of outer while
{ postexp[k]=tkn; k++; } while stack not empty
} { postexp[k]=pop(); k++ }
}
Fundamentals of Data Structure UNIT-IV 68
In Stack and Incoming priorities functions (Infix toPrefix)
Swap ICP and ISP for exponent(^) operator
icp(ch)
{ isp( ch)
if(ch=='+' || ch=='-') {
return 1; if(ch=='+' || ch=='-')
if(ch=='*' || ch=='/') return 1;
return 2;
if(ch=='*' || ch=='/')
if(ch=='^')
return 2;
return 3;
if(ch=='(') if(ch=='^')
return 5; return 4;
else else
return 0; return 0;
} }
Fundamentals of Data Structure UNIT-IV 70
Algorithm in_pre(inexp[ ])
else //3rd
{//Input: reversed infix expression
{ while (stack not empty &&
// Output : pre_exp[ ] has the prefix expression
isp(stk[top]) > icp(tkn) )
k=0; i=0;
{ pre_exp[k]=pop(); k++;
tkn=inexp[i];
}
while ( tkn!=‘\0’)
push(tkn);
{ if tkn is an operand
} // end of 3rd else
{pre_exp[k]=inexp[i];
}//end of 2nd else
k++;
}// end of 1st else
}
// read next token
else //1 st
i++;
{ if tkn==‘(‘ //open parenthesis
tkn=inexp[i];
{ push(‘(‘); }
}//end of outer while
else //2nd
while stack not empty
{
{pre_exp[k]=pop(); k++ }
if tkn==‘)‘ //open parenthesis
// reverse pre_exp to get prefix expression
{ while (tkn=pop()) !=‘(‘
}
{pre_exp[k]=tkn; k++; }
}
Fundamentals of Data Structure UNIT-IV 71
Infix to Prefix : Example 1
Operation
Expression Stack Output
5^E+D*(C^B+A) Empty -
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push
^B+A) +*( 5E^DC Print
B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ 5E^DCB^ Pop And Push
) +*(+ 5E^DCB^A Print
End +* 5E^DCB^A+ Pop Until '('
End Empty 5E^DCB^A+*+ Pop Every element
Fundamentals of Data Structure UNIT-IV 72
Infix to Prefix : Example 2
Infix string: A+B*C+D/E
Ch prefix stack
E E -
/ E /
D ED /
+ ED/ +
C ED/C +
* ED/C +, *
B ED/CB +, *
+ ED/CB* +
ED/CB*+ +
A ED/CB*+A +
ED/CB*+A+ -
Fundamentals of Data Structure UNIT-IV
73
Postfix to Infix
Algorithm Post_infx(E)
l=length of E
for i =0 to l-1
x=next_token E
case x: = operand push(x)
x: = operator
op1=pop();
op2=pop();
E1=strcat(‘(‘,op2,x,op1,’)’);
push (E1)
end
End Post_infx(E)
Fundamentals of Data Structure UNIT-IV 74
Postfix to prefix
Algorithm Post_pre(E)
l=length of E
for i =0 to l-1
x=next_token E
case x: = operand push(x)
x: = operator
op1=pop();
op2=pop();
E1=strcat(x,op2,op1);
push (E1)
end
end Post_pre(E)
Fundamentals of Data Structure UNIT-IV 75
prefix to infix prefix to postfix
Algorithm Pre_infx(E) Algorithm Pre_post(E)
l=length of E l=length of E
for i =l-1 to 0 for i =l-1 to 0
x=next_token E
x=next_token E case x: = operand push(x)
case x: = operand push(x) x: = operator
op1=pop();
x: = operator
op2=pop();
op1=pop(); E1=strcat(op1,op2,x);
op2=pop(); push (E1)
end
E1=strcat(‘(‘,op1,x,op2,’)’);
End Pre_post(E)
push (E1)
end
End Pre_infx(E)
Fundamentals of Data Structure UNIT-IV 76
Applications of Stacks
3. Expression Evaluation
❑ For evaluating expression ,it is converted into prefix or postfix form.
❑ Expression in postfix or prefix form can be easily evaluated by computer
because no precedence of operators is required in this.
Fundamentals of Data Structure UNIT-IV 77
Applications of Stacks (cont’d)
3. Expression Evaluation Example
Fundamentals of Data Structure UNIT-IV 78
Algorithm for evaluating a postfix expression
WHILE more input items exist
{
If symb is an operand
then push (opndstk,symb)
else //symbol is an operator
{
Opnd1=pop(opndstk);
Opnd2=pop(opndnstk);
Value = result of applying symb to opnd1 & opnd2
Push(opndstk,value);
} //End of else
} // end while
Result = pop (opndstk);
Fundamentals of Data Structure UNIT-IV 79
Question : Evaluate the following expression in
postfix : 623+-382/+*2^3+
Final answer is
• 49
• 51
• 52
• 7
• None of these
Fundamentals of Data Structure UNIT-IV 80
Evaluate- 623+-382/+*2^3+
Symbol opnd1 opnd2 value opndstk
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 7,2
^ 7 2 49 49
3 49,3
+ 49 3 52 52
Fundamentals of Data Structure UNIT-IV 81
Concept of Recursion
• Sometimes, the best way to solve a problem is by solving a smaller version of
the exact same problem first
• Recursion is a technique that solves a problem by solving a smaller problem of
the same type
• A procedure that is defined in terms of itself
Fundamentals of Data Structure UNIT-IV 82
Concept of Recursion
What’s Behind this function ?
int f(int a){
if (a==1)
return(1);
else
return(a * f( a-1));
}
It computes f! (factorial)
Fundamentals of Data Structure UNIT-IV 83
Concept of Recursion
Factorial:
a! = 1 * 2 * 3 * ... * (a-1) * a
Note:
a! = a * (a-1)!
remember:
...splitting up the problem into a smaller problem of the same type...
a!
a * (a-1)!
Fundamentals of Data Structure UNIT-IV 84
Concept of Recursion
int factorial(int a){
if (a==0) RECURSION !
return(1);
else
return(a * factorial( a-1));
}
Fundamentals of Data Structure UNIT-IV 85
Concept of Recursion
int factorial(int a){
if (a==1) Watching the Stack
return(1);
else
return(a * factorial(
a-1));
a=1
} Return to
L4
a=2
Return to
L4
a=3
Return to
L4
a=4 a=4
Return to Return to
a=5
L4
a=5
… L4
a=5
Initial After 1 recursion After 4th recursion
Every call to the method creates a new set of local variables !
Fundamentals of Data Structure UNIT-IV 86
Concept of Recursion
int factorial(int a){
if (a==1) Watching the Stack
return(1);
else
return(a * factorial(
a-1));
}
a=1
Return to
L4
a=2 a = 2*1 = 2
Return to Return to
L4 L4
a=3 a=3 a = 3*2 = 6
Return to Return to Return to
L4 L4 L4 a = 4*6 =
a=4 a=4 a=4
Return to Return to Return to Return
24 to
L4 L4 L4 L4
a=5 a=5 a=5 a=5 a = 5*24 = 120
After 4th recursion Result
Fundamentals of Data Structure UNIT-IV 87
Properties of Recursion
Problems that can be solved by recursion have these characteristics:
❑ One or more stopping cases have a simple, nonrecursive solution
❑ The other cases of the problem can be reduced (using recursion) to problems that are
closer to stopping cases
❑ Eventually the problem can be reduced to only stopping cases, which are relatively easy
to solve
Follow these steps to solve a recursive problem:
❑ Try to express the problem as a simpler version of itself
❑ Determine the stopping cases
❑ Determine the recursive steps
Fundamentals of Data Structure UNIT-IV 88
Concept of Recursion
The recursive algorithms we write generally consist of an if statement:
IF
the stopping case is reached solve it
ELSE
split the problem into simpler cases using recursion
Solution on stack
Solution on stack
Fundamentals of Data Structure UNIT-IV 89
Concept of Recursion
Common Programming Error
Recursion does not terminate
properly: Stack Overflow !
Fundamentals of Data Structure UNIT-IV 90
Exercise
Define a recursive solution for the following function:
n
f(x) = x
Fundamentals of Data Structure
UNIT-IV
91
Recursion vs. Iteration
You could have written the power-function iteratively, i.e. using a
loop construction
Where‘s the difference ?
Fundamentals of Data Structure
UNIT-IV
92
Recursion vs. Iteration
❑ Iteration can be used in place of recursion
❑ An iterative algorithm uses a looping construct
❑ A recursive algorithm uses a branching structure
❑ Recursive solutions are often less efficient, in terms of both time and space,
than iterative solutions
❑ Recursion can simplify the solution of a problem, often resulting in shorter,
more easily understood source code
❑ (Nearly) every recursively defined problem can be solved iteratively
iterative optimization can be implemented after recursive design
Fundamentals of Data Structure
UNIT-IV
93
Deciding whether to use a Recursive
Function
• When the depth of recursive calls is relatively “shallow”
• The recursive version does about the same amount of work as the non
recursive version
• The recursive version is shorter and simpler than the non recursive
solution
Fundamentals of Data Structure
UNIT-IV
94
Takeaways
❑ Stack is Last in First Out(LIFO) Data Structure
❑ Primitive operations on Stack are push, pop, isEmpty and isFull
❑ Stack can be represented by using Array as well as linked list.
❑ Stack is commonly used in expression conversion and Evaluation.
❑ Recursion is one of the important application of stack
Fundamentals of Data Structure
UNIT-IV
95
FAQS
1. What are the primitive operations of stack.
2. Explain with example stack applications.
Fundamentals of Data Structure
UNIT-IV
96
References
❑Horowitz,Sahani,”Fundamentals of Data Structures”, Galgotia Publication
❑Gilberg, Forozen,”Data Structures :A Pseudo Code Approach with C”
❑Maureen Sprankle,”Problem Solving and Programming Concepts”
97
Fundamentals of Data Structure UNIT-IV