Data Structures Stacks What are data structures Different

  • Slides: 9
Download presentation
Data Structures Stacks

Data Structures Stacks

What are data structures? Different ways to organize data n What data structures have

What are data structures? Different ways to organize data n What data structures have we used before? n n n lists / arrays Deck (Acey. Deucey) Address. Book and more!

Introducing the Stack n n LIFO ~ Last In First Out How does a

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

Stack Example PUSH A PUSH C PUSH D PUSH C var 1 = POP

Stack Example PUSH A PUSH C PUSH D PUSH C var 1 = POP var 2 = POP

Why do we use stacks? n n Used to keep track of things in

Why do we use stacks? n n 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 POP

Stack Applications Whenever we want to remember a “history” so as to go backwards

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

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

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

Dynamic size 1. 2. 3. 4. 5. Draw a stack using a list What

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

Static size 1. 2. 3. 4. 5. Draw a stack using an array (already

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