Queue Collections - C# Programming Exercise

In this exercise, you need to create a string queue using the Queue class that already exists in the DotNet platform. The Queue class is an implementation of the queue data structure that follows the FIFO (First In, First Out) principle, where the first element to enter is the first one to exit. The purpose of this exercise is to familiarize yourself with using the Queue class in C# to manage data efficiently.

In this exercise, you will create an instance of the Queue class, add elements to the queue using the Enqueue method (to insert elements), and remove elements using the Dequeue method (to remove the first element). You will also use the Peek method to view the first element without removing it. Throughout the exercise, you will learn how to leverage the predefined functionalities of the Queue class to manage data queues easily and efficiently.

By completing this exercise, you will gain experience in handling queues using C# framework classes, which will enable you to apply them in projects where you need to process elements in an orderly and sequential manner, such as task management or workflow control.

 Category

Dynamic Memory Management

 Exercise

Queue Collections

 Objective

Create a string queue using the Queue class that already exists in the DotNet platform.

 Write Your C# Exercise

// 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.");
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Dynamic Memory Management

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#.

  •  Queue Stack Reverse Polish Notation

    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...

  •  ArrayList

    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...

  •  ArrayList duplicate a text file

    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 ...

  •  Unlimited sum

    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...

  •  ArrayList - Text file reader

    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...

  •  Hast Table - Dictionary

    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...