SlideShare a Scribd company logo
1 of 6
Chapter 2 (Part 1)
Stack
2.1 Introduction to Stack
Stack is a data structure in which new items may be added or
existing items may be removed from one end. It follows Last In
First Out (LIFO) principle. The end from which addition and
deletion of data items is performed is known as top of stack.
2.2 Stack Operations
2.2.1 Push
The push operation adds an item to the top of the stack. The
push operation is not possible only if a limit on number of items
in the stack is specified and the stack is full.
2.2.2 Pop
The pop operation removes an item that is present at the top of
the stack. The stack with no items is known as empty stack. The
pop operation is not possible in an empty stack.
2.2.3 StackTop
The stackTop operation determines what the top item on a stack
is without removing the item. This operation is not defined for
empty stack.
2.3 ADT for Stack
abstract typedef <<data_type>> STACK (data_type);
abstract is_empty(s)
STACK(data_type) s;
postcondition empty == (len(s) == 0);
abstract data_type pop(s)
STACK(data_type) s;
precondition is_empty(s) == FALSE;
postconsition pop == top(s);
s == sub(s, 1, len(s)-1)
abstract push(s, data)
STACK s;
data_type data;
postcondition s = s + <data>
2.4 Algorithm for Stack Operations
Consider a stack (named as Stack) with the end from which
addition and removal of data items can be done be ‘top’. Let us
assume that the stack is of fixed size given by size.
2.4.1 Check for empty stack
1. Start
2. Is top == -1?
a) Yes : status = TRUE
b) No : status = FALSE
3. Return status
4. Stop
2.4.2 PUSH Operation
1. Start
2. Get an item to be pushed (lets say i)
3. Is top > size -1?
a) Yes : display “Stack is full” and goto step 6
b) No : goto step 4
4. Top = top + 1
5. Stack[top] = i
6. Stop
2.4.3 POP Operation
1. Start
2. Check for empty stack?
a) Yes : display “Stack is empty” and goto step 5
3. Pop_item = stack[top]
4. Top = top - 1
5. Stop
2.5 Static and Dynamic Implementation of Stack
Static Implementation Dynamic Implementation
Array is used to create stack. Pointer is used to create stack.
Simple to create. Complex
Size of stack is declared at the
beginning.
Size of stack is not declared at
the beginning.
The stack is of fixed size. The stack is of variable size.
2.6 Prefix, Infix and Postfix Notation
Prefix notation specifies that the operator precedes the two
operands. It is also known as polish notation. Eg : +AB
Infix notation specifies that the operator is in between the two
operands. Eg : A + B
Postfix notation specifies that the operator follows the two
operands. It is also known as reverse polish notation. Eg : AB+
2.7 Importance of Postfix Notation
- The order of execution of postfix expression is always from
left to right and uses stack. It do not need to analyze any
precedence rule. This made computation of expression
easy.
- Calculation is faster due to no need of parenthesis leading
to less number of operations.
2.8 Conversion from Infix to Postfix Expression
While (token present to read):
Read a token
If (token is number):
Push it to the output queue
If (token is a function):
Push it onto the operator stack
If (token is an operator):
While ((function at top of operator stack) or (operator at top of operator stack
with high precedence) or (operator at top of operator stack with equal precedence and left
associative)) and (operator at top of operator stack is not a left bracket) :
Pop operators from the operator stack onto the output queue
Push the token onto the operator stack
If (token is left bracket “(“ ) :
Push it onto operator stack
If (token is right bracket “)” ) :
While (operator at top of operator stack is not a left bracket):
Pop operator from operator stack onto the output queue
Pop left bracket from the stack
If (no token present to read) :
While (operator tokens present on the stack) :
Pop operator from operator stack onto the output queue
2.9 Evaluation of Postfix Expression
For each token in postfix expression:
If (token is operator) :
Operand2 = pop from stack
Operand1 = pop from stack
Result = evaluate token with operand1 and operand2
Push result back onto stack
Else if (token is operand) :
Push token onto stack
Result = pop from stack

More Related Content

What's hot (20)

stack presentation
stack presentationstack presentation
stack presentation
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Stack
StackStack
Stack
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Linked list
Linked listLinked list
Linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Stacks
StacksStacks
Stacks
 
Stack project
Stack projectStack project
Stack project
 
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
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked List
Linked ListLinked List
Linked List
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 

Similar to Stack - Operations and Applications

Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptxHalid Assen
 
In C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docxIn C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docxtristans3
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
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
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxmareeswari15
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptxskilljiolms
 
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
 

Similar to Stack - Operations and Applications (20)

Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
In C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docxIn C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docx
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
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
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
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
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
04 stacks
04 stacks04 stacks
04 stacks
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 

More from Sagacious IT Solution

More from Sagacious IT Solution (16)

List - Operations and Implementation
List - Operations and ImplementationList - Operations and Implementation
List - Operations and Implementation
 
Introduction to Data Structure and Algorithm
Introduction to Data Structure and AlgorithmIntroduction to Data Structure and Algorithm
Introduction to Data Structure and Algorithm
 
Queue - Operations and Implementations
Queue - Operations and ImplementationsQueue - Operations and Implementations
Queue - Operations and Implementations
 
Neural Networks
Neural NetworksNeural Networks
Neural Networks
 
