64

Can anyone give me an example of situation where a Deque data structure is needed?

Note - Please don't explain what a deque is?

roottraveller
  • 7,942
  • 7
  • 60
  • 65
user366312
  • 16,949
  • 65
  • 235
  • 452
  • 2
    For information: [Deque](http://en.wikipedia.org/wiki/Double-ended_queue) on wikipedia. – Oded Oct 07 '10 at 09:33
  • 26
    Plus one for "please don't explain what a deque is". This guy knows stackoverflow. – mackenir Aug 28 '13 at 11:34
  • 2
    Duplicate of [Where would I typically use a Deque in production software?](http://programmers.stackexchange.com/questions/147667/where-would-i-typically-use-a-deque-in-production-software) on Programmers.SE. – mins Mar 05 '15 at 19:25

8 Answers8

54

A Deque is a double ended queue, allowing inserting and removing from both ends.

In real scenario we can attached it to a Ticket purchasing line, It performs like a queue but some time It happens that some body has purchased the ticket and sudden they come back to ask some thing on front of queue. In this scenario because they have already purchased the ticket so they have privilege to come and ask for any further query. So in these kind of scenario we need a data structure where according to requirement we add data from front. And In same scenario user can also leave the queue from rear.

So it follows completely data structure of Deque.

S.P Singh
  • 1,267
  • 3
  • 17
  • 23
29
  1. A nice application of the deque is storing a web browser's history. Recently visited URLs are added to the front of the deque, and the URL at the back of the deque is removed after some specified number of insertions at the front.
  2. Another common application of the deque is storing a software application's list of undo operations.
  3. One example where a deque can be used is the A-Steal job scheduling algorithm.[5] This algorithm implements task scheduling for several processors. A separate deque with threads to be executed is maintained for each processor. To execute the next thread, the processor gets the first element from the deque (using the "remove first element" deque operation). If the current thread forks, it is put back to the front of the deque ("insert element at front") and a new thread is executed. When one of the processors finishes execution of its own threads (i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element from the deque of another processor ("remove last element") and executes it. - Courtesy Wikipedia
Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
improgrammer
  • 552
  • 6
  • 7
  • 9
    why wouldn't a normal queue suffice for 1 and 2? – Aadith Ramia Jan 05 '16 at 04:21
  • @Aadith I think you are right about #1, but #2 is better served by a deque as you can "undo your undo" by taking it off the front of the deque, which would be illegal in a queue. Also, C++-wise, for whatever reason, deques allow iteration while queues don't. –  Jan 21 '16 at 21:07
  • 1
    As per 'Java Concurrency in Practice': "Work stealing can be more scalable than a traditional producer-consumer design because workers don't contend for a shared work queue; most of the time they access only their own deque, reducing contention. When a worker has to access another's queue, it does so from the tail rather than the head, further reducing contention." – BartoszMiller Oct 11 '16 at 13:43
27

When modeling any kind of real-world waiting line: entities (bits, people, cars, words, particles, whatever) arrive with a certain frequency to the end of the line and are serviced at a different frequency at the beginning of the line. While waiting some entities may decide to leave the line.... etc. The point is that you need "fast access" to insert/deletes at both ends of the line, hence a deque.

ric0liva
  • 301
  • 2
  • 4
  • 16
    I'm not sure how realistic this is. Consider that you can't use a deque to model a real world line unless in that line only the last person can leave. Also in this hypothetical line if the third guy from the end wants to leave then everyone behind him better share his opinion because they'll need to leave too. – nsfyn55 May 20 '11 at 19:19
  • 2
    I working on this right now: I have one program that displays images with 60Hz using OpenGL. Another program decides what should be drawn in the images. Unfortunately this other program occasionally stops for the garbage collection. I use a deque in the display program as a cache for future images. This way I can ensure that there will always be images available even when the garbage collector stops the producer occasionally. – whoplisp Jul 03 '11 at 13:32
9

Example in Wikipedia

One example where a deque can be used is the A-Steal job scheduling algorithm. This algorithm implements task scheduling for several processors. A separate deque with threads to be executed is maintained for each processor. To execute the next thread, the processor gets the first element from the deque (using the "remove first element" deque operation). If the current thread forks, it is put back to the front of the deque ("insert element at front") and a new thread is executed. When one of the processors finishes execution of its own threads (i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element from the deque of another processor ("remove last element") and executes it.

In the recent CLR via C# book Richter describes improvements made to ThreadPool in .Net 4.0. It is definitely worth reading, especially about work stealing. Algorithm described by Richter looks very similar to the example from Wikipedia so I suspect Deque is used there as well.

Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
2

http://en.wikipedia.org/wiki/Deque says that there are job scheduling algorithms that use deques. The German wikipedia page (http://de.wikipedia.org/wiki/Deque) mentions pattern-matching algorithms and the implementation of non deterministic finite state machines as use cases for deques.

jens
  • 1,763
  • 1
  • 15
  • 25
1

A "queue" can be implemented using one of two data structures

  1. linked list - if you are exclusively working on the ends, and don't care about memory allocation overhead (or constrained as to how memory can be allocated), this makes sense.
  2. deque - if you work on the ends, positional access is also required (or the ability to iterate through items in the queue quickly) and memory allocation overhead is important (but not constrained), then deque offers the best of both (vector like allocation with linked list like functionality - well, at the ends anyway, insert in the middle is more expensive)

Typically, a deque is useful for priority queuing, scanning the queue is significantly faster with a deque than linked list.

Nim
  • 33,299
  • 2
  • 62
  • 101
0

A deque can model a train station where cars can enter and leave on the left or right side of a line, but only the cars at the ends can move in and out. This is because the cars on the inside cannot hit the cars on the outside to leave, so they just have to wait.

thegenie
  • 9
  • 2
0

There is a practical usage example when chaining iterators. I've used it to implement an extendable iterator:

import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedDeque;

public class ExtendableIterator<T> implements Iterator<T> {

    public Deque<Iterator<T>> its = new ConcurrentLinkedDeque<Iterator<T>>();

    public ExtendableIterator() {

    }

    public ExtendableIterator(Iterator<T> it) {
        this();
        this.extend(it);
    }

    @Override
    public boolean hasNext() {
        // this is true since we never hold empty iterators
        return !its.isEmpty() && its.peekLast().hasNext();
    }

    @Override
    public T next() {
        T next = its.peekFirst().next();
        if (!its.peekFirst().hasNext()) {
            its.removeFirst();
        }
        return next;
    }

    public void extend(Iterator<T> it) {
        if (it.hasNext()) {
            its.addLast(it);
        }
    }
}
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88