Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3
Definition of Stacks Operators for Stacks Push Pop Peek Applications for Stacks Reversing a Word Delimiter Matching Array-based Stack
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
Fig1. Bullets Clip: A Typical Stack
You can only eat the top apple first Fig.2 An Apple Stack
Push: insert a data item to the top of the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Push Operator in Stacks Bottom Top
Program Pop: get a data item on the top of the stack and remove it from the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Pop Operator in Stacks Bottom Top
Program Peek: get a data item on the top of the stack and do not remove it from the stack Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Fig2. Pop Operator in Stacks Bottom Item 6 Top
Stacks are typically small, temporal data structures. Can be calculated by the top pointer and the bottom pointer
Output Input Reversing a Word “Avatar” -> “ratavA” A A v v a a t t a a r r BottomTop
11 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 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
Delimiter Matching ( ) * ( ) Stack: 9 ( + 1 ) (9+1)=10 10 * ( ) (6+2)=8 8 10*8=80 80 = 80
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
How is stack implemented? Initiate an array Use variables to indentify the position of the top of the stack A[10]top=-1 Pop() Push(1) Push(8) Push(3) Pop() 3 top=0 1 top=1 8
Elementary Data Structures16 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 S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t t 1 return S[t + 1]
Elementary Data Structures17 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 S 012 t … Algorithm push(o) if t = S.length 1 then throw FullStackException else t t + 1 S[t] o
Elementary Data Structures18 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) if t = S.length 1 then A new array of size … for i 0 to t do A[i] S[i] S A t t + 1 S[t] o
Definition of Stacks Operators for Stacks Push Pop Peek Applications for Stacks Reversing a Word Delimiter Matching Array-based Stack