Stack Flashcards

1
Q

When would you use a Stack?

A

To reverse a list/array because LIFO (can get the last elements off the top of the stack first)

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

How do you add and remove from a stack?

A

You add and remove from the back

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

How do you build a queue with a Stack?

A

You need 2 stacks because:
Q: [10, 20, 30]
S1:[10, 20, 30]

You can only remove from back of stack and we need to remove 10 so we need to move everything from S1 to s2

S2:[30,20,10]

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

Describe a Stack?

A

A stack is a data structure that allows elements to be added and removed in a specific order (LIFO), and is commonly used in programming for function calls, recursion, and undo/redo functionality.

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

What are the two main stack operations?

A

push an element onto a stack, it is added to the top of the stack

pop an element from a stack, the element at the top of the stack is removed and returned

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

What 2 ways can we implement a stack?

A

A stack can be implemented using an array or a linked list.

When using an array, the stack has a fixed size, and the size cannot be changed dynamically.

When using a linked list, the size of the stack can grow or shrink dynamically as elements are added or removed.

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

Can you explain the difference between a Stack and a Queue and when you would use each?

A

Stack - LIFO
Queue - FIFO

Stack - 1 end
Queue - 2 ends

Stack - push, pop
Queue - enqueue dequeue

Stack - reverse, recursion problems
Queue - sequential processing based problems

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