100 likes | 220 Views
CSE 2341 Object Oriented Programming with C++ Note Set #19. Overview. Stack Data Structure. The Stack. STACK data structure that stores and retrieves items in a last-in-first-out manner only have access to element “on top” of the stack How is this different from a basic array?. Stack.
E N D
Overview • Stack Data Structure
The Stack • STACK • data structure that stores and retrieves items in a last-in-first-out manner • only have access to element “on top” of the stack • How is this different from a basic array?
Stack • Think of a stack of plates in a cafeteria last in first out 5 4 3 2 1 last out first in
Applications of Stacks • Used during program execution • Can use a stack to perform mathematical operations • general: useful data structure for any algorithms that work with the last saved element of a series.
2 Types • Static Stacks • Fixed size • Implemented as Arrays • Dynamic Stacks • ability to grow and shrink as needed. • Implemented as Linked lists
Basic Stack Operations • push(x) • causes a value to be stored or pushed onto the stack Empty Stack push(5) push(10) push(7) 5 10 7 5 10 5
Basic Stack Operations • pop(x) • retrieves (and hence, removes) a value from the stack. 10 5 7 10 5 pop(x) 5 pop(x) pop(x)
Basic Stack Operations • isFull() • boolean function needed with fixed-size stacks • prevents stack overflow from trying to push a value on when there is no more room • isEmpty() • boolean function needed for both fixed and dynamic stacks • prevents any errors from the pop operation on an empty stack.
Fini ?