# Questions tagged [queues]

A queue is a First In First Out (FIFO) data structure. Popular uses of queues include buffers and breadth-first graph traversal.

40 questions
Filter by
Sorted by
Tagged with
0answers
152 views

### 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 ...
1answer
40 views

### Queue FIFO search speed up through the change of visited array?

As I was preparing for the CCC this year, I am quite confused as to why a certain code modification was able to speed up my code for CCC Seniors Problem 2. Here was the C++ source code for my first ...
0answers
68 views

### Create a potential function for an abstract queue data structure to show constant amortized-time complexity

Consider a variation of a Queue called MaxQueue, Q, that has the following operations: dequeue(Q): removes and returns the first element of Q enqueue(Q, s): Appends the integer s to the end of Q ...
1answer
82 views

### Is it possible to uniformly shuffle N items into a deque of size N where you can only modify the first or last elements?

I had a question about random shuffling. Given a sorted list, is it possible to design an algorithm to return a uniformly random arrangement of the items in a deque with the following operations: ...
0answers
62 views

### Is there a known way to make an efficient, compact, and fully persistent stack or queue?

In the world of mutable/ephemeral data structures and imperative programming languages, one of the classic ways to implement a stack or queue is to use array doubling: use mutation to fill up or empty ...
1answer
48 views

### Monotonic queues recurrence relation

I'm looking at the following explanation of monotonic queues and their applications in problem solving. Monotonic Queue Summary I have a reasonable understanding of this data structure and the ...
0answers
304 views

### Monotone priority queues

I'm a little confused about how monotone priority queues work. As far as I understand: A queue is a FIFO data structure A priority queue is a queue where the current element of highest (or lowest) ...
1answer
353 views

### Why do we do topological sorting to find shortest or longest path in weighted DAG?

I was wondering why do we need to do the topological sort before performing relaxing of edges. Wouldn't it'd be better if we do : if starting vertex is "s" ...
0answers
46 views

### How many possibilities of merging two queues?

Say there're two queues of numbered items of different kinds (e.g. color), now we need to merge them into one queue so that each item maintains its order in the original queue, regardless of its kind. ...
0answers
38 views

### n thread consensus queues

Its generally agreed that wait free queues have a consensus number of 2. But what about this following code for a consensus protocol for a queue. How is it that this does not go against the idea above?...
0answers
17 views

### How would the queueing delay in an LCFS queue change for different queue sizes?

Here we differentiate between the informative and non-informative packets. In FCFS, all packets are informative as they are sent in sequence, and so queueing delay rises with queue size. But i LCFS, ...
1answer
98 views

### Category theory structures to describe topics and queues

What are the category theory structures describing queues and topics? By queues and topics I mean the ones described in systems like Apache Kafka, AcriveMQ or Java Messaging Service. Where I can find ...
0answers
40 views

### 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 ...
2answers
132 views

### 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 ...
2answers
2k views

### Circular Queue using Dynamic Array

I'm learning data structures from "Fundamentals of Data structures in C" by Sahni . In the topic, Circular Queue using Dynamic Array, the author has mentioned below point, Let capacity be the ...
1answer
1k views

### 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 here: https://www....
1answer
275 views

### Amortized time complexity for double stack emulated queue

Assume that we have a data type $stack$ which has two operation $push$ and $pop$, both operations' time complexity is $O(1)$ in worst case. The $stack$ also has a property $size$ indicate how many ...
2answers
34k views

### 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 ...
0answers
32 views

### Data Structure which maintains spacing for similar elements

I want a queue like data structure in which a new element inserted has to be some elements away from a related element. While reading from this data structure, related elements should only be read ...
1answer
291 views

### Understanding basic queue and dequeue operations

CLRS gives the following implementation for a queue's enqueue and dequeue operations ...
1answer
416 views

### 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 ...
1answer
276 views

### 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, ...
1answer
212 views

### 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 ...
1answer
134 views

### 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 ...
1answer
389 views

### Why isn't deque implemented as a dynamic array?

We know that std::vector in C++ is implemented as a dynamic array with specific pattern of reallocation (multiplying capacity by a constant), which allows it to ...
0answers
9 views

### Is there a special name for a queue that works with groups of elements

I have a data type that accepts and returns byte arrays. However, the input is from a network callback or loading a huge file in piecemeal and the output is CRLF-terminated lines. The internal storage ...
1answer
340 views

### Using Pascal's Triangle to implement queues and stacks using heaps

I have the following question as homework in an algorithms, analysis and data structures class: And here's an answer I wrote up: A queue is a first-in-first-out data structure. A heap is a data ...
1answer
3k views

### Size of Circular Queue [closed]

I am Implementing a Queue using circular arrays in C language .The Implementation uses one empty position to indicate that the queue is full.That is if the rear is two position behind front. The ...
0answers
2k views

### stack vs queue efficiency

Is there any literature / research on the average size efficiency of stacks vs queues? I asked because I was recently working on a problem where, if using a queue, the size quickly blew up and the ...
0answers
68 views

### Modelling a queue to a resource pool (children queueing for balls)

I want to develop an algorithm to serve the queue of children queueing to a basket full of balls of different colour, where each ball can be painted within a known time any colour (of several colours ...
1answer
5k views

### 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. ...
2answers
3k views

### 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 ...
2answers
1k views

### Why is average waiting time is the same for FIFO and LIFO

I'm reading the The Algorithm Design Manual book on Data Structures and it says the following about FIFO and LIFO containers: Support retrieval in first in, first out (FIFO) order. This is surely ...
1answer
92 views

### Data Structure example

Name a data structure that you could use to implement a queue such that all of the standard queue operations are O(1). If you are thinking of a data structure based on Python's built in list, you need ...
1answer
29 views

### Preventing multiple threads from racing on a queue while not wastefully polling its size

I have a queue with 50 ints in it, and I have 3 threads that want to race at the queue to print the values in it, with a 2 second delay before trying to get the next int. The queue is not thread safe,...
3answers
284 views

### 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 ...
1answer
703 views

### Can a queue automaton recognize palindromes?

Consider the language of even-length palindromes $L = \{ WW^R \mid W \in \{0,1\}^* \}$. This language is surely context free and I need an NPDA to recognize it. But, what if we replace the stack with ...
1answer
122 views

### Queue implemented using stacks

I have this problem I'm trying to solve: Implement a queue with a constant number of stacks so that each queue operation takes a constant (worst-case) number of stack operations. I don't want ...
1answer
1k views

### Queue, moving the element at the tail to the head

Suppose I have a queue where I pull from left and push to the right, and suppose I have the contents in the queue as $a b c @ d e$ (from left to right, left is head, right is tail). Is there a simple ...
3answers
516 views

### 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 ...