Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 214 – Computer Science II Stacks

Similar presentations


Presentation on theme: "CSE 214 – Computer Science II Stacks"— Presentation transcript:

1 CSE 214 – Computer Science II Stacks
Source:

2 What’s a stack? Anyone like PEZ? An ordered LIFO data structure
Last In First Out How do they work? stack data on top of each other add data only to the top of the stack take data only from the top of the stack Anyone like PEZ?

3 Anatomy of a PEZ dispenser
Push Push Push Pop Pop Pop Ref:

4 Possible Implementations
How might we store our stack data? in an array (we’ll do this in lecture) in a linked list (you’ll do this in HW 5)

5 Using an array to store a Stack
Push Push Push Pop Pop Pop

6 What array operations might we need?
Add element to end of array elements push Remove and get last element in array pop Examine last element in array peek Examine all the elements in array iterator Note: we may also have to periodically resize our array

7 Using a Linked List to store a Stack
head null head null head null head null head null head null head null

8 What linked list operations might we need?
Append to end push Get from end peek Remove and get end pop Get all elements stackIterator, hasNext, next Bottom Line: Stacks can be efficiently implemented using a linked lists


Download ppt "CSE 214 – Computer Science II Stacks"

Similar presentations


Ads by Google