Stacks Flashcards
(14 cards)
What are stacks comprised of?
- A sequence of items
- A stack pointer
On what kind of basis is date retrieved from a stack?
Last-In-First-Out
What are the key operations of a stack?
- Push
- Pop
- Peek
- Test for empty stack
- Test for full stack
What is a stack overflow?
An attempt is made to push more data onto the stack that it can store
What is a stack underflow?
Occurs when a pop is attempted on an empty stack.
What are the uses of stacks?
- Reversing sequences of items
- Maintaining “undo” lists in applications
- Maintaining a web-browser history
- Storing processor state (register values) when handling an interrupt
Test if a stack is empty?
Check if the stack pointer is equal to -1
Test if a stack is full?
Compare the stack pointer to the maximum size of the array (-1)
Push an item onto a stack?
- Check if the stack is full if not…
- Increment the stack pointer by 1
- Assign the pushed item to the element within stack indexed by the stack pointer
Peeking a stack?
- Check if the stack is empty, if not…
- Save the item in element indexed by the stack pointer to a variable
- Return variable
Popping a stack?
- Check if the stack is empty, if not…
- Decrement the stack pointer
- Return the item at the stack pointer + 1
What is the call stack
-The call stack is an area of memory used to store data used by running processes and subroutines.
How does call stack work?
- With each new function/subroutine call a stack frame is pushed onto the call stack.
- When a process completes, its stack frame is popped off the call stack and the previous process continues.
- Each stack frame contains local variables, parameter values and return addresses for each “open” subroutine within a process.
- Only the stack frame at the top of the stack is actively processed.
What information does a stack frame contain
- local variables
- parameter values
- return addresses