Exercise
Queue Collections
Objective
Create a string queue using the Queue class that already exists in the DotNet platform.
Write Your C# Exercise
C# Exercise Example
// Importing the necessary namespace for collections and basic functionalities
using System; // For basic functionalities like Console
using System.Collections.Generic; // For using the Queue class
// Main program to demonstrate the Queue functionality
class Program
{
static void Main(string[] args)
{
// Create a Queue of strings
Queue queue = new Queue();
// Menu to allow user to interact with the queue
while (true)
{
Console.WriteLine("\nMenu:");
Console.WriteLine("1. Enqueue an element");
Console.WriteLine("2. Dequeue an element");
Console.WriteLine("3. Display the queue");
Console.WriteLine("4. Exit");
Console.Write("Enter your choice: ");
string choice = Console.ReadLine();
if (choice == "1")
{
// Prompt user to enter a string to enqueue into the queue
Console.Write("Enter a string to enqueue: ");
string element = Console.ReadLine();
queue.Enqueue(element); // Add the string to the queue
Console.WriteLine($"Enqueued: {element}");
}
else if (choice == "2")
{
// Dequeue an element from the queue
if (queue.Count > 0)
{
string dequeuedElement = queue.Dequeue(); // Remove and get the element from the front of the queue
Console.WriteLine($"Dequeued: {dequeuedElement}");
}
else
{
Console.WriteLine("Queue is empty! Cannot dequeue.");
}
}
else if (choice == "3")
{
// Display the current elements in the queue
if (queue.Count > 0)
{
Console.WriteLine("Queue contents:");
foreach (string item in queue)
{
Console.WriteLine(item); // Print each item in the queue
}
}
else
{
Console.WriteLine("Queue is empty.");
}
}
else if (choice == "4")
{
// Exit the program
break;
}
else
{
// Invalid choice
Console.WriteLine("Invalid choice. Please try again.");
}
}
}
}
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this exercise, you need to create a program that reads a Reverse Polish Notation (RPN) expression from a text file, such as: 3 4 6 5 - + * 6 + (Result 21). In...
In this exercise, you need to create a string list using the ArrayList class that already exists in the .NET platform. The ArrayList class allows storing items...
In this exercise, you need to create a program that reads from a text file and stores it to another text file by reversing the order of the lines. This exercise will ...
In this exercise, you need to create a program that allows the user to enter an unlimited amount of numbers. Besides entering numbers, the program should allow the user to e...
In this exercise, you need to create a basic text file reader that displays 21 lines of text and allows the user to navigate using the up and down arrow keys, and exit using...
In this exercise, you need to create a dictionary using a hash table. The purpose of this exercise is to practice the implementation of an efficient data struc...