2.1 Stack
2.1 Stack
2.1 Stack
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
There are two ways to implement a stack in memory:
Array implementation
Linked-list implementation
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
Array implementation of Stack
-1 Empty [Underflow]
Push 5 Push 6 Push 7 Pop 7 Push 8 Pop 8 Push 9 Pop 9 Pop 6 Pop 5
UGC NET CS 2017 Jan - II
The seven elements A, B, C, D, E, F and G are pushed
onto a stack in reverse order, i.e., starting from G. The
stack is popped five times and each element is inserted
into a queue.Two elements are deleted from the queue and
pushed back onto the stack. Now, one element is popped
from the stack. The popped item is:
A
B
F
G
GATE CS 2021 | Set 1
Consider the following sequence of operations on an empty stack.
Push(54);push(52);pop();push(55);push(62);s=pop();
Consider the following sequence of operations on an empty queue.
enqueue(21);enqueue(24);dequeue();enqueue(28);enqueue(32);q=dequeue();
The value of s+q is:
86
68
24
94
Expressions
In any programming language, if we want to perform any
calculation or to frame a condition etc., we use a set of
symbols to perform the task. These set of symbols makes
an expression.
An expression is a collection of operators and operands
that represents a specific value.
Based on the operator position, expressions are divided
into THREE (3) types. They are as follows:
Infix Expression
Postfix Expression
Prefix Expression
Infix Expression
In infix expression, operator is used in between the
operands.
The general structure of an Infix expression is as follows:
Operand1 Operator Operand2
Example
Infix: a + b * c
operands: a, b, c
operators: +, *
Prefix Expression
In prefix expression, operator is used before operands.
We can say that "Operands follows the Operator". The
general structure of Prefix expression is as follows:
Operator Operand1 Operand2
Example
Infix: a + b
Prefix: + a b
Postfix Expression
In postfix expression, operator is used after operands. We
can say that "Operator follows the Operands“. The general
structure of Postfix expression is as follows:
Operand1 Operand2 Operator
Example
Infix: a + b
Postfix: a b +
Infix to Postfix Conversion
To convert Infix Expression into Postfix Expression using a stack
data structure, we can use the following steps:
1. Read all the symbols one by one from left to right in the given
Infix Expression.
2. If the reading symbol is operand, then directly print it to the
result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the
Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the
contents of stack until the first left parenthesis is poped and print
each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,) then pop all the
operators which have higher or equal precedence than current
operator and print them to the result. After that Push the current
operator on to the Stack.
Example
Consider the following Infix Expression: ( A + B ) * ( C -
D)
Evaluation of Postfix Expressions
A postfix expression is a collection of operators and
operands in which the operator is placed after the
operands. That means, in a postfix expression the operator
follows the operands. Postfix Expression has following
general structure:
Operand1 Operand2 Operator
GATE-CS-2004
Assume that the operators +, -, × are left associative and
^ is right associative. The order of precedence (from
highest to lowest) is ^, x , +, -. The postfix expression
corresponding to the infix expression a + b × c - d ^ e ^ f
is:
a b c × + d e f ^ ^ -
a b c × + d e ^ f ^ -
a b + c × d - e ^ f ^
- + a × b c ^ ^ d e f
Postfix Expression Evaluation using Stack
A postfix expression can be evaluated using the Stack data
structure. To evaluate a postfix expression using Stack data
structure we can use the following steps...
Read all the symbols one by one from left to right in the given
Postfix Expression
If the reading symbol is operand, then push it on to the Stack.
If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped operands in two
different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result
back on to the Stack.
Finally! Perform a pop operation and display the popped value as
final result.
What is the output of the program for the following input:
52*332+*+ [Gate IT 2007]
15
25
30
150
int factorial(int n)
{
if(n = = 0)
return 1;
else
return (n * factorial(n-1));
}
Types of recursion
There are only two types of recursion as has already been
mentioned. Let us see how they are different from one another.
Direct recursion is the simpler way as it only involves a single
step of calling the original function or method or subroutine.
Direct recursion can be used to call just a single function by
itself.
Indirect recursion involves several steps.
The first call is made by the original method to a second
method, which in turn calls the original method.
In simple words, we can say that there is always a variation in
the depth of indirect recursion, and this variation in depth
depends on the number of methods involved in the process.
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;
}
Linear Recursion
A function is called the linear recursive if the function makes
a single call to itself at each time the function runs and
grows linearly in proportion to the size of the problem.
fun(n) {
if(n>0) {
fun(n-1); // Calling itself only once
}
}
Tree Recursion
To understand Tree Recursion let’s first understand Linear Recursion. If a
recursive function calling itself for one time then it’s known as Linear
Recursion. Otherwise if a recursive function calling itself for more than one
time then it’s known as Tree Recursion.
void fun(int n)
{
if (n > 0) {
printf("%d ", n);
fun(n - 1);
fun(n - 1);
}
}
int main()
{
fun(3);
return 0;
}
Nested Recursion
In this recursion, a recursive function will pass the parameter as a recursive call.
That means “recursion inside recursion”.
int fun(int n)
{
if (n > 100)
return n - 10;
return fun(fun(n + 11));
}
int main()
{
int r;
r = fun(95);
printf("%d\n", r);
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.