Welcome to our exploration of queues and deques. These structures frequently surface in everyday programming, managing everything from system processes to printer queues. In this lesson, our goal is to understand and implement queues
and deques
in C#. Let's dive in!
A queue, similar to waiting in line at a store, operates on the "First In, First Out" or FIFO principle. C#'s Queue<T>
class enables the implementation of queues. This class includes methods such as Enqueue()
for adding items and Dequeue()
for removing items.
C#1using System; 2using System.Collections.Generic; 3 4// Create a queue and add items 5Queue<string> q = new Queue<string>(); 6q.Enqueue("Apple"); 7q.Enqueue("Banana"); 8q.Enqueue("Cherry"); 9 10// Remove an item 11Console.WriteLine(q.Dequeue()); // Expects "Apple"
The dequeued item, "Apple"
, was the first item we inserted, demonstrating the FIFO principle of queues.
Before trying to remove items from our queue, let's ensure it is not empty. This precaution will prevent runtime errors when attempting to dequeue from an empty queue.
C#1using System; 2using System.Collections.Generic; 3using System.Linq; 4 5// Create a queue and enqueue items 6Queue<string> q = new Queue<string>(); 7q.Enqueue("Item 1"); 8q.Enqueue("Item 2"); 9 10// Check if the queue is non-empty, then dequeue an item 11if (q.Any()) 12{ 13 Console.WriteLine(q.Dequeue()); // Expects "Item 1" 14}
A deque, or "double-ended queue," allows the addition and removal of items from both ends. C# provides the LinkedList<T>
class for implementing deques. We can add items to both ends of our deque using the AddLast(item)
method for the right end and the AddFirst(item)
method for the left. Similarly, we can remove elements from the left and right ends using RemoveFirst
and RemoveLast
.
C#1using System; 2using System.Collections.Generic; 3 4// Create a deque and add items 5LinkedList<string> d = new LinkedList<string>(); 6d.AddLast("Middle"); 7d.AddLast("Right end"); 8d.AddFirst("Left end"); 9 10// Remove an item 11Console.WriteLine(d.Last.Value); // Expects "Right end" 12d.RemoveLast(); 13 14// Remove an item from the left 15Console.WriteLine(d.First.Value); // Expects "Left end" 16d.RemoveFirst();
The LinkedList<T>
class offers substantial functionality, though it does not have a built-in rotation method. We can implement rotation manually using a loop.
C#1using System; 2using System.Collections.Generic; 3 4// Create a deque 5LinkedList<string> d = new LinkedList<string>(); 6d.AddLast("Apple"); 7d.AddLast("Banana"); 8d.AddLast("Cherry"); 9 10// Rotate the deque to the right by 1 place 11for (int i = 0; i < 1; i++) 12{ 13 d.AddFirst(d.Last.Value); 14 d.RemoveLast(); 15} 16 17foreach (var item in d) 18{ 19 Console.WriteLine(item); // Expects [Cherry, Apple, Banana] in separate lines 20}
Here, the manual rotation loop shifts all items to the right by one position.
Congratulations on finishing this detailed study of queues and deques in C#! You've learned their operating principles and how to construct and manipulate them in C#.
Prospectively, we aim to comprehend additional data structures like these. This quest opens up a world of opportunities for expressing your ideas and solving complex problems. Are you ready for forthcoming practice exercises to consolidate this new knowledge? Let's continue our exploration!