Machine Learning Algorithms
Machine Learning AlgorithmsMachine Learning Algorithms
Machine Learning Algorithms
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
 
Expert System
Expert SystemExpert System
Expert System
 
Game Playing Search Techniques - Examples
Game Playing Search Techniques - ExamplesGame Playing Search Techniques - Examples
Game Playing Search Techniques - Examples
 
Uninformed Search Examples
Uninformed Search ExamplesUninformed Search Examples
Uninformed Search Examples
 
Examples of Informed Search
Examples of Informed SearchExamples of Informed Search
Examples of Informed Search
 
Searching Algorithm
Searching AlgorithmSearching Algorithm
Searching Algorithm
 
Knowledge Representation, Inference and Reasoning
Knowledge Representation, Inference and ReasoningKnowledge Representation, Inference and Reasoning
Knowledge Representation, Inference and Reasoning
 
Structured Knowledge Representation
Structured Knowledge RepresentationStructured Knowledge Representation
Structured Knowledge Representation
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Crypto Arithmetic Problem - Example
Crypto Arithmetic Problem - ExampleCrypto Arithmetic Problem - Example
Crypto Arithmetic Problem - Example
 
Introduction To Artificial Intelligence
Introduction To Artificial IntelligenceIntroduction To Artificial Intelligence
Introduction To Artificial Intelligence
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
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
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
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
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
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
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
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
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
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
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.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...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
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...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.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
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
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
 

Stack - Operations and Applications

  • 1. Chapter 2 (Part 1) Stack 2.1 Introduction to Stack Stack is a data structure in which new items may be added or existing items may be removed from one end. It follows Last In First Out (LIFO) principle. The end from which addition and deletion of data items is performed is known as top of stack. 2.2 Stack Operations 2.2.1 Push The push operation adds an item to the top of the stack. The push operation is not possible only if a limit on number of items in the stack is specified and the stack is full. 2.2.2 Pop The pop operation removes an item that is present at the top of the stack. The stack with no items is known as empty stack. The pop operation is not possible in an empty stack. 2.2.3 StackTop
  • 2. The stackTop operation determines what the top item on a stack is without removing the item. This operation is not defined for empty stack. 2.3 ADT for Stack abstract typedef <<data_type>> STACK (data_type); abstract is_empty(s) STACK(data_type) s; postcondition empty == (len(s) == 0); abstract data_type pop(s) STACK(data_type) s; precondition is_empty(s) == FALSE; postconsition pop == top(s); s == sub(s, 1, len(s)-1) abstract push(s, data) STACK s; data_type data; postcondition s = s + <data> 2.4 Algorithm for Stack Operations Consider a stack (named as Stack) with the end from which addition and removal of data items can be done be ‘top’. Let us assume that the stack is of fixed size given by size. 2.4.1 Check for empty stack 1. Start 2. Is top == -1? a) Yes : status = TRUE b) No : status = FALSE
  • 3. 3. Return status 4. Stop 2.4.2 PUSH Operation 1. Start 2. Get an item to be pushed (lets say i) 3. Is top > size -1? a) Yes : display “Stack is full” and goto step 6 b) No : goto step 4 4. Top = top + 1 5. Stack[top] = i 6. Stop 2.4.3 POP Operation 1. Start 2. Check for empty stack? a) Yes : display “Stack is empty” and goto step 5 3. Pop_item = stack[top] 4. Top = top - 1 5. Stop 2.5 Static and Dynamic Implementation of Stack
  • 4. Static Implementation Dynamic Implementation Array is used to create stack. Pointer is used to create stack. Simple to create. Complex Size of stack is declared at the beginning. Size of stack is not declared at the beginning. The stack is of fixed size. The stack is of variable size. 2.6 Prefix, Infix and Postfix Notation Prefix notation specifies that the operator precedes the two operands. It is also known as polish notation. Eg : +AB Infix notation specifies that the operator is in between the two operands. Eg : A + B Postfix notation specifies that the operator follows the two operands. It is also known as reverse polish notation. Eg : AB+ 2.7 Importance of Postfix Notation - The order of execution of postfix expression is always from left to right and uses stack. It do not need to analyze any precedence rule. This made computation of expression easy. - Calculation is faster due to no need of parenthesis leading to less number of operations. 2.8 Conversion from Infix to Postfix Expression
  • 5. While (token present to read): Read a token If (token is number): Push it to the output queue If (token is a function): Push it onto the operator stack If (token is an operator): While ((function at top of operator stack) or (operator at top of operator stack with high precedence) or (operator at top of operator stack with equal precedence and left associative)) and (operator at top of operator stack is not a left bracket) : Pop operators from the operator stack onto the output queue Push the token onto the operator stack If (token is left bracket “(“ ) : Push it onto operator stack If (token is right bracket “)” ) : While (operator at top of operator stack is not a left bracket): Pop operator from operator stack onto the output queue Pop left bracket from the stack If (no token present to read) : While (operator tokens present on the stack) : Pop operator from operator stack onto the output queue 2.9 Evaluation of Postfix Expression For each token in postfix expression: If (token is operator) :
  • 6. Operand2 = pop from stack Operand1 = pop from stack Result = evaluate token with operand1 and operand2 Push result back onto stack Else if (token is operand) : Push token onto stack Result = pop from stack