Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)

Similar presentations


Presentation on theme: "Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)"— Presentation transcript:

1 Data Structures - Stacks

2 What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey) AddressBook and more!

3 Introducing the Stack LIFO ~ Last In First Out How does a stack of trays/plates work? 1. Start with an empty stack 2. Place tray on top of stack 3. Place another tray on top of the stack 4. Remove tray on top from stack 5. etc. push– data is added to the top of the stack pop – data is removed from the top of the stack isEmpty – returns True if stack is empty

4 Stack Example PUSH A PUSH C PUSH D PUSH C var1 = POP var2 = POP

5 Why do we use stacks? Used to keep track of things in the order that they occur Continually PUSH things onto the stack To go back, we POP things off the stack Example: PUSH A PUSH C PUSH D PUSH C POP

6 Stack Applications Whenever we want to remember a “history” so as to go backwards and forwards Examples on the computer?

7 Stack Implementation How do we implement a stack? Static size vs. Dynamic size

8 Dynamic size 1. Draw a stack using a list 2. What does an empty stack look like? 3. Push a number (8) 4. Push a number (6) 5. What should be the top? 1. How should we remember/track the top? 6. Pop! What happens?

9 Static size 1. Draw a stack using an array (already has a size and is filled with zeros 2. What does an empty stack look like? 3. Push a number (8) 4. Push a number (6) 5. What should be the top? 1. How should we remember/track the top? 6. Pop! What happens?


Download ppt "Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)"

Similar presentations


Ads by Google