Skip to main content

Deque

A double-ended queue, often abbreviated as "deque" (pronounced as "deck"), is a linear data structure that allows elements to be added or removed from both ends efficiently. It is an extension of a queue and a stack, providing operations for adding and removing elements from the front and back. Deques can be implemented using arrays or linked lists, and they are useful in scenarios where you need to perform insertions and deletions at both ends of the data structure.

Advantages

  • Efficient Operations: Deques allow constant-time (O(1)) insertion and removal of elements at both ends, making them suitable for various algorithms and data manipulation tasks.
  • Versatility: Deques can serve as both a queue and a stack, providing flexibility in implementing various data structures and algorithms.
  • Fast Access: Deques provide fast access to both the front and rear elements, enabling efficient traversal and search operations.

Disadvantages

  • Memory Overhead: Depending on the implementation, deques may require additional memory compared to simpler data structures like arrays or linked lists.
  • Complexity: Implementing and maintaining a deque can be more complex than using a simple stack or queue, especially when dealing with dynamic resizing.

Code

namespace Extended.Collections.Playground.Generic;

internal class DequeSandbox : Sandbox
{
public Deque<string> m_deque = new Deque<string>();

protected override void Run()
{
m_deque.PushFirst("First");
m_deque.PushLast("Last");

Logger.Information("Count: {Count}", m_deque.Count); // 2

Logger.Information("Value: {Value}", m_deque.PopFirst()); // First
Logger.Information("Vlaue: {Value}", m_deque.PopFirst()); // Last
Logger.Information("Count: {Count}", m_deque.Count); // 0
}
}
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;