SlideShare a Scribd company logo
1 of 19
STACKSTACK
Kalyani Neve
Asst.Prof.
GHRIBM,Jalgaon
Stack :
Stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack.
Two operations mainly performed on stack:
a) Push is used to insert an element
b) Pop is used to delete an element.
Push
AAA
BBB
CCC
Stack operations :
1. Stack() creates a new stack that is empty. It needs no
parameters and returns an empty stack.
2. push(item) adds a new item to the top of the stack. It needs
the item and returns nothing.
3. pop() removes the top item from the stack. It needs no
parameters and returns the item. The stack is modified.
4. peek() returns the top item from the stack but does not
remove it. It needs no parameters. The stack is not
modified.
5. isEmpty() tests to see whether the stack is empty. It needs
no parameters and returns a boolean value.
6. size() returns the number of items on the stack. It needs no
parameters and returns an integer.
Static Representation of Stack :
Algorithms for Push and Pop Operations on Stack :
PUSH(S, TOP, X)
This procedure insert an element X to the top of a stack which
is represented by S containing N elements with a pointer TOP
denoting the top element in the stack.
1. [Check stack overflow?]
If TOP >= N then :
Print OVERFLOW, and Return
2. [Increment TOP]
Set TOP := TOP +1
3. [Insert ITEM in new TOP position.]
Set S[TOP] := ITEM
4. Return
POP(S, TOP, ITEM)
This procedure deletes the top element of S and assigns it to the
variable ITEM.
1. [Check for stack underflow?]
If TOP = 0 then:
Print UNDERFLOW, and Return
2. [Assign TOP element to stack]
Set ITEM := S[TOP]
3. [Decreases TOP]
Set TOP := TOP -1
4. Return
Dynamic Representation of Stack :
Linked Representation :
The linked presentation of stack, i.e. stack is implemented using
a SLL.
The nodes are divided into two parts:
1. INFO hold the element of the stack.
2. LINK hold pointers to the next element in the stack.
AAA BBB CCC
TOP
PUSH_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure pushes an ITEM into a linked stack.
1. [Available stack?]
If AVAIL=NULL then
Write OVERFLOW, and Exit
2. [Remove first node from AVAIL list.]
Set New := AVAIL and AVAIL := LINK[AVAIL]
3. [Copies ITEM into new node]
Set INFO[NEW] := ITEM
4. [New node points to the original top node in the stack]
Set LINK[NEW] := TOP
5. [Reset TOP]
Set TOP := NEW
6. Exit
POP_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure deletes the top element of a linked list and assigns
it to the variable ITEM.
1. [Stack has an item to be removed?]
If TOP = NULL then Write : UNDERFLOW & Exit
2. [Copies top element into ITEM]
Set ITEM := INFO[TOP]
3. [Remember the old value of TOP pointer in TEMP and reset
TOP to point to the next element in the stack]
Set TEMP := TOP and TOP := LINK[TOP]
4. [Return deleted node to the AVAIL list]
Set LINK[TEMP := AVAIL and AVAIL := TEMP
5. Exit
Applications of Stack :
There are two main applications of stack:
1. Infix to Postfix
2. Evaluation of Postfix Expression
3.
1. Infix to Postfix
Infix : The operator written in between the operands. Eg. A+B
Prefix : The notation in which the operator is written before the
operands, it is called Prefix. Eg. +AB
Postfix: The notation in which the operator is written after the
operands, it is called Postfix. Eg. AB+
Infix to Postfix: Algorithm
POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression P.
1. Push “(“ onto stack, and add “)” to the end of Q.
2. Scan Q from left to right and repeat steps 3 to 6 for each
element of Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then:
1. Repeatedly POP from STACK and add to P each
operator (on the top of STACK) which has the same
precedence as or higher precedence than operator.
2. Add operator to stack.
[End of If Structure]
6. If a right parenthesis is encountered, then :
1. Repeatedly POP from stack and add to P each
operator (on the top of stack) until a left
parenthesis is encountered.
2. Remove the left parenthesis. [Do not add to P.]
[End of If Structure]
[End of step 2 loop]
6. Exit
1. A+(B*C-(D/E^F)*G)*G
Symbol scanned STACK POSTFIX
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+
2. (A+B) * C/D+E^F/G
3. A-N/(C*D^E)
4. A+B/C-D
2. Evaluating Postfix Notation
• Use a stack to evaluate an expression in postfix notation.
• The postfix expression to be evaluated is scanned from left
to right.
• Variables or constants are pushed onto the stack.
• When an operator is encountered, the indicated action is
performed using the top elements of the stack, and the result
replaces the operands on the stack.
Postfix expressions: Algorithm using stacks (cont.)
Algorithm for evaluating a postfix expression
WHILE more input items exist
{
If symb is an operand
then push (opndstk,symb)
else //symbol is an operator
{
Opnd1=pop(opndstk);
Opnd2=pop(opndnstk);
Value = result of applying symb to opnd1 & opnd2
Push(opndstk,value);
} //End of else
} // end while
Result = pop (opndstk);

More Related Content

What's hot (20)

Stack and queue
Stack and queueStack and queue
Stack and queue
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack
StackStack
Stack
 
Stack project
Stack projectStack project
Stack project
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithms
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Sorting
SortingSorting
Sorting
 

Similar to Unit 3 stack

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) .pptxhaaamin01
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms StacksManishPrajapati78
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid ali rashid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
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.pptxKALPANAC20
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 

