Questions tagged [queue]

A queue is an ordered, first-in-first-out data structure. Typical implementations of queues support pushing elements to the back and popping them off the front position.

  • A queue is not limited to a fixed capacity
    • A bounded queue is a queue limited to a fixed number of items
  • A doubly linked list is a good implementation choice

Operations

  • enqueue - pushes an element to the end of the queue
  • dequeue - pops an element from the front of the queue

See Also

Resources

Queue Wikipedia Article

11098 questions
915
votes
32 answers

How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript? I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures.
KingNestor
  • 65,976
  • 51
  • 121
  • 152
462
votes
20 answers

How to implement a queue using two stacks?

Suppose we have two stacks and no other temporary variable. Is to possible to "construct" a queue data structure using only the two stacks?
Nitin
  • 15,151
  • 8
  • 23
  • 14
260
votes
8 answers

Queue.Queue vs. collections.deque

I need a queue which multiple threads can put stuff into, and multiple threads may read from. Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be…
miracle2k
  • 29,597
  • 21
  • 65
  • 64
224
votes
9 answers

Size-limited queue that holds last N elements in Java

A very simple & quick question on Java libraries: is there a ready-made class that implements a Queue with a fixed maximum size - i.e. it always allows addition of elements, but it will silently remove head elements to accomodate space for newly…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
209
votes
12 answers

How do I clear the std::queue efficiently?

I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner). In one scenario, I want to clear the queue in one shot( delete all jobs from the queue). I don't see any clear method available in…
aJ.
  • 34,624
  • 22
  • 86
  • 128
200
votes
4 answers

Multiprocessing - Pipe vs Queue

What are the fundamental differences between queues and pipes in Python's multiprocessing package? In what scenarios should one choose one over the other? When is it advantageous to use Pipe()? When is it advantageous to use Queue()?
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
169
votes
10 answers

Creating a blocking Queue in .NET?

I have a scenario where I have multiple threads adding to a queue and multiple threads reading from the same queue. If the queue reaches a specific size all threads that are filling the queue will be blocked on add until an item is removed from the…
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
166
votes
15 answers

Fixed size queue which automatically dequeues old values upon new enqueues

I'm using ConcurrentQueue for a shared data structure which purpose is holding the last N objects passed to it (kind of history). Assume we have a browser and we want to have the last 100 browsed Urls. I want a queue which automatically drop…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
164
votes
8 answers

How do I instantiate a Queue object in java?

When I try: Queue q = new Queue(); The compiler is giving me an error. Any help? Also, if I want to initialize a queue do I have to implement the methods of the queue?
wam090
  • 2,823
  • 8
  • 33
  • 36
164
votes
15 answers

Is there a fixed sized queue which removes excessive elements?

I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element. Is there an existing implementation for this in Java?
c0d3x
  • 2,593
  • 6
  • 23
  • 18
155
votes
16 answers

Run PHP Task Asynchronously

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don't want to make the user wait for the result. For example, when creating a new account, I…
davr
  • 18,877
  • 17
  • 76
  • 99
155
votes
6 answers

Which concurrent Queue implementation should I use in Java?

From the JavaDocs: A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. This queue does not permit null elements. ArrayBlockingQueue is a classic "bounded buffer", in which a fixed-sized array…
David Hofmann
  • 5,683
  • 12
  • 50
  • 78
145
votes
9 answers

What is the difference between the add and offer methods in a Queue in Java?

Take the PriorityQueue for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E) Can anyone give me an example of a Queue where the add and offer methods are different? According to the Collection doc, the add method…
Finbarr
  • 31,350
  • 13
  • 63
  • 94
138
votes
12 answers

what is the basic difference between stack and queue?

What is the basic difference between stack and queue?? Please help me i am unable to find the difference. How do you differentiate a stack and a queue? I searched for the answer in various links and found this answer.. In high level programming, a…
Harish Rachakonda
  • 1,531
  • 3
  • 12
  • 7
125
votes
6 answers

LinkedBlockingQueue vs ConcurrentLinkedQueue

My question relates to this question asked earlier. In situations where I am using a queue for communication between producer and consumer threads would people generally recommend using LinkedBlockingQueue or ConcurrentLinkedQueue? What are the…
Adamski
  • 54,009
  • 15
  • 113
  • 152
1
2 3
99 100