SlideShare a Scribd company logo
Introduction To Stack 
By: 
Dr. Ghulam Rasool
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
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
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 notations
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*
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 *+
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
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)
Assignment I 
Q.2 Write a program that should take infix expression and convert it 
into PSN. The program should also evaluate PSN expression.
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.
Example 
• infix expression: 1+ 2 *3 
• Postfix : 1 2 3 + * 
Symbol Stack 
1 1 
2 1, 2 
3 1, 2, 3 
+ 1, 5 
* 5
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
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)

More Related Content

PPTX
Stack and its Applications : Data Structures ADT
PPTX
Queue and its operations
PPT
Searching in c language
PPT
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
PPSX
Data Structure (Queue)
PPTX
Queue in Data Structure
PPTX
Unit 3 stack
PDF
Applications of stack
Stack and its Applications : Data Structures ADT
Queue and its operations
Searching in c language
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Data Structure (Queue)
Queue in Data Structure
Unit 3 stack
Applications of stack

What's hot (20)

PPTX
Python-List.pptx
PPT
PPTX
Unit 1 polynomial manipulation
PPTX
Unit 5 internal sorting & files
PPTX
Introduction to data structure ppt
PPTX
Sorting in python
PPTX
PPTX
Stack - Data Structure - Notes
PPTX
Bfs and Dfs
PPTX
My lectures circular queue
PPTX
Data structure Stack
PPTX
Polish Notation In Data Structure
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Tries data structures
PPTX
Lecture 02: Preliminaries of Data structure
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPTX
Linked list
PPTX
Different types of Linked list.
PPSX
PDF
Infix to postfix expression in ds
Python-List.pptx
Unit 1 polynomial manipulation
Unit 5 internal sorting & files
Introduction to data structure ppt
Sorting in python
Stack - Data Structure - Notes
Bfs and Dfs
My lectures circular queue
Data structure Stack
Polish Notation In Data Structure
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Tries data structures
Lecture 02: Preliminaries of Data structure
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Linked list
Different types of Linked list.
Infix to postfix expression in ds
Ad

Viewers also liked (14)

PPTX
Ppt on Linked list,stack,queue
PPT
Improving Pronunciation
PPTX
Introduction to stack
PPTX
Data Representation
PPTX
Introduction To Data Structures.
PPTX
Computer Evolution
PPTX
Introduction to Algorithm
PPTX
Programming Language
PPTX
Processor Basics
PPTX
Register & Memory
PPT
Assembly Language Lecture 1
PPTX
Linked list
PPTX
Data Representation
PPT
Assembly Language Basics
Ppt on Linked list,stack,queue
Improving Pronunciation
Introduction to stack
Data Representation
Introduction To Data Structures.
Computer Evolution
Introduction to Algorithm
Programming Language
Processor Basics
Register & Memory
Assembly Language Lecture 1
Linked list
Data Representation
Assembly Language Basics
Ad

Similar to Introduction To Stack (20)

PPTX
Stack_Overview_Implementation_WithVode.pptx
PPSX
Data structure_Stack Introduction & app.
PPT
Data structure lecture7
PPT
week 7,8,10,11 alll files included from .ppt
PPTX
Stacks and queues using aaray line .pptx
PPTX
DS-UNIT 3 FINAL.pptx
PPTX
Implementation of stacks and queues in C
PDF
Data structure lab manual
PPTX
Stack & Queue in Data Structure and Algorithms
PPTX
Data strutcure and annalysis topic stack
PPT
Stack application
PPTX
Stack data structure
PDF
Data Structures And Algorithms(stacks queues)
PPTX
DS MOD2 (1) (1).pptx
PPTX
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
PPTX
Stack,queue and linked list data structure.pptx
PPTX
Stacks IN DATA STRUCTURES
PPTX
Unit 3 Stacks and Queues.pptx
PPTX
DS UNIT 2 PPT.pptx stack queue representations
PPTX
Stacks in DATA STRUCTURE
Stack_Overview_Implementation_WithVode.pptx
Data structure_Stack Introduction & app.
Data structure lecture7
week 7,8,10,11 alll files included from .ppt
Stacks and queues using aaray line .pptx
DS-UNIT 3 FINAL.pptx
Implementation of stacks and queues in C
Data structure lab manual
Stack & Queue in Data Structure and Algorithms
Data strutcure and annalysis topic stack
Stack application
Stack data structure
Data Structures And Algorithms(stacks queues)
DS MOD2 (1) (1).pptx
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
Stack,queue and linked list data structure.pptx
Stacks IN DATA STRUCTURES
Unit 3 Stacks and Queues.pptx
DS UNIT 2 PPT.pptx stack queue representations
Stacks in DATA STRUCTURE

More from Education Front (18)

PPTX
Generic Software Process Models
PPTX
2- Dimensional Arrays
PPTX
Problem Sloving
PPTX
Problem Solving - 1
PPTX
Process Models
PPTX
Process Models
PPT
Revised Process of Communication
PPT
Importance of Language in Communication
PPT
Lecture1 (SE Introduction)
PPTX
Lecture 2 (Software Processes)
PPTX
Introduction to data structure
PPTX
Facing Today’s Communication Challenges
PPTX
Introduction To EMU
PPTX
Lecture 2: Facing Today’s Communication Challenges.
PPTX
Effective communication skills
PPTX
Introduction to Presentation Skills
PPTX
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
PPTX
Multiple Drug Resistance and Antibiotic Misuse In English.
Generic Software Process Models
2- Dimensional Arrays
Problem Sloving
Problem Solving - 1
Process Models
Process Models
Revised Process of Communication
Importance of Language in Communication
Lecture1 (SE Introduction)
Lecture 2 (Software Processes)
Introduction to data structure
Facing Today’s Communication Challenges
Introduction To EMU
Lecture 2: Facing Today’s Communication Challenges.
Effective communication skills
Introduction to Presentation Skills
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Multiple Drug Resistance and Antibiotic Misuse In English.

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
additive manufacturing of ss316l using mig welding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
PPT on Performance Review to get promotions
DOCX
573137875-Attendance-Management-System-original
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Arduino robotics embedded978-1-4302-3184-4.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Sustainable Sites - Green Building Construction
additive manufacturing of ss316l using mig welding
Embodied AI: Ushering in the Next Era of Intelligent Systems
CH1 Production IntroductoryConcepts.pptx
UNIT 4 Total Quality Management .pptx
Internet of Things (IOT) - A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT on Performance Review to get promotions
573137875-Attendance-Management-System-original
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Lecture Notes Electrical Wiring System Components
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
OOP with Java - Java Introduction (Basics)
UNIT-1 - COAL BASED THERMAL POWER PLANTS

Introduction To Stack

  • 1. Introduction To Stack By: Dr. Ghulam Rasool
  • 2. 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
  • 3. 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
  • 4. 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 notations
  • 5. 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*
  • 6. 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 *+
  • 7. 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
  • 8. 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)
  • 9. Assignment I Q.2 Write a program that should take infix expression and convert it into PSN. The program should also evaluate PSN expression.
  • 10. 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.
  • 11. Example • infix expression: 1+ 2 *3 • Postfix : 1 2 3 + * Symbol Stack 1 1 2 1, 2 3 1, 2, 3 + 1, 5 * 5
  • 12. 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
  • 13. 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)