Unit - IV Stack
Unit - IV Stack
Unit 4
Stack
Sunil Emekar
Lecturer in Computer Engineering
2
Sunil P. Emekar
Stack
3 A stack is a linear data structure that follows the Last-In-First-Out
(LIFO) principle.
It is called as stack because it behaves like a real-world stack,
piles of books, etc.
It behaves like a stack of plates, where the last plate added is
the first one to be removed.
Think of it this way:
Pushing an element onto the stack is like adding a new plate on
top.
Popping an element removes the top plate from the stack.
Sunil P. Emekar
Stack
4
Key Operations on Stack Data Structures
Push: Adds an element to the top of the stack.
Pop: Removes the top element from the stack.
IsEmpty: Checks if the stack is empty.
IsFull: Checks if the stack is full (in case of fixed-size arrays).
Sunil P. Emekar
5 Array representation of
Stack
Sunil P. Emekar
PUSH operation
6
The steps involved in the PUSH operation is given below:
Before inserting an element in a stack, we check whether the
stack is full.
If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
When we initialize a stack, we set the value of top as -1 to check
that the stack is empty.
When the new element is pushed in a stack, first, the value of the
top gets incremented, i.e., top=top+1, and the element will be
placed at the new position of the top.
The elements will be inserted until we reach the max size of the
stack.
Sunil P. Emekar
7
Sunil P. Emekar
POP operation
8
The steps involved in the POP operation is given below:
Before deleting the element from the stack, we check whether the
stack is empty.
If we try to delete the element from the empty stack, then
the underflow condition occurs.
If the stack is not empty, we first access the element which is
pointed by the top
Once the pop operation is performed, the top is decremented by
1, i.e., top=top-1.
Sunil P. Emekar
9
Sunil P. Emekar
Polish Notation
10
Polish Notation in the data structure is a method of
expressing mathematical, logical, and algebraic equations
universally.
This type of notation was introduced by the Polish
mathematician Jan Lukasiewicz.
This notation is used by the compiler to evaluate
mathematical equations based on their order of operations.
An arithmetic expression mainly comprises of 2 parts, that
are Operands and Operators. (2 ^ 3 + 5 * 2 ^ 2 - 12 / 6 )
Operands are either numbers or variables that can be
replaced by numbers to evaluate the expressions.
Operators are symbols symbolizing the operation to be
performed between operands present in the expression.
Precedence used for binary operators
Highest – Exponentiation (^)
Next Highest – Multiplication ( * ) and Division ( / )
Sunil P. Emekar
Sunil P. Emekar
2. Prefix Notation :
This polish notation in data structure states that the
12
operator should be present as a prefix or before the
operands. This notation is also known as "Polish
Notation".
+AB -CD
The fundamental property of Polish notation is that the
order in which the operations are to be performed is
completely determined by the position of the
operators and operands in the expression
3. Postfix Notation :
This notation states that the operator should be
present as a suffix, postfix, or after the operands.
It is also known as Suffix notation or Reverse Polish
Notation.
AB+ CD-
Sunil P. Emekar
Evaluation of Postfix expression
13
The computer usually evaluate an arithmetic expression
written in fixed notation in two steps
first it converts the expression to post fixed notation and
then it evaluates the post fix expression
in each step the stack is the main tool that is used to
accomplish the given task.
Sunil P. Emekar
14
Sunil P. Emekar
15
Sunil P. Emekar
Conversion of an Infix
16 Expression to Postfix Expression
Steps to convert infix to Postfix:
Read the given expression from left to right. If the scanned
element is an operand, simply append it to the resultant postfix
expression.
If the scanned operator is an operator and the stack is empty,
push the operator into the stack
If the scanned element is an operator and the precedence of
the operator is greater than or equal to the precedence of the
operator on the top of the stack, push it into the stack
If the scanned operator is an operator and the precedence of
the scanned operator is less than the precedence of the
operator on the top of the stack, pop the operator on the top of
the stack and add it to the postfix expression until the stack
becomes empty or the precedence of the operator on the top
of the stack is less than the precedence of the scanned
operator. Now, push the scanned operator into the stack.
Sunil P. Emekar
17
Sunil P. Emekar
example: 3 + 8 - 9 / 8
18
3 - 3
+ + 3
8 + 38
- - 38+
9 - 38+9
/ -/ 38+9
8 -/ 38+98
Postfix 38+98/-
Sunil P. Emekar expression:
19
Sunil P. Emekar
20
Sunil P. Emekar
Reverse a stack
21
Sunil P. Emekar
Example:
Input: 23,56,82,9023,56,82,90
22
Output: 90,82,56,2390,82,56,23
Sunil P. Emekar
Implementation of stack using singly
23
linked list
To implement a stack using the singly linked list concept,
all the singly linked list operations should be performed
based on Stack operations LIFO(last in first out)
Sunil P. Emekar
Instead of using array, we can also use linked list to
implement stack. Linked list allocates the memory
24 dynamically.
In linked list implementation of stack, the nodes are
maintained non-contiguously in the memory.
Each node contains a pointer to its immediate successor
node in the stack.
Stack is said to be overflown if the space left in the memory
heap is not enough to create a node.
Sunil P. Emekar
Push Operation
25
ptr->val = val;
ptr->next = head;
Sunil P. Emekar head=ptr;
void push ()
{
int val;
26 struct node *ptr =(struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
Sunil P. Emekar
}
POP Operation
27
Sunil P. Emekar
28 void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
printf("Item popped");
}
}
Sunil P. Emekar
Recursion
29 The process in which a function calls itself directly or indirectly
is called recursion and the corresponding function is called a
recursive function.
A recursive function solves a particular problem by calling a
copy of itself and solving smaller sub problems of the original
problems. Need of Recursion
Recursion is an amazing technique with the help of which we can
reduce the length of our code and make it easier to read and
write.
Properties of Recursion:
Performing the same operations multiple times with different
inputs.
In every step, we try smaller inputs to make the problem smaller.
Base condition is needed to stop the recursion otherwise infinite
loop will occur.
Applications of Recursion:
Factorial function , Fibonacci series, Towers of Hanoi (TOH),
Sunil P. Emekar
Inorder/Preorder/Postorder Tree Traversals, DFS of Graph.
Factorial of number
30
Sunil P. Emekar
31
Sunil P. Emekar
32
Sunil P. Emekar
#include<stdio.h>
#include<conio.h>
33
int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
getch();
return 0;
Sunil P. Emekar
}
34 Fibonacci sequence
Sunil P. Emekar
35
Sunil P. Emekar
#include <stdio.h>
36 int fibonacci(int n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}
int main() {
int n;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Sunil P. Emekar
Tower of Hanoi
37 The Tower of Hanoi is a classic mathematical puzzle. It consists
of three rods (or pegs) and several disks which are of different
sizes, which can slide onto any rod.
Objective :
The goal is to move the entire stack to another rod, following these
very basic rules given below:
Only one disk can be moved at a time.
Each move consists of taking the upper disk from one of the stacks
and placing it on top of the other.
No disk may be placed on top of a smaller disk.
Sunil P. Emekar
38
Sunil P. Emekar