2.1 Stack-1
2.1 Stack-1
Syllabus
Stacks: Abstract Data Type, Primitive Stack
operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of
stack: Prefix and Postfix Expressions,
Evaluation of postfix expression, Iteration and
Recursion- Principles of recursion, Tail
recursion, Removal of recursion Problem
solving using iteration and recursion with
examples such as binary search, Fibonacci
numbers, and Hanoi towers. Tradeoffs between
iteration and recursion.
Introduction
A stack is a data structure in which addition of
new element or deletion of an existing element
always takes place at the same end. This end is
often known as top of stack.
When an item is added to a stack, the operation
is called push, and when an item is removed
from the stack the operation is called pop. Stack
is also called as Last- In-First- Out (LIFO) list.
A helpful analogy is to think of a stack of books;
you can remove only the top book, also you can
add a new book on the top.
Stack (ADT)
Stack is an Abstract data structure (ADT) works on the
principle Last in First out (LIFO). The last element add to
the stack is the first element to be delete. Insertion and
deletion can be takes place at one end called TOP. It looks
like one side closed tube.
The add operation of the stack is called push operation
The delete operation is called as pop operation.
Push operation on a full stack causes stack overflow.
Pop operation on an empty stack causes stack underflow.
TOP is a pointer, which is used to access the top element
of the stack.
If you push elements that will be added at the top of the
stack.
In the same way when we pop the elements, the element
at the top of the stack is deleted.
Operations on Stack
There are various operations which can be
performed on stack.
Push : Adding an element onto the stack
Pop : Removing an element from the stack
peek() − get the top data element of the
stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
Implementation of STACK in memory
https://www.cs.usfca.edu/~galles/visualization/
Algorithms.html
Array implementation of Stack
-1 Empty [Underflow]
int factorial(int n)
{
if(n = = 0)
return 1;
else
return (n * factorial(n-1));
}
Generating nth Fibonacci
number
int fibo(int a, int b, int n)
{
if(n==1)
return a;
else if(n==2)
return b;
else
return fibo(b,a+b,n-1);
}
Types of recursion
Direct recursion is the simpler way as it only
involves a single step of calling the original
function. Direct recursion can be used to call just
a single function by itself.
Indirect recursion
In indirect recursion, a function calls another
function which then calls the first function again. The
recursion ends when the base case is met, at this
point, the process stops.
There is always a variation in the depth of indirect
recursion, and this variation in depth depends on the
number of functions involved in the recursion.
Example
void funA(int n)
{
if (n > 0) {
printf("%d ", n);
funB(n - 1);
}
}
void funB(int n)
{
if (n > 1) {
printf("%d ", n);
funA(n / 2);
}
}
int main() {
funA(20); }
Head Recursion
If a recursive function calling itself and that recursive call is the first
statement in the function then it’s known as Head
Recursion. There’s no statement, no operation before the call.
The function doesn’t have to process or perform any operation at
the time of calling and all operations are done at returning time.
void fun(int n)
{
if (n > 0) {
fun(n - 1);
printf("%d ", n);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}
Tail recursion
The tail recursion is basically using the recursive function as
the last statement of the function. So when nothing is left
to do after coming back from the recursive call, that is
called tail recursion.
Using Stack
GATE CS 2011
unsigned int fun(unsigned int n, unsigned int r)
{
if (n > 0) return (n%r + fun (n/r, r ));
else return 0;
}
What is the return value when it is called as
fun(345, 10) ?
345
12
5
3
GATE-CS-2015 (Set 2)
int fun (int n)
{
int x=1, k;
if (n==1) return x;
for (k=1; k < n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
The return value of fun(5) is:
1
21
51
31
fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) + fun(3) * fun(2) + fun(4) *
fun(1)
= 1 + 2*[fun(1)*fun(4) + fun(2)*fun(3 )]
GATE-CS-2004
int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is:
5
6
7
8
GATE-CS-2016 (Set 1)
void count(int n) {
static int d=1;
printf(“%d”, n);
printf(“%d”, d);
d++;
if(n > 1) count(n-1);
printf(“%d”, d);
}
What will be the output of count(3)?
3 1 2 2 1 3 4 4 4
3 1 2 1 1 1 2 2 2
3 1 2 2 1 3 4
3 1 2 1 1 1 2