VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/deque-in-c/

⇱ Deque in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deque in C#

Last Updated : 19 Mar, 2025

C# Deque means double-ended queue. It is a linear data structure that allows us to add or remove elements from both the front and the end. A deque is more flexible than the normal queue where we can only add elements from the back and remove them from the front.

  • Elements can be added or removed from both the front and end.
  • The deque can grow and shrink dynamically.
  • We can use deque as a Stack or Queue depending upon the use cases.

Example: This example demonstrates how to add elements to both the front and the back of the deque


Output
Elements in the Deque:
30
20
10
50

Why use a Deque?

Deque is very useful in scenarios where we need to manage the data from both the ends. Some of the common applications of deque are listed below:

  • Job Scheduling: Managing tasks with varying priorities.
  • Sliding Window Algorithms: Solving problems like finding the maximum in a subarray.
  • Breadth-First Search (BFS): Traversing graphs or trees.

Types of Deque

The below diagram demonstrates the types of deque in C#.

👁 Types-of-Dequeue


  • Input-Restricted deque: In this, insertion is allowed only at one end, but deletion can be done from both ends.
  • Output-Restricted deque: In this, deletion is allowed only at one end, but insertion can be done from both ends.
  • Double-Ended deque: In this, insertion and deletion are allowed at both ends.

Declaration of Deque

In C#, there is no built-in deque class. We can create deque with the help of LinkedList<T> class or by using Queue<T> class.

Note: Queue<T> can also create a deque, but it is not ideal for operation at both the ends due to limitations.

Using LinkedList<T> (Recommended Approach)

A LinkedList<T> is a good choice for a deque because it lets us add or remove items quickly from both the front and back.

Declaration of deque using LinkedList<T>

LinkedList<int> deque = new LinkedList<int>();

Example: This example, demonstrates how to add elements to both the front and back and then iterating through the list


Output
Deque Elements:
30
20
10
40
50

Implementing Deque in C#

C# provides Three approaches to implement deque in C#, we are going to discuss about each approach one by one

1. Implementing Deque using Arrays

An Array-based deque can be implemented by managing a fixed-size array and keeping track of front and rear pointers.

Implementation:


Output
40 30 10 20 
Dequeue from front: 40
Dequeue from rear: 20
30 10 

2. Implementing Deque using LinkedList

LinkedList<T> class is an ideal choice for deque because it allows fast insertion and deletion operations from both ends. The LinkedList<T> stores elements in a doubly-linked list, both ends can be accessed and modified in constant time O(1).

Implementation:

Output:

👁 Output

3. Implementing Deque Using List<T>

A list<T> is dynamic array, it can efficiently access elements by index. This approach is less efficient for the front operations (add/remove) compared to the LinkedList<T> approach but can still be useful for small lists.

Implementation:


Output
10
30

Performing Various Operations on Deque

1. Adding Elements to the Front

We can use the EnqueueFront() to add elements to the front of the deque.

Example: This example demonstrates how to insert elements to the front of the deque.

Output:

👁 Output

2. Adding Element to the Rear

We can use the EnqueueRear() to add elements to the end of the deque.

Example:

Output:

👁 Output

3. Removing Elements from the Front

We can use the dequeFront() method to remove elements from the front of the deque.

Example:

Output:

👁 Output

4. Removing Elements from the Rear

We can use the dequeRear() method to remove elements from the end of the deque.

Example:

Output:

👁 Output
Comment

Explore