Stacks Flashcards

1
Q

What is a stack?

A

A stack is a first in, last out (FILO) abstract data structure where items are added and removed from the top.

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

What are the key operations in a stack?

A

Push (adding an item), Pop (removing the top item), and Peek (returning the value of the top item without removing it).

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

What does the push operation do in a stack?

A

It adds an item to the top of the stack if there is space.

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

What does the pop operation do in a stack?

A

It removes the top item from the stack, decreasing the stack’s size.

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

What is stack overflow?

A

Overflow happens when trying to push to a full stack.

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

What is stack underflow?

A

Underflow happens when trying to pop from an empty stack.

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

How is a stack used in managing function calls?

A

A stack stores return addresses and local variables when a function is called. When the function ends, these are popped to return to the previous state.

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

What is the pseudo-code for pushing an item onto an array-based stack?

A

if top < stack_size
then
top = top + 1
stack[top] = item
else
output “Stack Overflow Error”
end if

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

What is the pseudo-code for popping an item from an array-based stack?

A

if top <> 0
then
item = stack[top]
top = top - 1
else
output “Stack Underflow Error”
end if

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

What does the Peek operation do in a stack?

A

Returns the value of the item at the top of the stack without removing it.

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

Where are stacks seen in real life?

A
  1. Undo/Redo functions
  2. Browser history
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some pros of using a stack?

A
  1. Simplicity
  2. Minimal operations
  3. Memory efficient
  4. Natural fit for specific problems
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some cons of using a stack?

A
  1. Limited functionality
  2. Overflow/Underflow
  3. Recursive nature
  4. Not always optimal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some uses of a stack?

A
  1. Expression evaluation
  2. Backtracking
  3. Tree and Graph traversals
  4. Call stack management
How well did you know this?
1
Not at all
2
3
4
5
Perfectly