200 likes | 509 Views
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.
E N D
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 • 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
Analogy of Stack Fig1. Bullets Clip: A Typical Stack
Analogy of Stack 2 • You can only eat the top apple first Fig.2 An Apple Stack
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
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
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
Stack Size • Stacks are typically small, temporal data structures. • Can be calculated by the top pointer and the bottom pointer
Application for Stacks 1: • Reversing a Word • “Avatar” -> “ratavA” Output Input r a Top t a v A Bottom
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?
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
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 (
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
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
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 tt 1 returnS[t + 1] … S 0 1 2 t Elementary Data Structures
… 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 tt + 1 S[t] o Elementary Data Structures
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 … fori0tot do A[i] S[i] S A t t + 1 S[t] o Elementary Data Structures
Summary • Definition of Stacks • Operators for Stacks • Push • Pop • Peek • Applications for Stacks • Reversing a Word • Delimiter Matching • Array-based Stack