4 STACKS AND QUEUES Flashcards

1
Q

What are the two data structures introduced in this chapter?

A

Stacks and queues

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

What is the primary difference between stacks and queues?

A

Stacks return the most recently inserted data first (LIFO), while queues return the oldest data (FIFO)

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

What is the core algorithm that stacks form the basis for?

A

Depth-first search

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

What type of search do queues enable?

A

Breadth-first search

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

Define a stack.

A

A last-in, first-out (LIFO) data structure

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

What operations does a stack support?

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

What happens when elements are pushed onto a stack in the order 1, 2, 3, 4, 5?

A

They are retrieved in the order 5, 4, 3, 2, 1

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

How can stacks be implemented?

A
  • Arrays
  • Linked lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the initial value of the top index in a stack implemented as an array?

A

-1

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

What does the Push operation do in a stack?

A

Adds a new element to the top of the stack

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

What is the code structure for pushing an element onto a stack implemented as an array?

A

IF s.top == s.array_size – 1:
Expand the size of the array
s.top = s.top + 1
s.array[s.top] = value

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

What does the Pop operation do in a stack?

A

Removes the element from the top of the stack and returns it

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

What is the code for popping an element from a stack implemented as an array?

A

IF s.top > -1:
value = s.array[s.top]
s.top = s.top – 1
return value

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

What is the primary advantage of using a linked list to implement a stack?

A

Increased flexibility; no need to worry about array size

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

Define a queue.

A

A first-in, first-out (FIFO) data structure

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

What operations does a queue support?

A
  • Enqueue
  • Dequeue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What happens when five elements are enqueued in the order 1, 2, 3, 4, 5?

A

They are retrieved in the same order: 1, 2, 3, 4, 5

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

How can queues be implemented?

A
  • Arrays
  • Linked lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What problem can occur when dequeuing from a fixed array?

A

A block of empty space will accumulate at the front of the array

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

What is a better solution to avoid shifting elements in a queue implemented as an array?

A

Wrapping the queue around the end of the array

21
Q

What pointers are maintained in a queue implemented as a linked list?

A
  • Front
  • Back
22
Q

What does the Enqueue operation do in a queue?

A

Adds a new element to the back of the queue

23
Q

What is the code for enqueuing an element in a queue?

A

LinkedListNode: node = LinkedListNode(value)
IF q.back == null:
q.front = node
q.back = node
ELSE:
q.back.next = node
q.back = node

24
Q

What does the Dequeue operation do in a queue?

A

Removes the element from the front of the queue and returns it

25
What is the code for dequeuing an element from a queue?
IF q.front == null: return null value = q.front.value q.front = q.front.next IF q.front == null: q.back = null return value
26
What is the impact of the order of insertion and removal in data structures?
It can have a staggering impact on the behavior of algorithms
27
In what situation do we use queues?
When we need to preserve the ordering of insertions
28
In what situation do we use stacks?
When we want to process the most recent item first
29
What is depth-first search?
An algorithm that explores deeper along a single path until it hits a dead end
30
How does depth-first search maintain future states to explore?
Using a stack
31
What is a depth-first search algorithm?
An algorithm that explores deeper along a single path until it hits a dead end, then backs up to the last branching point to explore other options.
32
How does a depth-first search maintain future states to explore?
It uses a stack, always choosing the most recently inserted option to try next.
33
In the coffee research example, how are topics added to the stack?
Topics are added in reverse alphabetical order.
34
What structure is used to represent the topics and their connections in a depth-first search?
A graph.
35
What indicates the topics that have been explored in the graph structure?
Shaded nodes.
36
What does the circled topic indicate during the search?
The topic being examined in that iteration of the search.
37
What happens when a dead end is reached in a depth-first search?
The algorithm returns to earlier items on the stack.
38
How does a breadth-first search differ from a depth-first search?
It uses a queue to store future states and explores the option that has been waiting the longest.
39
What is the key ordering difference between stacks and queues?
Stacks return the newest data, while queues return the oldest data.
40
What is the primary advantage of breadth-first search?
It explores along a frontier of topics, prioritizing breadth over depth.
41
What happens to unexplored nodes in a breadth-first search?
They are only added if they are not already in the queue.
42
What is the ideal search strategy for someone who wants to explore the most recent topic?
Depth-first search.
43
What is the ideal search strategy for someone who prefers to handle items in the order they arrive?
Breadth-first search.
44
True or False: Both depth-first search and breadth-first search explore multiple options at the same time.
False.
45
Fill in the blank: Stacks are ideal for prioritizing the _______ items.
[most recent]
46
Fill in the blank: Queues are ideal for handling items in the order in which they _______.
[arrive]
47
Why is it important to consider data structure properties when designing algorithms?
Because they impact the behavior of the algorithms.
48
What can be changed to switch from breadth-first to depth-first behavior?
The data structure used.