A queue is a First In First Out (FIFO) data structure. Popular uses of queues include buffers and breadth-first graph traversal.
Questions tagged [queues]
54 questions
21
votes
1 answer
Is there an existing data structure that is of fixed size, and will push the oldest/last element out if a new element is inserted?
I'm looking for a data structure that will push its oldest/last element out if a new element is inserted. For example, let D represent the structure. D contains 3 elements of the type Number D's default values will be initialized to 1, 2 and 3.
$$D…
Greg M
- 313
- 2
- 8
9
votes
2 answers
What is the time complexity of enqueue and dequeue of a queue implemented with a singly linked list?
I’m trying to understand the time complexity of a queue implemented with a linked list data structure. My book says that we can the implement a queue in O(1) time by:
enqueueing at the back
dequeueing at the head
and it also says
Note that…
Appe
- 91
- 1
- 1
- 3
6
votes
0 answers
How to sort a queue using a temporary stack?
Suppose we have N natural numbers in a queue. ex queue = [3, 14, 1, 20] and an empty stack
We are allowed to make only two actions:
Action "x": Dequeue an element from the queue and push it to the stack.
Action "o": Pop an element from the stack…
entropyfever
- 179
- 7
5
votes
3 answers
Assigning a unique representation to equivalent circular queues
First, a few vague definitions:
Circular queues (or circular buffers) are data structures like normal queues but with their ends connected together (forming a "circle").
wikipedia
Let's say that 2 circular queues are equal if they have the exact…
A. Sallai
- 243
- 1
- 6
4
votes
1 answer
Is Breadth First Search Space Complexity on a Grid different?
Is the Space Complexity O(number_rows + number_cols) for Breadth First Search on a Grid. This is an attempt to show my reasoning:
For example, the flood fill question is described…
Meem
- 43
- 1
- 5
3
votes
1 answer
Understanding basic queue and dequeue operations
CLRS gives the following implementation for a queue's enqueue and dequeue operations
head = 1
tail = 1
ENQUEUE(Q, x)
Q[Q.tail] = x
if Q.tail == Q.length
Q.tail = 1
else Q.tail = Q.tail + 1
DEQUEUE(Q)
x = Q[Q.head]
if Q.head == Q.length
…
Curious
- 273
- 2
- 6
3
votes
1 answer
Breadth First Search actually require specifically Queue instead of any other type of Collection?
As far as I know, Breadth-First-Search [BFS] theory traditionally requires a Queue to process a given level of a graph (i.e: wiki seems to assume so).
But does BFS really actually require a Queue, rather than just any type of Collection?
A Queue is…
cellepo
- 150
- 6
3
votes
1 answer
Analysis of the Banana Game
My computer science professor introduced an interesting game in order to get us (his students) more familiar with the Stack and Queue ADTs.
Game Description
The banana game is played with a container -- e.g., a queue or a stack. The object of the…
antonky
- 133
- 4
3
votes
1 answer
Designing a Queue that efficiently tracks position
Suppose we want to construct a queue with the following properties:
The priority of each member of the queue must be known. For example, for the head node, their priority is 1, for the next node, their priority is 2, etc. Then given a node, we must…
Newb
- 314
- 2
- 12
2
votes
1 answer
Circular queue problem
In ciruclar queue the following command statement is very useful to use the determine the unused location. What is its importance? I know that without this line it won't be a circular queue. But how to say it in theoretical way?
rear=(rear+1)%max
Kalana Mihiranga
- 234
- 2
- 12
2
votes
2 answers
What's the advantage of two pointers linked list implementation of Queue versus one pointer circular list
Princeton Algorithms course shows the implementation of Queue using linked list and two pointers - head and tail. I've implemented the same functionality as a circular linked list using only one pointer tail. I'm wondering what's the advantage of…
Max Koretskyi
- 325
- 1
- 4
- 14
2
votes
3 answers
Using Queues for a Stack and Stacks for a Queue
I was asked a question on how to use a pair of Queues to create a Stack and how to use a pair of Stacks to create a Queue. Any thoughts on how I would do this? Right now I don't even know where to start.
Beca
- 21
- 1
2
votes
0 answers
Equivalence of input-restricted and output-restricted deques
There is a question in Art of Computer Programming by Knuth which needs us to prove that input-restricted deques and output-restricted deques can form the same number of (and possibly individually same) permutations. The solution is
By operating…
sonu
- 121
- 3
2
votes
3 answers
Why can't we replace Dijkstra's priority queue with a regular queue?
I am confused why we can't simply use a normal queue instead of a priority queue for Dijkstra's. I agree that you can find the shortest path in fewer iterations of the while loop using a priority queue. However, the runtime will still be O((E + V)…
A. Akella
- 31
- 3
1
vote
1 answer
Duplicate elements in Queue for BFS Maze Algorithm
I'm fairly new to studying data structures and algorithms.
I've implemented a non-recursive BFS on a 2d "maze array" which seems to work well. It finds the position of 2 in an array of 1s (walls) and 0s (spaces), marking visited positions with…
Robin Andrews
- 265
- 4
- 15