1 / 20

Stacks

Data Structure: Chapter 3. Min Chen School of Computer Science and Engineering Seoul National University. Stacks. Content. Definition of Stacks Operators for Stacks Push Pop Peek Applications for Stacks Reversing a Word Delimiter Matching Array-based Stack. Definition of Stacks.

candid
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure: Chapter 3 Min Chen School of Computer Science and Engineering Seoul National University Stacks

  2. Content • Definition of Stacks • Operators for Stacks • Push • Pop • Peek • Applications for Stacks • Reversing a Word • Delimiter Matching • Array-based Stack

  3. Definition of Stacks • A stack is a Last-In-First-Out(LIFO) abstract data structure. • In a stack, access to a element is restricted: only the top item can be read or removed at one time Top Bottom

  4. Analogy of Stack Fig1. Bullets Clip: A Typical Stack

  5. Analogy of Stack 2 • You can only eat the top apple first Fig.2 An Apple Stack

  6. Operators for Stacks: Push • Push: insert a data item to the top of the stack Item 6 Top Item 5 Item 4 Item 3 Item 2 Item 1 Bottom Fig2. Push Operator in Stacks

  7. Operators for Stacks: Pop • Pop: get a data item on the top of the stack and remove it from the stack Item 6 Program Top Item 5 Item 4 Item 3 Item 2 Item 1 Bottom Fig2. Pop Operator in Stacks

  8. Operators for Stacks: Peek • Peek: get a data item on the top of the stack and do not remove it from the stack Item 6 Item 6 Program Top Item 5 Item 4 Item 3 Item 2 Item 1 Bottom Fig2. Pop Operator in Stacks

  9. Stack Size • Stacks are typically small, temporal data structures. • Can be calculated by the top pointer and the bottom pointer

  10. Application for Stacks 1: • Reversing a Word • “Avatar” -> “ratavA” Output Input r a Top t a v A Bottom

  11. Application for Stacks 2: Delimiter Matching ( Balanced Symbol Checking ) • In processing programs and working with computer languages there are many instances when symbols must be balanced { } , [ ] , ( ) A stack is useful for checking symbol balance. When a closing symbol is found it must match the most recent opening symbol of the same type. Algorithm?

  12. Algorithm for Balanced Symbol Checking • Make an empty stack • read symbols until end of file • if the symbol is an opening symbol push it onto the stack • if it is a closing symbol do the following • pop the stack. If the symbol popped does not match the closing symbol report an error • At the end of the file if the stack is not empty report an error

  13. Example: • Delimiter Matching • ( 9 + 1 ) * ( 6 + 2 ) = 80 Stack: ) 2 + ) (9+1)=10 6 1 (6+2)=8 + 8 ( 10*8=80 9 * 10 80 (

  14. Efficiency of Stacks • Items can be both pushed and popped from a stack in constant O(1) time • We have no choices but just operate on the particular item (top item) • Very Quick

  15. Array-based Stack • How is stack implemented? • Initiate an array • Use variables to indentify the position of the top of the stack Push(3) A[10] top=-1 top=0 top=1 Pop() Push(1) Push(8) Pop() 3 1 8

  16. A simple way of implementing the Stack uses an array We add elements from left to right A variable keeps track of the index of the top element Array-based Stack Algorithm size() returnt + 1 Algorithmpop() ifisEmpty()then throw EmptyStackException else tt  1 returnS[t + 1] … S 0 1 2 t Elementary Data Structures

  17. S 0 1 2 t Array-based Stack (cont.) • The array storing the stack elements may become full • A push operation will then throw a FullStackException • Limitation of the array-based implementation • Not intrinsic to the Stack Algorithmpush(o) ift=S.length  1then throw FullStackException else tt + 1 S[t] o Elementary Data Structures

  18. Growable Array-based Stack • In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one • How large should the new array be? • incremental strategy: increase the size by a constant c • doubling strategy: double the size Algorithm push(o) ift=S.length  1 then A new array of size … fori0tot do A[i] S[i] S A t t + 1 S[t] o Elementary Data Structures

  19. Summary • Definition of Stacks • Operators for Stacks • Push • Pop • Peek • Applications for Stacks • Reversing a Word • Delimiter Matching • Array-based Stack

  20. Thank you!

More Related