Lec 20 Stack
Lec 20 Stack
01/21/2025
Thapar Institute of Engineering & Technology
Introduction
A stack is a data structure that provides storage of data in such a way that the
element stored last will be retrieved first.
Example:
In real life we can think of stack as a stack of copies, stack of plates etc.
The first copy put in the stack is the last one to be removed.
Similarly last copy to put in the stack is the first one to be removed.
1. Push()
• It is the term used to insert an element into a stack.
2. Pop()
• It is the term used to delete an element from a stack.
3. peek()
• Get the top data element of the stack, without removing it.
4. isFull()
• Check if stack is full.
5. isEmpty()
• Check if stack is empty.
As an Array
As a Linked List
2 20 1
TO 10 0
NOTE: The variable top contains the index number P
NOTE: Pushing one more element into the stack will result in
overflow. Thapar Institute of Engineering & Technology
void push()
{
A B C D E
if(top>=n-1)
0 1 2 3 TOP = 4 5 6 7 8 9
{
printf("\n STACK is over flow"); A B C D E F
0 1 2 3 4 TOP =5 6 7 8 9
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
Thapar Institute of Engineering & Technology
Pop Operation
The pop operation deletes the most recently entered item from
the stack.
Any attempt to delete an element from the empty stack results
in “data underflow” condition.
The variable TOP contains the index number of the current top
position of the stack.
Each time the pop operation is performed, the TOP variable is
decremented by 1.
top = 2
NOTE: Each node of a stack as a linked list has two parts : data part and link
part and is created with the help of self referential structure.
The data part stores the data and link part stores the address of the next node of
the linked list.
NODE
TOP
DAT NEX
A Thapar Institute of Engineering & Technology
Push operation using Linked List
Push 5 Push 10
Push 20
Pop Pop
struct node
Self Referential Structure: These are special structures
{
which contains pointers to themselves.
int data;
Here, next is a pointer of type node itself.
struct node *next;
Self Referential structures are needed to create Linked
};
Lists.
temp- // address stored in TOP gets stored in next of newly created node.
>next=top;
printf(“%d”, temp=data); // This line will display data stored in deleted node
free (temp) ; // delete operator will delete the node stored at temp
}
else
printf(“underflow”);
}
When a program executes, stack is used to store the return address at time of
function call. After the execution of the function is over, return address is popped
from stack and control is returned back to the calling function.
In Backtracking algorithms
Thapar Institute of Engineering & Technology
Reverse of String or List
●We can accomplish this task by pushing each character or
member from the string/list in the order it appears
●When the line is finished, characters are then popped off the
stack
E
P
G G
O N N N
A
P
A A A
Name : Name : Name : Name : Name :
“L” “LE” “LEG” “LEGN” “LEGNA” Thapar Institute of Engineering & Technology
Arithmetic Expressions
Operators
Binary : +, -, *, / ,%
Unary: -
Priority convention:
*, /, % have medium priority
+, - have lowest priority
• Infix notation
• Is format where operator is specified in between the two operands.
• a+b
• Prefix notation
• Is format where operator is specified before the two operands.
• +ab
• Postfix notation
• Is format where operator is specified after the two operands. Postfix notation is also called RPN
or Reverse Polish Notation.
• ab+
A+B + AB AB +
(A - C) + B + - ACB AC – B +
A+(B*C) + A * BC ABC *+
(A+B)/(C-D) /+ AB – CD AB + CD -/
• Example :
• "* + 5 6 3" is (5+6)*3, and cannot be interpreted as 5+(6*3),
whereas parenthesis is required to achieve with infix.
Þ (A B + ) - C
ÞAB+C–
Þ A + (B C *)
Þ A B CInfix
Example: * + notation a + c – r / b * r
Þ a + c – (r b /) * r
Þ a + c – (r b / r *)
Þ (a c +) – (r b / r * )
Þ ac+rb/r*-
2
Step 4. If scanned element = operand, add it to P.
Step 5. If scanned element = operator, then:
a) Repeatedly pop from STACK and add to P each operator from the top of the stack until the
operator at the top of the stack has lower precedence than the operator extracted from I.
b) Add scanned operator to the top of the STACK.
Step 6. If scanned element = ‘)’ then:
b) Repeatedly pop from STACK and add to P each operator until the left parenthesis is
encountered.
Thapar Institute of Engineering & Technology
Example 1: Infix notation A - B
Step 1. Scan the postfix expression P from left to right and repeat the steps 2,
3 until the STACK is empty.
Step 2. If the scanned element = operand, push it into the STACK.
Step 3. If the scanned element = operator, then
a) Pop the two operands viz operand A and operand B from the
STACK.
b) Evaluate ((operand B) operator (operand A))
c) Push the result of evaluation into the STACK.
Thapar Institute of Engineering & Technology
Thapar Institute of Engineering & Technology
Thapar Institute of Engineering & Technology
Thapar Institute of Engineering & Technology
Recursion
Recursive Solution
Base case
factorial (5) = 5 x factorial (4)
arrived Some
= 5 x (4 x factorial (3)) concept from
elementary
= 5 x (4 x (3 x factorial (2)))
maths: Solve the
= 5 x (4 x (3 x (2 x factorial (1)))) inner-most
bracket, first, and
= 5 x (4 x (3 x (2 x (1 x factorial (0)))))
then go outward
= 5 x (4 x (3 x (2 x (1 x 1))))
= 5 x (4 x (3 x (2 x 1)))
= 5 x (4 x (3 x 2))
= 5 x (4 x 6)
= 5 x 24
= 120
m 1 if n 0,
The puzzle starts with the disks in a neat stack in ascending order of size on
one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following rules:
• Only one disk must be moved at a time.
• Each move consists of taking the upper disk from one of the rods and
sliding it onto another rod, on top of the other disks that may
already be present on that rod.
Tower(2,A,C, A A B
B) B (2)
C B (3)
Tower(1,C,A,
Tower(3,A,B,C) A B) A. C
C Tower(1,B,C, (4)
A)
B. A (5)
Tower(2,B,A, B C B C
C) (6)
Tower(1,A,B, A C
C) (7)
2n - 1
where n is the number of disks.
Explicit Pattern
Number of Number of
Disks Moves
1 1
2 3
3 7
Powers of two help reveal the 4 15
5 31
pattern:
Number of Disks (n) Number of
Moves
1 2^1 - 1 = 2 - 1
=1
2 2^2 - 1 = 4 - 1
=3
3 2^3 - 1 = 8 - 1
=7 Thapar Institute of Engineering & Technology
For N=4 , how will this recursion solve the
problem as shown