SlideShare a Scribd company logo
1 of 23
Download to read offline
STACKS
CREATED BY: SUCHISMITA MAHATO
NIT, JAMSHEDPUR
INTRODUCTION
08-04-2022
2
NIT, Jamshedpur
• It is a non-primitive data structure.
• It is an ordered list in which addition of new data item and deletion of already existing
data item is done from only one end known as top of stack (TOS).
• As all the deletion and insertion in a stack is done fromTOS, the last added element will
be the first to be removed from the stack.
• This is the reason why stack is also called Last-in-First-Out (LIFO) type of list.
• Note that the most frequently accessible element in the stack is the topmost element,
whereas the last accessible element is the bottom of the stack.
STACKS
08-04-2022
3
NIT, Jamshedpur
LIFO PRINCIPLE OF STACK
08-04-2022
4
NIT, Jamshedpur
STACK IMPLEMENTATION
• Stacks can be implemented in two ways:
• Static implementation: It uses arrays to create stack. Though a very
simple technique, but is not a flexible way of creation as the size of
stack has to be declared during program design, after that the size
cannot be varied.
• Dynamic implementation: It is also called linked list representation and
uses pointers to implement the stack type of data structure.
08-04-2022
5
NIT, Jamshedpur
OPERATIONS ON STACK
• PUSH:
• The process of adding a new element to the top of stack is called
PUSH operation.
• Pushing an element in the stack invoke adding of element as the new
element will be inserted at the top.
• After every push operation, the top is incremented by 1.
• In case the array is full and no new element can be accommodated, it
is called STACK-FULL condition and is also known as STACK
OVERFLOW.
08-04-2022
6
NIT, Jamshedpur
OPERATIONS ON STACK
• POP:
• The process of deleting an element from the top of stack is called
POP operation.
• After every pop operation, the stack is decremented by 1.
• If there is no element on the stack and the pop is performed, it will
result into STACK UNDERFLOW condition.
08-04-2022
7
NIT, Jamshedpur
STACKTERMINOLOGY
08-04-2022
8
NIT, Jamshedpur
• Context: The environment is which a function executes.
• Stack frames: The data structure containing all the data needed
each time a procedure or function is called.
• MAXSIZE: Maximum size of the stack.
• TOP: It is used to check stack overflow or underflow conditions.
Initially Top stores -1. This assumption is taken so that whenever an
element is added to the stack, the Top is first incremented and then
the item is inserted into the location currently indicated by Top.
STACKTERMINOLOGY
08-04-2022
9
NIT, Jamshedpur
• STACK: It is an array of size MAXSIZE.
• Stack Underflow: This is the situation when the stack contains no
element. At this point, the top of stack is present at the bottom of
the stack.
• Stack Overflow: This is the situation when the stack becomes full
and no more elements can be pushed onto the stack. At this point,
the stack top is present at the highest location of the stack.
ALGORITHM FOR PUSH(STACK[MAXSIZE], ITEM)
08-04-2022
10
NIT, Jamshedpur
1. Initialize:
set top = -1
2. Repeat steps 3 to 5 until top<MAXSIZE – 1
3. Read Item
4. set top = top + 1
5. set stack[top] = item
6. Print “Stack Overflow”
ALGORITHM FOR POP(STACK[MAXSIZE], ITEM)
08-04-2022
11
NIT, Jamshedpur
1. Repeat steps 2 to 4 until top >= 0
2. set Item = stack[top]
3. set top = top - 1
4. print the item deleted
5. Print “Stack Underflow”
APPLICATIONS OF STACKS
08-04-2022
12
NIT, Jamshedpur
• Evaluation of Arithmetic Expressions
• Backtracking
• Delimiter Checking
• Reverse a Data
• Processing Function Calls
EVALUATION OF ARITHMETIC OPERATIONS
08-04-2022
13
NIT, Jamshedpur
• A stack is a very effective data structure for evaluating arithmetic
expressions in programming languages.
• An arithmetic expression consists of operands and operators.
• In addition to operands and operators, the arithmetic expression may also
include parenthesis like "left parenthesis" and "right parenthesis".
• Example: A + (B - C)
• To evaluate the expressions, one needs to be aware of the standard
precedence rules for arithmetic expression.
EVALUATION OF ARITHMETIC OPERATIONS
08-04-2022
14
NIT, Jamshedpur
• The precedence rules for the five basic arithmetic operators are:
• Evaluation of Arithmetic Expression requires two steps:
• First, convert the given expression into special notation.
• Evaluate the expression in this new notation.
Operators Associativity Precedence
^ exponentiation Right to left Highest followed by *Multiplication and /division
*Multiplication, /division Left to right Highest followed by + addition and -
subtraction
+ addition, - subtraction Left to right lowest
NOTATIONS FOR ARITHMETIC EXPRESSION
08-04-2022
15
NIT, Jamshedpur
• There are three notations to represent an arithmetic expression:
• Infix Notation: The infix notation is a convenient way of writing an expression in which each operator is placed
between the operands. Infix expressions can be parenthesized or unparenthesized depending upon the problem
requirement.
• Example: A + B, (C - D) etc.
• All these expressions are in infix notation because the operator comes between the operands.
• Prefix Notation: The prefix notation places the operator before the operands. This notation was introduced by
the Polish mathematician and hence often referred to as polish notation.
• Example: + AB, -CD etc.
• All these expressions are in prefix notation because the operator comes before the operands.
• Postfix Notation: The postfix notation places the operator after the operands. This notation is just the reverse
of Polish notation and also known as Reverse Polish notation.
• Example: AB +, CD+, etc.
• All these expressions are in postfix notation because the operator comes after the operands.
CONVERSION OF ARITHMETIC EXPRESSION INTO
VARIOUS NOTATIONS
08-04-2022
16
NIT, Jamshedpur
Infix Notation Prefix Notation Postfix Notation
A * B * A B AB*
(A+B)/C /+ ABC AB+C/
(A*B) + (D-C) +*AB - DC AB*DC-+
EVALUATING POSTFIX EXPRESSION
• Stack is the ideal data structure to evaluate the postfix expression because the
top element is always the most recent operand.
• The next element on the Stack is the second most recent operand to be
operated on.
• Before evaluating the postfix expression, the following conditions must be
checked. If any one of the conditions fails, the postfix expression is invalid.
• When an operator encounters the scanning process, the Stack must contain a pair of
operands or intermediate results previously calculated.
• When an expression has been completely evaluated, the Stack must contain exactly one
value.
08-04-2022
17
NIT, Jamshedpur
EVALUATING POSTFIX EXPRESSION
• Now let us consider the following infix
expression 2 * (4+3) - 5.
• Its equivalent postfix expression is 2 4 3 + *
5.
08-04-2022
18
NIT, Jamshedpur
REVERSING A DATA
08-04-2022
19
NIT, Jamshedpur
• To reverse a given set of data, we need to reorder the data so that the
first and last elements are exchanged, the second and second last element
are exchanged, and so on for all other elements.
• Example: Suppose we have a string Welcome, then on reversing it would
be Emoclew.
• There are different reversing applications:
• Reversing a string
• Converting Decimal to Binary
REVERSING A STRING
• A Stack can be used to reverse the characters of a string.
• This can be achieved by simply pushing one by one each character
onto the Stack, which later can be popped from the Stack one by one.
• Because of the last in first out property of the Stack, the first
character of the Stack is on the bottom of the Stack and the last
character of the String is on the Top of the Stack and after performing
the pop operation in the Stack, the Stack returns the String in Reverse
order.
08-04-2022
20
NIT, Jamshedpur
REVERSING A STRING
08-04-2022
21
NIT, Jamshedpur
CONVERTING DECIMAL TO BINARY
08-04-2022
22
NIT, Jamshedpur
• Although decimal numbers are used in most business applications, some scientific and
technical applications require numbers in either binary, octal, or hexadecimal.
• A stack can be used to convert a number from decimal to binary/octal/hexadecimal form.
• For converting any decimal number to a binary number, we repeatedly divide the decimal
number by two and push the remainder of each division onto the Stack until the number
is reduced to 0.
• Then we pop the whole Stack and the result obtained is the binary equivalent of the
given decimal number.
• Example: Converting 14 number Decimal to Binary
CONVERTING DECIMAL TO BINARY
08-04-2022
23
NIT, Jamshedpur

