90 likes | 106 Views
Data Structures - Stacks. What are data structures?. Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey) AddressBook and more!. Introducing the Stack. LIFO ~ Last In First Out How does a stack of trays/plates work?
E N D
What are data structures? • Different ways to organize data • What data structures have we used before? • lists / arrays • Deck (AceyDeucey) • AddressBook • and more!
Introducing the Stack • LIFO ~ Last In First Out • How does a stack of trays/plates work? • 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 • isEmpty – returns True if stack is empty
Stack Example PUSH A PUSH C PUSH D PUSH C var1 = POP var2 = POP
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 POP POP POP
Stack Applications • Whenever we want to remember a “history” so as to go backwards and forwards • Examples on the computer?
Stack Implementation • How do we implement a stack? • Static size vs. Dynamic size
Dynamic size • 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? • How should we remember/track the top? • Pop! What happens?
Static size • 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? • How should we remember/track the top? • Pop! What happens?