Python - DS: Queues Flashcards

1
Q

What is a queue? What are the 3 methods of interaction and what do they do?

A

The first person in the queue is the first to be served. Queues are a First In, First Out or FIFO structure.

This data structure mimics a physical queue of objects like a line of people buying movie tickets. Each person has a name (the data). The first person to enqueue, or get into line, is both at the front.
_____

Queues provide three methods for interaction:

Enqueue - adds data to the “back” or end of the queue

Dequeue - provides and removes data from the “front” or beginning of the queue

Peek - reveals data from the “front” of the queue without removing it

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

What data structure do we use to implement a queue?
How do we roughly do this?
Is traversal allowed?
What is it called when a queue has a limit to its size?

A

Queues can be implemented using a linked list as the underlying data structure.

The front of the queue is equivalent to the head node of a linked list and the back of the queue is equivalent to the tail node.

Since operations are only allowed affecting the front or back of the queue, any traversal or modification to other nodes within the linked list is disallowed. Since both ends of the queue must be accessible, a reference to both the head node and the tail node must be maintained.

One last constraint that may be placed on a queue is its length. If a queue has a limit on the amount of data that can be placed into it, it is considered a bounded queue.

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

Take a look at the following code based on the Queue class you’ve been working with. What do we know is true for muffins_to_be_eaten?

A

muffins_to_be_eaten.head == muffins_to_be_eaten.tail

muffins_to_be_eaten was instantiated with a max_size of 10, not a size of 10.

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

Which two Queue methods should alert that the queue is empty?

A

dequeue() and peek()

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

Why is has_space() a useful method for the Queue class to have?

A

It allows us to see if we can enqueue() a new value on the end of the queue.

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

Which values go where in Queue‘s __init__() method?

A
#1: 0,
#2: None,
#3: None,
#4: max\_size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you instantiate an empty queue, what should self.head and self.tail be set to?

A

None and None

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

What should be the if statement for has_space()?

A

self.max_size

If the queue has a max_size then it should check if the max_size is greater than its size. If there is no max_size, then there is space for an additional item.

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

What will the return value be if we add a final statement calling dequeue() on sharks_in_the_shark_tank?

A

“Dhruti”

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

Fill in the method name:

A

enqueue

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

Fill in the blank:

is_empty() is a useful method for preventing __.

A

queue underflow

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

Fill in the method name:

A

dequeue

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