Stack/Queue Flashcards

1
Q

What are the 4 operations of queues?

A

enqueue(i)–inserts element i at the tail end of the queue;
dequeue()–removes the element at the head of the queue and return its value.

And two additional operators provided for convenience:

size()–returns current size of the queue;
peek()–peeks at the element at the head of the queue without changing the queue.

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

How does the average runtime for “popping” elements off the top of a stack compare with the average runtime for “dequeuing” elements from a queue?

A

To pop an element off of a stack, you just need to remove that element and the rest of the elements stay where they are. So the runtime is O(1).

However, to dequeue the element at the head of a queue, all the other elements need to move forward one, so the runtime isO(n) , with n being the number of elements before any number is dequeued.

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

What is one of the major downfalls of linear queues?

A

one of the major downfalls of traditional linear queues is that they can be slow. It takes time to shift all the elements along during a dequeuing.

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

What are circular queues?

A

circular queues, instead of all the elements needing to shift toward the head, we simply change the position of the head!

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

What is the runtime of circular queues?

A

In fact, circular queues take the runtime down fromO(n) to O(1).

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