Section 7 Chapter 39 - Stacks Flashcards

1
Q

Stack

A

A data structure that functions as an ordered collection of elements where elements can be added and removed in a “Last In, First out” way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Applications of stacks (2)

A
  • Going back on a web browser

- Undoing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to implement a stack

A

A list can be used to mimic a stack, with elements only being able to be either appended to the end, or removed from the end.
Alternatively a static array can be used with a pointer for the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Operations on a stack (5)

A
  • push
  • pop
  • peek
  • isEmpty
  • isFull
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Overflow of a stack

A

A stack will have a maximum size. An attempt to add an element to a stack that would push the size past the limit will result in a stack overflow

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Underflow of a stack

A

Will occur if the stack is attempted to be popped while being empty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Role of a stack in subroutine calls

A

A call stack is used when a program is running to keep track of addresses and instructions. When a subroutine is entered, its local variables, parameters and the return address are all added to the call stack (together these values form a stack frame). When the subroutine ends the stack frame is popped and the program jumps to the return address. If there are nested subroutines then the call stack may contain many stack frames

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stack frame

A

A call stack is composed of stack frames. Each stack frame corresponds to a call to a subroutine which has not yet terminated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How parameters and local variables are stored

A

They are both stored in the stack frame

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Advantage of storing local variables in the stack

A

It is much faster than using the heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Contents of a stack frame

A
  • Local variables
  • Parameters
  • Return address
How well did you know this?
1
Not at all
2
3
4
5
Perfectly