Similar to Unit 3 stack (20)

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
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.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
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Stack
StackStack
Stack
 

More from kalyanineve

Sorting and searching
Sorting and searchingSorting and searching
Sorting and searchingkalyanineve
 
Unit 5 graphs minimum spanning trees
Unit 5   graphs minimum spanning treesUnit 5   graphs minimum spanning trees
Unit 5 graphs minimum spanning treeskalyanineve
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 

More from kalyanineve (6)

Sorting and searching
Sorting and searchingSorting and searching
Sorting and searching
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Unit 5 graphs minimum spanning trees
Unit 5   graphs minimum spanning treesUnit 5   graphs minimum spanning trees
Unit 5 graphs minimum spanning trees
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 

Recently uploaded

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

Unit 3 stack

  • 2. Stack : Stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. Two operations mainly performed on stack: a) Push is used to insert an element b) Pop is used to delete an element. Push AAA BBB CCC
  • 3. Stack operations : 1. Stack() creates a new stack that is empty. It needs no parameters and returns an empty stack. 2. push(item) adds a new item to the top of the stack. It needs the item and returns nothing. 3. pop() removes the top item from the stack. It needs no parameters and returns the item. The stack is modified. 4. peek() returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified.
  • 4. 5. isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value. 6. size() returns the number of items on the stack. It needs no parameters and returns an integer. Static Representation of Stack : Algorithms for Push and Pop Operations on Stack :
  • 5. PUSH(S, TOP, X) This procedure insert an element X to the top of a stack which is represented by S containing N elements with a pointer TOP denoting the top element in the stack. 1. [Check stack overflow?] If TOP >= N then : Print OVERFLOW, and Return 2. [Increment TOP] Set TOP := TOP +1 3. [Insert ITEM in new TOP position.] Set S[TOP] := ITEM 4. Return
  • 6. POP(S, TOP, ITEM) This procedure deletes the top element of S and assigns it to the variable ITEM. 1. [Check for stack underflow?] If TOP = 0 then: Print UNDERFLOW, and Return 2. [Assign TOP element to stack] Set ITEM := S[TOP] 3. [Decreases TOP] Set TOP := TOP -1 4. Return
  • 7. Dynamic Representation of Stack : Linked Representation : The linked presentation of stack, i.e. stack is implemented using a SLL. The nodes are divided into two parts: 1. INFO hold the element of the stack. 2. LINK hold pointers to the next element in the stack. AAA BBB CCC TOP
  • 8. PUSH_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM) This procedure pushes an ITEM into a linked stack. 1. [Available stack?] If AVAIL=NULL then Write OVERFLOW, and Exit 2. [Remove first node from AVAIL list.] Set New := AVAIL and AVAIL := LINK[AVAIL] 3. [Copies ITEM into new node] Set INFO[NEW] := ITEM 4. [New node points to the original top node in the stack] Set LINK[NEW] := TOP 5. [Reset TOP] Set TOP := NEW 6. Exit
  • 9. POP_LINKSTK(INFO, LINK, TOP, AVAIL, ITEM) This procedure deletes the top element of a linked list and assigns it to the variable ITEM. 1. [Stack has an item to be removed?] If TOP = NULL then Write : UNDERFLOW & Exit 2. [Copies top element into ITEM] Set ITEM := INFO[TOP] 3. [Remember the old value of TOP pointer in TEMP and reset TOP to point to the next element in the stack] Set TEMP := TOP and TOP := LINK[TOP] 4. [Return deleted node to the AVAIL list] Set LINK[TEMP := AVAIL and AVAIL := TEMP 5. Exit
  • 10. Applications of Stack : There are two main applications of stack: 1. Infix to Postfix 2. Evaluation of Postfix Expression 3.
  • 11. 1. Infix to Postfix Infix : The operator written in between the operands. Eg. A+B Prefix : The notation in which the operator is written before the operands, it is called Prefix. Eg. +AB Postfix: The notation in which the operator is written after the operands, it is called Postfix. Eg. AB+
  • 12. Infix to Postfix: Algorithm POLISH(Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. 1. Push “(“ onto stack, and add “)” to the end of Q. 2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the STACK is empty: 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator is encountered, then: 1. Repeatedly POP from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than operator.
  • 13. 2. Add operator to stack. [End of If Structure] 6. If a right parenthesis is encountered, then : 1. Repeatedly POP from stack and add to P each operator (on the top of stack) until a left parenthesis is encountered. 2. Remove the left parenthesis. [Do not add to P.] [End of If Structure] [End of step 2 loop] 6. Exit
  • 14. 1. A+(B*C-(D/E^F)*G)*G Symbol scanned STACK POSTFIX A ( A + (+ A ( (+( A B (+( AB * (+(* AB C (+(* ABC - (+(- ABC* ( (+(-( ABC* D (+(-( ABC*D / (+(-(/ ABC*D E (+(-(/ ABC*DE
  • 15. ^ (+(-(/^ ABC*DE F (+(-(/^ ABC*DEF ) (+(- ABC*DEF^/ * (+(-* ABC*DEF^/ G (+(-* ABC*DEF^/G ) (+ ABC*DEF^/G*- * (+* ABC*DEF^/G*- H (+* ABC*DEF^/G*-H ) ABC*DEF^/G*-H*+
  • 16. 2. (A+B) * C/D+E^F/G 3. A-N/(C*D^E) 4. A+B/C-D
  • 17. 2. Evaluating Postfix Notation • Use a stack to evaluate an expression in postfix notation. • The postfix expression to be evaluated is scanned from left to right. • Variables or constants are pushed onto the stack. • When an operator is encountered, the indicated action is performed using the top elements of the stack, and the result replaces the operands on the stack.
  • 18. Postfix expressions: Algorithm using stacks (cont.)
  • 19. Algorithm for evaluating a postfix expression WHILE more input items exist { If symb is an operand then push (opndstk,symb) else //symbol is an operator { Opnd1=pop(opndstk); Opnd2=pop(opndnstk); Value = result of applying symb to opnd1 & opnd2 Push(opndstk,value); } //End of else } // end while Result = pop (opndstk);