More Related Content

Similar to sweetu stacks.pdf

STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxLECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSKUP1
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computerabinathsabi
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.pptmrizwan38
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxEmroz Sardar
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
DSA-Day-2-PS.pptx
DSA-Day-2-PS.pptxDSA-Day-2-PS.pptx
DSA-Day-2-PS.pptxamanbhogal7
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesFerdin Joe John Joseph PhD
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queueSunipa Bera
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++Khushal Mehta
 
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
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptxDdushb
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 

Similar to sweetu stacks.pdf (20)

STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Stack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptxStack Data Structure with Static Implementation (2).pptx
Stack Data Structure with Static Implementation (2).pptx
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
DSA-Day-2-PS.pptx
DSA-Day-2-PS.pptxDSA-Day-2-PS.pptx
DSA-Day-2-PS.pptx
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 
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
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Stacks
StacksStacks
Stacks
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
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
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
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...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

sweetu stacks.pdf

  • 1. STACKS CREATED BY: SUCHISMITA MAHATO NIT, JAMSHEDPUR
  • 2. INTRODUCTION 08-04-2022 2 NIT, Jamshedpur • It is a non-primitive data structure. • It is an ordered list in which addition of new data item and deletion of already existing data item is done from only one end known as top of stack (TOS). • As all the deletion and insertion in a stack is done fromTOS, the last added element will be the first to be removed from the stack. • This is the reason why stack is also called Last-in-First-Out (LIFO) type of list. • Note that the most frequently accessible element in the stack is the topmost element, whereas the last accessible element is the bottom of the stack.
  • 4. LIFO PRINCIPLE OF STACK 08-04-2022 4 NIT, Jamshedpur
  • 5. STACK IMPLEMENTATION • Stacks can be implemented in two ways: • Static implementation: It uses arrays to create stack. Though a very simple technique, but is not a flexible way of creation as the size of stack has to be declared during program design, after that the size cannot be varied. • Dynamic implementation: It is also called linked list representation and uses pointers to implement the stack type of data structure. 08-04-2022 5 NIT, Jamshedpur
  • 6. OPERATIONS ON STACK • PUSH: • The process of adding a new element to the top of stack is called PUSH operation. • Pushing an element in the stack invoke adding of element as the new element will be inserted at the top. • After every push operation, the top is incremented by 1. • In case the array is full and no new element can be accommodated, it is called STACK-FULL condition and is also known as STACK OVERFLOW. 08-04-2022 6 NIT, Jamshedpur
  • 7. OPERATIONS ON STACK • POP: • The process of deleting an element from the top of stack is called POP operation. • After every pop operation, the stack is decremented by 1. • If there is no element on the stack and the pop is performed, it will result into STACK UNDERFLOW condition. 08-04-2022 7 NIT, Jamshedpur
  • 8. STACKTERMINOLOGY 08-04-2022 8 NIT, Jamshedpur • Context: The environment is which a function executes. • Stack frames: The data structure containing all the data needed each time a procedure or function is called. • MAXSIZE: Maximum size of the stack. • TOP: It is used to check stack overflow or underflow conditions. Initially Top stores -1. This assumption is taken so that whenever an element is added to the stack, the Top is first incremented and then the item is inserted into the location currently indicated by Top.
  • 9. STACKTERMINOLOGY 08-04-2022 9 NIT, Jamshedpur • STACK: It is an array of size MAXSIZE. • Stack Underflow: This is the situation when the stack contains no element. At this point, the top of stack is present at the bottom of the stack. • Stack Overflow: This is the situation when the stack becomes full and no more elements can be pushed onto the stack. At this point, the stack top is present at the highest location of the stack.
  • 10. ALGORITHM FOR PUSH(STACK[MAXSIZE], ITEM) 08-04-2022 10 NIT, Jamshedpur 1. Initialize: set top = -1 2. Repeat steps 3 to 5 until top<MAXSIZE – 1 3. Read Item 4. set top = top + 1 5. set stack[top] = item 6. Print “Stack Overflow”
  • 11. ALGORITHM FOR POP(STACK[MAXSIZE], ITEM) 08-04-2022 11 NIT, Jamshedpur 1. Repeat steps 2 to 4 until top >= 0 2. set Item = stack[top] 3. set top = top - 1 4. print the item deleted 5. Print “Stack Underflow”
  • 12. APPLICATIONS OF STACKS 08-04-2022 12 NIT, Jamshedpur • Evaluation of Arithmetic Expressions • Backtracking • Delimiter Checking • Reverse a Data • Processing Function Calls
  • 13. EVALUATION OF ARITHMETIC OPERATIONS 08-04-2022 13 NIT, Jamshedpur • A stack is a very effective data structure for evaluating arithmetic expressions in programming languages. • An arithmetic expression consists of operands and operators. • In addition to operands and operators, the arithmetic expression may also include parenthesis like "left parenthesis" and "right parenthesis". • Example: A + (B - C) • To evaluate the expressions, one needs to be aware of the standard precedence rules for arithmetic expression.
  • 14. EVALUATION OF ARITHMETIC OPERATIONS 08-04-2022 14 NIT, Jamshedpur • The precedence rules for the five basic arithmetic operators are: • Evaluation of Arithmetic Expression requires two steps: • First, convert the given expression into special notation. • Evaluate the expression in this new notation. Operators Associativity Precedence ^ exponentiation Right to left Highest followed by *Multiplication and /division *Multiplication, /division Left to right Highest followed by + addition and - subtraction + addition, - subtraction Left to right lowest
  • 15. NOTATIONS FOR ARITHMETIC EXPRESSION 08-04-2022 15 NIT, Jamshedpur • There are three notations to represent an arithmetic expression: • Infix Notation: The infix notation is a convenient way of writing an expression in which each operator is placed between the operands. Infix expressions can be parenthesized or unparenthesized depending upon the problem requirement. • Example: A + B, (C - D) etc. • All these expressions are in infix notation because the operator comes between the operands. • Prefix Notation: The prefix notation places the operator before the operands. This notation was introduced by the Polish mathematician and hence often referred to as polish notation. • Example: + AB, -CD etc. • All these expressions are in prefix notation because the operator comes before the operands. • Postfix Notation: The postfix notation places the operator after the operands. This notation is just the reverse of Polish notation and also known as Reverse Polish notation. • Example: AB +, CD+, etc. • All these expressions are in postfix notation because the operator comes after the operands.
  • 16. CONVERSION OF ARITHMETIC EXPRESSION INTO VARIOUS NOTATIONS 08-04-2022 16 NIT, Jamshedpur Infix Notation Prefix Notation Postfix Notation A * B * A B AB* (A+B)/C /+ ABC AB+C/ (A*B) + (D-C) +*AB - DC AB*DC-+
  • 17. EVALUATING POSTFIX EXPRESSION • Stack is the ideal data structure to evaluate the postfix expression because the top element is always the most recent operand. • The next element on the Stack is the second most recent operand to be operated on. • Before evaluating the postfix expression, the following conditions must be checked. If any one of the conditions fails, the postfix expression is invalid. • When an operator encounters the scanning process, the Stack must contain a pair of operands or intermediate results previously calculated. • When an expression has been completely evaluated, the Stack must contain exactly one value. 08-04-2022 17 NIT, Jamshedpur
  • 18. EVALUATING POSTFIX EXPRESSION • Now let us consider the following infix expression 2 * (4+3) - 5. • Its equivalent postfix expression is 2 4 3 + * 5. 08-04-2022 18 NIT, Jamshedpur
  • 19. REVERSING A DATA 08-04-2022 19 NIT, Jamshedpur • To reverse a given set of data, we need to reorder the data so that the first and last elements are exchanged, the second and second last element are exchanged, and so on for all other elements. • Example: Suppose we have a string Welcome, then on reversing it would be Emoclew. • There are different reversing applications: • Reversing a string • Converting Decimal to Binary
  • 20. REVERSING A STRING • A Stack can be used to reverse the characters of a string. • This can be achieved by simply pushing one by one each character onto the Stack, which later can be popped from the Stack one by one. • Because of the last in first out property of the Stack, the first character of the Stack is on the bottom of the Stack and the last character of the String is on the Top of the Stack and after performing the pop operation in the Stack, the Stack returns the String in Reverse order. 08-04-2022 20 NIT, Jamshedpur
  • 22. CONVERTING DECIMAL TO BINARY 08-04-2022 22 NIT, Jamshedpur • Although decimal numbers are used in most business applications, some scientific and technical applications require numbers in either binary, octal, or hexadecimal. • A stack can be used to convert a number from decimal to binary/octal/hexadecimal form. • For converting any decimal number to a binary number, we repeatedly divide the decimal number by two and push the remainder of each division onto the Stack until the number is reduced to 0. • Then we pop the whole Stack and the result obtained is the binary equivalent of the given decimal number. • Example: Converting 14 number Decimal to Binary
  • 23. CONVERTING DECIMAL TO BINARY 08-04-2022 23 NIT, Jamshedpur