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

What's hot

stack
stackstack
stack
Raj Sarode
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
Soham Nanekar
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
Interpolation search
Interpolation searchInterpolation search
Interpolation search
Usr11011
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
asmhemu
 
Queues
QueuesQueues
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
University of Science and Technology Chitttagong
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Queue
QueueQueue
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
manojmanoj218596
 

What's hot (20)

stack
stackstack
stack
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Stack project
Stack projectStack project
Stack project
 
Interpolation search
Interpolation searchInterpolation search
Interpolation search
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Queues
QueuesQueues
Queues
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Queue
QueueQueue
Queue
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 

Viewers also liked

Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
Srajan Shukla
 
Improving Pronunciation
Improving PronunciationImproving Pronunciation
Improving Pronunciation
Education Front
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Data Representation
Data RepresentationData Representation
Data Representation
Education Front
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
Education Front
 
Computer Evolution
Computer EvolutionComputer Evolution
Computer Evolution
Education Front
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
Education Front
 
Processor Basics
Processor BasicsProcessor Basics
Processor Basics
Education Front
 
Register & Memory
Register & MemoryRegister & Memory
Register & Memory
Education Front
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
Motaz Saad
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Data Representation
Data RepresentationData Representation
Data Representation
Education Front
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
Education Front
 

Viewers also liked (14)

Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Improving Pronunciation
Improving PronunciationImproving Pronunciation
Improving Pronunciation
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Data Representation
Data RepresentationData Representation
Data Representation
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 
Computer Evolution
Computer EvolutionComputer Evolution
Computer Evolution
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
 
Programming Language
Programming LanguageProgramming Language
Programming Language
 
Processor Basics
Processor BasicsProcessor Basics
Processor Basics
 
Register & Memory
Register & MemoryRegister & Memory
Register & Memory
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
 
Linked list
Linked listLinked list
Linked list
 
Data Representation
Data RepresentationData Representation
Data Representation
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 

Similar to Introduction To Stack

Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stack application
Stack applicationStack application
Stack application
Student
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
kumarkaushal17
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
VeerannaKotagi1
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
KALPANAC20
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...
muskankumari7360
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Sorting
SortingSorting
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
DivyeshKumar Jagatiya
 

Similar to Introduction To Stack (20)

Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack application
Stack applicationStack application
Stack application
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Sorting
SortingSorting
Sorting
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 

More from Education Front

Generic Software Process Models
Generic Software Process ModelsGeneric Software Process Models
Generic Software Process ModelsEducation Front
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
Problem Sloving
Problem SlovingProblem Sloving
Problem Sloving
Education Front
 
Problem Solving - 1
Problem Solving - 1Problem Solving - 1
Problem Solving - 1
Education Front
 
Process Models
Process ModelsProcess Models
Process Models
Education Front
 
Process Models
Process ModelsProcess Models
Process Models
Education Front
 
Revised Process of Communication
Revised Process of CommunicationRevised Process of Communication
Revised Process of Communication
Education Front
 
Importance of Language in Communication
Importance of Language in CommunicationImportance of Language in Communication
Importance of Language in Communication
Education Front
 
Lecture1 (SE Introduction)
Lecture1 (SE Introduction)Lecture1 (SE Introduction)
Lecture1 (SE Introduction)
Education Front
 
Lecture 2 (Software Processes)
Lecture 2 (Software Processes)Lecture 2 (Software Processes)
Lecture 2 (Software Processes)
Education Front
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureEducation Front
 
Facing Today’s Communication Challenges
Facing Today’s Communication ChallengesFacing Today’s Communication Challenges
Facing Today’s Communication Challenges
Education Front
 
Introduction To EMU
Introduction To EMUIntroduction To EMU
Introduction To EMU
Education Front
 
Lecture 2: Facing Today’s Communication Challenges.
Lecture 2: Facing Today’s Communication Challenges.Lecture 2: Facing Today’s Communication Challenges.
Lecture 2: Facing Today’s Communication Challenges.
Education Front
 
Effective communication skills
Effective communication skillsEffective communication skills
Effective communication skillsEducation Front
 
Introduction to Presentation Skills
Introduction to Presentation SkillsIntroduction to Presentation Skills
Introduction to Presentation Skills
Education Front
 
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Multiple Drug Resistance and Antibiotic Misuse in Urdu.Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Education Front
 
Multiple Drug Resistance and Antibiotic Misuse In English.
Multiple Drug Resistance and Antibiotic Misuse In English.Multiple Drug Resistance and Antibiotic Misuse In English.
Multiple Drug Resistance and Antibiotic Misuse In English.
Education Front
 

More from Education Front (18)

Generic Software Process Models
Generic Software Process ModelsGeneric Software Process Models
Generic Software Process Models
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Problem Sloving
Problem SlovingProblem Sloving
Problem Sloving
 
Problem Solving - 1
Problem Solving - 1Problem Solving - 1
Problem Solving - 1
 
Process Models
Process ModelsProcess Models
Process Models
 
Process Models
Process ModelsProcess Models
Process Models
 
Revised Process of Communication
Revised Process of CommunicationRevised Process of Communication
Revised Process of Communication
 
Importance of Language in Communication
Importance of Language in CommunicationImportance of Language in Communication
Importance of Language in Communication
 
Lecture1 (SE Introduction)
Lecture1 (SE Introduction)Lecture1 (SE Introduction)
Lecture1 (SE Introduction)
 
Lecture 2 (Software Processes)
Lecture 2 (Software Processes)Lecture 2 (Software Processes)
Lecture 2 (Software Processes)
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Facing Today’s Communication Challenges
Facing Today’s Communication ChallengesFacing Today’s Communication Challenges
Facing Today’s Communication Challenges
 
Introduction To EMU
Introduction To EMUIntroduction To EMU
Introduction To EMU
 
Lecture 2: Facing Today’s Communication Challenges.
Lecture 2: Facing Today’s Communication Challenges.Lecture 2: Facing Today’s Communication Challenges.
Lecture 2: Facing Today’s Communication Challenges.
 
Effective communication skills
Effective communication skillsEffective communication skills
Effective communication skills
 
Introduction to Presentation Skills
Introduction to Presentation SkillsIntroduction to Presentation Skills
Introduction to Presentation Skills
 
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Multiple Drug Resistance and Antibiotic Misuse in Urdu.Multiple Drug Resistance and Antibiotic Misuse in Urdu.
Multiple Drug Resistance and Antibiotic Misuse in Urdu.
 
Multiple Drug Resistance and Antibiotic Misuse In English.
Multiple Drug Resistance and Antibiotic Misuse In English.Multiple Drug Resistance and Antibiotic Misuse In English.
Multiple Drug Resistance and Antibiotic Misuse In English.
 

Recently uploaded

Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
nikitacareer3
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
iemerc2024
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 

Recently uploaded (20)

Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 

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)