Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3."— Presentation transcript:

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

2  Definition of Stacks  Operators for Stacks  Push  Pop  Peek  Applications for Stacks  Reversing a Word  Delimiter Matching  Array-based Stack

3  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 Fig1. Bullets Clip: A Typical Stack

5  You can only eat the top apple first Fig.2 An Apple Stack

6  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

7 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

8 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

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

10 Output Input  Reversing a Word  “Avatar” -> “ratavA” A A v v a a t t a a r r BottomTop

11 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 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

13  Delimiter Matching  ( 9 + 1 ) * ( 6 + 2 ) Stack: 9 ( + 1 ) (9+1)=10 10 * ( ) 6 + 2 (6+2)=8 8 10*8=80 80 = 80

14  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  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

16 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]

17 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

18 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

19  Definition of Stacks  Operators for Stacks  Push  Pop  Peek  Applications for Stacks  Reversing a Word  Delimiter Matching  Array-based Stack

20


Download ppt "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3."

Similar presentations


Ads by Google