0% found this document useful (0 votes)
7 views13 pages

Introduction to Stack Data Structure

This document provides an introduction to stacks, including: 1. Stacks are LIFO (last in, first out) data structures that can be implemented using arrays or linked lists. Operations include push to insert and pop to remove elements. 2. Stack implementations using arrays track the top index, and operations like push increment this index. Pop removes the element at the current top. 3. Stacks have applications in recursion, arithmetic expressions, and parsing postfix notation. Towers of Hanoi is presented as a recursive problem solved using stacks.

Uploaded by

Nirmalendu Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Introduction to Stack Data Structure

This document provides an introduction to stacks, including: 1. Stacks are LIFO (last in, first out) data structures that can be implemented using arrays or linked lists. Operations include push to insert and pop to remove elements. 2. Stack implementations using arrays track the top index, and operations like push increment this index. Pop removes the element at the current top. 3. Stacks have applications in recursion, arithmetic expressions, and parsing postfix notation. Towers of Hanoi is presented as a recursive problem solved using stacks.

Uploaded by

Nirmalendu Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Introduction To Stack

By:
Dr. Ghulam Rasool

For more downloads please visit:


1
[Link]
Stack
• A variable sized data structure in which insertion or deletion
is made just from one end
or
• A LIFO data structure is called Stack

Operations on Stack
1) Push ( to insert an element into stack)
2) Pop( to take an element out of stack)
The last element inserted is deleted or taken out first and
first element at last
The stack can be implemented using Array as well as Link
List

For more downloads please visit:


2
[Link]
Stack using Arrays
• The no of elements that can be inserted in a the stack is
the dimension of array. The current position of stack(i.e
its size is known by a pointer called its top.
Algorithm to insert data in stack Push(S,Top, X, N)
1 If Top >= N then
wrtie (‘Stack full’) and exit
2 Top=Top+1
3 S[Top]= X
4 Exit

For more downloads please visit:


3
[Link]
Stack Operations
Algorithm to delete an element from Stack Pop(S, Top)
1 If Top= 0 then
write(‘ stack is empty’) and exit
2 Pop = S[Top]
3 Top= Top-1
4 Exit
Applications of Stack
i) Recursion
ii) Evaluation of Arithmetic expressions
a) Infix notation
b) prefix notations
c) postfix notationsFor more downloads please visit:
4
[Link]
Recursion
• The calling of a subprogram to itself is called recursions
Examples: Factorial of a number
Fact(N)
1) If N= 0 then
Fact =1
2) Fact= N* Fact(N-1)

Polish Suffix notations


• It is named after Polish mathematician Jan Lukasiewiez.
The operator symbol is placed before its two operands in
this notations
• Examples: AB+, CD-, EF*
For more downloads please visit:
5
[Link]
PSN notations
• The expressions in which operands are preceded by the
operators are called PSN notations or postfix notations.
• Example: Following are PSN notations
AB+
DE*
ABC*+
• Convert A+B*C into PSN
Symbol scanned operator Stack PSN
A A
+ + A
B + AB
* +* AB
C +* ABC
$ ABC *+
For more downloads please visit:
6
[Link]
RPN(Q, P)
• Suppose Q is expression in infix form. This algorithm
covert this expression into equivalent postfix expression P
1) Scan Q from left to right and repeat Steps 2 to 5 for each
element of Q until the end of Expression
2) If an operand is encountered, add it to P
3) If a left parenthesis is encountered, push it onto stack
4) If an operator is encountered then:
a) Repeatedly pop from stack and add to P each operator
which has the same precedence as or higher precedence
than (?)
b) Add (?) to stack
5) If a right parenthesis is encountered then:
a) Repeatedly pop from stack and add to P each operator
until a left parenthesis is encountered
b) remove left parenthesis
6) Exit For more downloads please visit:
7
[Link]
Quiz 1
• Q.1 What is difference b/w Stack and Array.
Can a stack be an array and an array can be a
stack?
• Q.2 Convert following expressions into PSN
((A- (B+C)) *D)^(E+F)

For more downloads please visit:


8
[Link]
Assignment I
Q.2 Write a program that should take infix expression and convert it
into PSN. The program should also evaluate PSN expression.

For more downloads please visit:


9
[Link]
Algorithm for evaluation of PSN
1 Scan the Postfix string from left to right.
2 Initialise an empty stack.
3 Repeat step 4 until the end of string
4 If the scanned character is an operand, Push
it to the stack.
a) If the scanned character is an Operator, pop top
two elements and apply operator.
b) Push result back on top of stack
5 Pop last value from stack
5 Exit.
For more downloads please visit:
10
[Link]
Example
• infix expression: 1+ 2 *3
• Postfix :1 2 3 + *
Symbol Stack
1 1
2 1, 2
3 1, 2, 3
+ 1, 5
* 5

For more downloads please visit:


11
[Link]
Towers of Hanoi
• Suppose three Pegs labled A, B and C and Suppose on Peg A
there are placed a finite number n of disks with decreasing size.
• The objective of game is to move the disks from Peg A to PegC
using Peg B as an intermediate.
• The rules of game are as:
i) Only one disk may be moved at a time, specifically only the
top disk on any Peg may be moved to any other Peg.
ii) A larger disk can not be placed on a smaller disk
• Example: For n=3
• Move top disk from A to C Move top disk from A to C
• Move top disk from A to B Move top disk from B to A
• Move top disk from C to B Move top disk from B to C
• Move top disk from A to C

For more downloads please visit:


12
[Link]
Recursive Solution
1 Move top n-1 disks from A to B
2 Move the top disk from A to C
3 Move the top n-1 disks from B to C
General notation:
Tower(N, Beg, Sec, End)
When N=1
Tower(1, Beg, Sec, End)
When N>1
Tower( N-1 Beg, End, Sec)
Tower(1, Beg, Sec, End) Beg …> End
Tower( N-1, Sec, Beg, End)
For more downloads please visit:
13
[Link]

You might also like