SlideShare uma empresa Scribd logo
1 de 37
Stack Data structure
1
ILOs
 The end of this session, student will be able to,
 Define what is stack data structure
 Explain the LIFO technique with real world example
 Explain the primary operation on stack data structure
 Draw a diagram for a given primary operation on stack
 Explain what are prefix, infix and postfix operation for a given algorithm
 Apply the stack data structure to convert the infix formula to postfix and so on.
2
Introduction
Stacks and queues are the two data structures where
insert and delete operations are applied at specific
ends only.
These are special cases of ordered lists and are also
called controlled linear lists.
3
Introduction…
There is a wide variety of software applications where
we need these restricted data structure operations.
The following are some examples where stacks and
queues are generally used:
4
Introduction…
 used in "undo" mechanism in text editor.
 Language processing:
 space for parameters and local variables is created internally using a stack.
 compiler's syntax check for matching braces is implemented by using stack.
 support for recursion.
 Expression evaluation like postfix or prefix in compilers.
 3. Backtracking (game playing, finding paths, exhaustive searching.
 4. Memory management, run-time environment for nested language
features. etc
5
Introduction…
 Stack is a data structure which is used to
handle data in a last-in-first-out (LIFO)
method. That is we can remove the most
recently added element from the stack first.
6
Introduction…
In our everyday life, we come across many examples of
stacks, for example, a stack of books, a stack of dishes,
or a stack of chairs. The data structure stack is very
similar to these practical examples
7
Introduction…
Consider a stack of books on a table. We can easily
put a new book on the top of the stack, and similarly,
we can easily remove the topmost book as compared
to the books lying in-between or at the bottom
positions.
In the same way, only the topmost element of a stack
can be accessed while direct access of other
intermediate positions is not feasible. Elements may
be added to or removed from only one end, called
the top of a stack.
8
Introduction…
The linear data structures such as arrays and
linked lists allow users to insert or delete an
element at any position in the list, that is, we
can insert or delete an element at the
beginning, at the end, or at any intermediate
position.
9
Primitive Operations
 The three basic stack operations are push, pop, and getTop.
Besides these, there are some more operations that can be
implemented on a stack such as stack_initialization,
stack_empty, and stack_full.
 The stack_initialization operation prepares the stack for use
and sets it to a vacant state. The stack_empty operation
simply tests whether the stack is empty.
 The stack_empty operation is useful as a safeguard against an
attempt to pop an element from an empty stack.
10
Primitive Operations…
 Popping an empty stack is an error condition. The
stack_empty condition is also termed stack underflow.
 Another stack operation is GetTop. This returns the top
element of the stack without actually popping it.
 A few more stack operations include traversing the stack,
counting the total number of elements in the stack, and
copying the stack.
11
Primitive Operations…
1. Push—inserts an element on the top of the stack
2. Pop—deletes an element from the top of the stack
3. GetTop—reads (only reading, not deleting) an element from
the top of the stack
4. Stack_initialization—sets up the stack in an empty condition
5. Empty—checks whether the stack is empty
6. Full—checks whether the stack is full
12
Primitive Operations…
13
14
15
16
17
18
19
20
ALGORITHM OF INSERTION IN STACK: (PUSH)
21
Insertion(a,top,item,max)
If top=max then
print ‘STACK OVERFLOW’
Exit
else
top=top+1 end if
a[top]=item
Exit
ALGORITHM OF DELETION IN STACK: (POP)
22
Deletion(a,top,item)
If top=0 then
print ‘STACK UNDERFLOW’
Exit
else
item=a[top]
end if
top=top-1
Exit
ALGORITHM OF DISPLAY IN STACK:
23
Display(top,i,a[i])
If top=0 then
Print ‘STACK IS EMPTY’
exit
else
For i=top to 0
Print a[i] End for
exit
APPLICATIONS OF STACKS ARE:
Reversing Strings:
A simple application of stack is reversing strings.
To reverse a string , the characters of string are
pushed onto the stack one by one as the string is
read from left to right.
Once all the characters of string are pushed onto
stack, they are popped one by one. Since the
character last pushed in comes out first, subsequent
pop operation results in the reversal of the string.
24
APPLICATIONS OF STACKS ARE:…
To reverse the string ‘REVERSE’ the string is read
from left to right and its characters are pushed .
LIKE:
25
APPLICATIONS OF STACKS ARE:…
Checking the validity of an expression
containing nested parenthesis:
Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
26
APPLICATIONS OF STACKS ARE:…
27
VALID INPUTS INVALID INPUTS
{ } { ( }
( { [ ] } ) ( [ ( ( ) ] )
{ [ ] ( ) } { } [ ] )
[ { ( { } [ ] ( { [ { ) } ( ] } ]
})}]
APPLICATIONS OF STACKS ARE:…
Evaluating arithmetic expressions:
INFIX notation:
The general way of writing arithmetic expressions is
known as infix notation. e.g, (a+b)
PREFIX notation: e.g, +AB
POSTFIX notation: e.g: AB+
28
Infix to Postfix Conversion
Algorithm basics
Scan expression left to right
When operand found, place at end of new
expression
When operator found, save to determine new
position
29
Infix to Postfix Conversion…
1. Operand
 Append to end of output expression
2. Operator ^
 Push ^ onto stack
3. Operators -,+,/,*
i. Pop and append to the postfix string every operator on the stack that
a. is above the most recently scanned left parenthesis, and
b. has precedence higher than or is a right-associative operator
of equal precedence to that of the new operator symbol.
i. ii. Push the new operator onto the stack.
30
Infix to Postfix Conversion…
4. Open parenthesis
 Push ( onto stack)
5. Close parenthesis
 Pop operators from stack and append to output
 Until open parenthesis is popped.
 Discard both parentheses
31
Infix to Postfix…
 Converting the infix expression a + b * c to postfix form
32
Infix to Postfix…
 Converting an infix expression to
postfix form: A*(B*C+D*E)+F
33
Convert the following infix a / b * (c + (d - e)) to
postfix form
34
Infix to Postfix…
 Converting an infix expression to postfix form: A + B * C / D - E
35
Exersice
( A + B * ( C - D ) ) / E
A * B- (C + D) + E
36
Thank You
37

Mais conteúdo relacionado

Semelhante a Stacks Data structure.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
KALPANAC20
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
Ramachandra Adiga G
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 

Semelhante a Stacks Data structure.pptx (20)

Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
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
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
 
stack 1.pdf
stack 1.pdfstack 1.pdf
stack 1.pdf
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack
StackStack
Stack
 
stack & queue
stack & queuestack & queue
stack & queue
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Stack
StackStack
Stack
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stacks
StacksStacks
Stacks
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 

Último

Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Último (20)

Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 

Stacks Data structure.pptx

  • 2. ILOs  The end of this session, student will be able to,  Define what is stack data structure  Explain the LIFO technique with real world example  Explain the primary operation on stack data structure  Draw a diagram for a given primary operation on stack  Explain what are prefix, infix and postfix operation for a given algorithm  Apply the stack data structure to convert the infix formula to postfix and so on. 2
  • 3. Introduction Stacks and queues are the two data structures where insert and delete operations are applied at specific ends only. These are special cases of ordered lists and are also called controlled linear lists. 3
  • 4. Introduction… There is a wide variety of software applications where we need these restricted data structure operations. The following are some examples where stacks and queues are generally used: 4
  • 5. Introduction…  used in "undo" mechanism in text editor.  Language processing:  space for parameters and local variables is created internally using a stack.  compiler's syntax check for matching braces is implemented by using stack.  support for recursion.  Expression evaluation like postfix or prefix in compilers.  3. Backtracking (game playing, finding paths, exhaustive searching.  4. Memory management, run-time environment for nested language features. etc 5
  • 6. Introduction…  Stack is a data structure which is used to handle data in a last-in-first-out (LIFO) method. That is we can remove the most recently added element from the stack first. 6
  • 7. Introduction… In our everyday life, we come across many examples of stacks, for example, a stack of books, a stack of dishes, or a stack of chairs. The data structure stack is very similar to these practical examples 7
  • 8. Introduction… Consider a stack of books on a table. We can easily put a new book on the top of the stack, and similarly, we can easily remove the topmost book as compared to the books lying in-between or at the bottom positions. In the same way, only the topmost element of a stack can be accessed while direct access of other intermediate positions is not feasible. Elements may be added to or removed from only one end, called the top of a stack. 8
  • 9. Introduction… The linear data structures such as arrays and linked lists allow users to insert or delete an element at any position in the list, that is, we can insert or delete an element at the beginning, at the end, or at any intermediate position. 9
  • 10. Primitive Operations  The three basic stack operations are push, pop, and getTop. Besides these, there are some more operations that can be implemented on a stack such as stack_initialization, stack_empty, and stack_full.  The stack_initialization operation prepares the stack for use and sets it to a vacant state. The stack_empty operation simply tests whether the stack is empty.  The stack_empty operation is useful as a safeguard against an attempt to pop an element from an empty stack. 10
  • 11. Primitive Operations…  Popping an empty stack is an error condition. The stack_empty condition is also termed stack underflow.  Another stack operation is GetTop. This returns the top element of the stack without actually popping it.  A few more stack operations include traversing the stack, counting the total number of elements in the stack, and copying the stack. 11
  • 12. Primitive Operations… 1. Push—inserts an element on the top of the stack 2. Pop—deletes an element from the top of the stack 3. GetTop—reads (only reading, not deleting) an element from the top of the stack 4. Stack_initialization—sets up the stack in an empty condition 5. Empty—checks whether the stack is empty 6. Full—checks whether the stack is full 12
  • 14. 14
  • 15. 15
  • 16. 16
  • 17. 17
  • 18. 18
  • 19. 19
  • 20. 20
  • 21. ALGORITHM OF INSERTION IN STACK: (PUSH) 21 Insertion(a,top,item,max) If top=max then print ‘STACK OVERFLOW’ Exit else top=top+1 end if a[top]=item Exit
  • 22. ALGORITHM OF DELETION IN STACK: (POP) 22 Deletion(a,top,item) If top=0 then print ‘STACK UNDERFLOW’ Exit else item=a[top] end if top=top-1 Exit
  • 23. ALGORITHM OF DISPLAY IN STACK: 23 Display(top,i,a[i]) If top=0 then Print ‘STACK IS EMPTY’ exit else For i=top to 0 Print a[i] End for exit
  • 24. APPLICATIONS OF STACKS ARE: Reversing Strings: A simple application of stack is reversing strings. To reverse a string , the characters of string are pushed onto the stack one by one as the string is read from left to right. Once all the characters of string are pushed onto stack, they are popped one by one. Since the character last pushed in comes out first, subsequent pop operation results in the reversal of the string. 24
  • 25. APPLICATIONS OF STACKS ARE:… To reverse the string ‘REVERSE’ the string is read from left to right and its characters are pushed . LIKE: 25
  • 26. APPLICATIONS OF STACKS ARE:… Checking the validity of an expression containing nested parenthesis: Stacks are also used to check whether a given arithmetic expressions containing nested parenthesis is properly parenthesized. 26
  • 27. APPLICATIONS OF STACKS ARE:… 27 VALID INPUTS INVALID INPUTS { } { ( } ( { [ ] } ) ( [ ( ( ) ] ) { [ ] ( ) } { } [ ] ) [ { ( { } [ ] ( { [ { ) } ( ] } ] })}]
  • 28. APPLICATIONS OF STACKS ARE:… Evaluating arithmetic expressions: INFIX notation: The general way of writing arithmetic expressions is known as infix notation. e.g, (a+b) PREFIX notation: e.g, +AB POSTFIX notation: e.g: AB+ 28
  • 29. Infix to Postfix Conversion Algorithm basics Scan expression left to right When operand found, place at end of new expression When operator found, save to determine new position 29
  • 30. Infix to Postfix Conversion… 1. Operand  Append to end of output expression 2. Operator ^  Push ^ onto stack 3. Operators -,+,/,* i. Pop and append to the postfix string every operator on the stack that a. is above the most recently scanned left parenthesis, and b. has precedence higher than or is a right-associative operator of equal precedence to that of the new operator symbol. i. ii. Push the new operator onto the stack. 30
  • 31. Infix to Postfix Conversion… 4. Open parenthesis  Push ( onto stack) 5. Close parenthesis  Pop operators from stack and append to output  Until open parenthesis is popped.  Discard both parentheses 31
  • 32. Infix to Postfix…  Converting the infix expression a + b * c to postfix form 32
  • 33. Infix to Postfix…  Converting an infix expression to postfix form: A*(B*C+D*E)+F 33
  • 34. Convert the following infix a / b * (c + (d - e)) to postfix form 34
  • 35. Infix to Postfix…  Converting an infix expression to postfix form: A + B * C / D - E 35
  • 36. Exersice ( A + B * ( C - D ) ) / E A * B- (C + D) + E 36