Week 32 — What is a Queue / Deque?

Question of the Week #32
What is a Queue / Deque?
9 Replies
Eric McIntyre
Eric McIntyre17mo ago
Queue is data structure which stores data in a real life queue manner. It follows FIFO(First in First Out) principle like a queue in a real life. So in queue a data is inserted in and removed in a First in First Out manner which means the data inserted first in a queue is removed first and the data inserted at the end of the queue will be removed in the end.
Eric McIntyre
Eric McIntyre17mo ago
Dequeue means double ended queue as it's name suggests it is similar to queue but in queue you can insert and remove the data in only one side but in dequeue you can remove and also insert the data from both ends., also from the beginning and also from the end.
Submission from shanand1995#0000
Eric McIntyre
Eric McIntyre17mo ago
A queue is a data structure that follows a FIFO pattern for insertion and removal. While a deque is a data structure in which the data can be inserted from any end and can be removed from any of the two ends. A queue can also be visualised as a priority queue whose priority is negative of the timestamp of the time of insertion. And a stack can be viewed as a priority queue that has a priority of the timestamp of the time of insertion.
Submission from oneplusiota#0000
Eric McIntyre
Eric McIntyre17mo ago
Queue is a simple queue, where nodes are bound by a pointer that joins the end of the node to the beginning of the next node, deque or "double ended queue" is a queue that uses two pointers: the beginning of the node joins the end of the previous node and the end of the node joins the beginning of the next node. Queue is a superinterface of Deque.
Submission from crispetes#0000
Eric McIntyre
Eric McIntyre17mo ago
Queue is a data type where data is inserted from one end and retrieved from the other. Just like a queue irl Dequeue is short for double ended queue and lets you add and retrieve data from both ends.
Submission from bhavraj#0000
Eric McIntyre
Eric McIntyre17mo ago
A queue is a data structure that keeps items in a First In First Out (FIFO) manner. Items added first to the queue are the ones processed first. You can only add at one end and remove at the other end. A deque however is a double ended queue. Means you can add at either end or process data at either end
Submission from ato_markin#0000
Eric McIntyre
Eric McIntyre17mo ago
A queue is a type of data structure that stores multiple elements of a certain data type, kinda like a list. Unlike a list, items are added to a queue onto the top and then taken off the bottom, in a first in first out system, a bit like one of those old plastic straw dispensers. Queues usually also take the bottom item off when viewed, but there are methods to peek and insert items into a queue at a given index, in this manner they are a lot like arraylists, although that is not their intended use case A dequeue is a type of queue that supports adding and taking items from both ends of the queue, for whatever reason. Some good use cases for queues are for example when using multiple threads, you can make a queue system to store a pool of tasks and then have threads take items off the bottom of the queue as they work.
⭐ Submission from saturn5vfive.#0000
Eric McIntyre
Eric McIntyre17mo ago
A queue is a data structure that allows adding and retrieving/removing elements. In contrast to stacks, queues are first-in-first-out i.e. elements are removed in the order of insertion (the element inserted first is also retrieved/removed first). This is analogous to a queue of people waiting for something. The interface java.util.Queue represents data structures like this. It allows to add elements on one side and remove elements from the other side:
Queue<String> someQueue = new LinkedList<>();//LinkedList implements Queue

//add some elements
someQueue.add("Hello");
someQueue.add("World");
someQueue.add("I");
someQueue.add("am");
someQueue.add("dan1st");

//remove() removes the element added first
System.out.println(someQueue.remove());//Hello
System.out.println(someQueue.remove());//World

//get the element which would be removed next without actually removing it
System.out.println("next element would be "+someQueue.peek());//next element would be I

System.out.println(someQueue.remove());//I
System.out.println(someQueue.remove());//am
System.out.println(someQueue.remove());//dan1st
Queue<String> someQueue = new LinkedList<>();//LinkedList implements Queue

//add some elements
someQueue.add("Hello");
someQueue.add("World");
someQueue.add("I");
someQueue.add("am");
someQueue.add("dan1st");

//remove() removes the element added first
System.out.println(someQueue.remove());//Hello
System.out.println(someQueue.remove());//World

//get the element which would be removed next without actually removing it
System.out.println("next element would be "+someQueue.peek());//next element would be I

System.out.println(someQueue.remove());//I
System.out.println(someQueue.remove());//am
System.out.println(someQueue.remove());//dan1st
java.util.Deque (short for "double-ended queue") is an extension of the Queue interface. A Deque allows to insert and remove elements from both sides:
Deque<String> someDeque = new LinkedList<>();//LinkedList even implements Deque

//add at the end
someDeque.addLast("Hello");
someDeque.addLast("World");

//add at the beginning
someDeque.addFirst("this is inserted at the beginning");
someDeque.addFirst("this is even before");

//remove at beginning and end
System.out.println(someDeque.removeFirst());//this is even before
System.out.println(someDeque.removeLast());//World

//obtain the first and last elements without removing
System.out.println(someDeque.peekFirst());//this is inserted at the beginning
System.out.println(someDeque.peekLast());//Hello

//these two elements are still in it
System.out.println(someDeque.size());//2
Deque<String> someDeque = new LinkedList<>();//LinkedList even implements Deque

//add at the end
someDeque.addLast("Hello");
someDeque.addLast("World");

//add at the beginning
someDeque.addFirst("this is inserted at the beginning");
someDeque.addFirst("this is even before");

//remove at beginning and end
System.out.println(someDeque.removeFirst());//this is even before
System.out.println(someDeque.removeLast());//World

//obtain the first and last elements without removing
System.out.println(someDeque.peekFirst());//this is inserted at the beginning
System.out.println(someDeque.peekLast());//Hello

//these two elements are still in it
System.out.println(someDeque.size());//2
Eric McIntyre
Eric McIntyre17mo ago
The Deque interface can also be used for stacks, even though it supports more functionality. It provides the methods push, pop and peek which are equivalent to addFirst, removeFirst and getFirst. For example, the ArrayDeque implementation is an efficient implementation of stacks:
Deque<String> stack = new ArrayDeque<>();

stack.push("World");
stack.push("Hello");

System.out.println(stack.pop());//Hello
System.out.println(stack.pop());//World
Deque<String> stack = new ArrayDeque<>();

stack.push("World");
stack.push("Hello");

System.out.println(stack.pop());//Hello
System.out.println(stack.pop());//World
⭐ Submission from dan1st#0000
Want results from more Discord servers?
Add your server