Create a Queue by inheriting List's capabilities - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Queue

Description

Create a Queue by inheriting List's capabilities

Demo Code

using System;/* ww  w .  ja v a 2 s.  c  o  m*/
// class MyQueue inherits List's capabilities
public class MyQueue : List
{
    // pass name "queue" to List constructor
    public MyQueue() : base("queue") { }
    // place dataValue at end of queue by inserting
    // dataValue at end of linked list
    public void Enqueue(object dataValue)
    {
        InsertAtBack(dataValue);
    }
    // remove item from front of queue by removing
    // item at front of linked list
    public object Dequeue()
    {
        return RemoveFromFront();
    }
}
// demonstrate functionality of class MyQueue
class QueueTest
{
    static void Main()
    {
        MyQueue queue = new MyQueue();
        // create objects to store in the queue
        bool aBoolean = true;
        char aCharacter = '$';
        int anInteger = 34567;
        string aString = "hello";
        // use method Enqueue to add items to queue
        queue.Enqueue(aBoolean);
        queue.Display();
        queue.Enqueue(aCharacter);
        queue.Display();
        queue.Enqueue(anInteger);
        queue.Display();
        queue.Enqueue(aString);
        queue.Display();
        // use method Dequeue to remove items from queue
        object removedObject = null;
        // remove items from queue
        try
        {
            while (true)
            {
                removedObject = queue.Dequeue();
                Console.WriteLine($"{removedObject} dequeued");
                queue.Display();
            }
        }
        catch (EmptyListException emptyListException)
        {
            // if exception occurs, write stack trace
            Console.Error.WriteLine(emptyListException.StackTrace);
        }
    }
}
class ListNode
{
    // automatic read-only property Data
    public object Data { get; private set; }
    // automatic property Next
    public ListNode Next { get; set; }
    public ListNode(object dataValue) : this(dataValue, null) { }
    public ListNode(object dataValue, ListNode nextNode)
    {
        Data = dataValue;
        Next = nextNode;
    }
}
public class List
{
    private ListNode firstNode;
    private ListNode lastNode;
    private string name; // string like "list" to display
    public List(string listName)
    {
        name = listName;
        firstNode = lastNode = null;
    }
    public List() : this("list") { }
    public void InsertAtFront(object insertItem)
    {
        if (IsEmpty())
        {
            firstNode = lastNode = new ListNode(insertItem);
        }
        else
        {
            firstNode = new ListNode(insertItem, firstNode);
        }
    }
    public void InsertAtBack(object insertItem)
    {
        if (IsEmpty())
        {
            firstNode = lastNode = new ListNode(insertItem);
        }
        else
        {
            lastNode = lastNode.Next = new ListNode(insertItem);
        }
    }
    public object RemoveFromFront()
    {
        if (IsEmpty())
        {
            throw new EmptyListException(name);
        }
        object removeItem = firstNode.Data; // retrieve data
                                            // reset firstNode and lastNode references
        if (firstNode == lastNode)
        {
            firstNode = lastNode = null;
        }
        else
        {
            firstNode = firstNode.Next;
        }
        return removeItem; // return removed data
    }
    public object RemoveFromBack()
    {
        if (IsEmpty())
        {
            throw new EmptyListException(name);
        }
        object removeItem = lastNode.Data; // retrieve data
                                           // reset firstNode and lastNode references
        if (firstNode == lastNode)
        {
            firstNode = lastNode = null;
        }
        else
        {
            ListNode current = firstNode;
            // loop while current.Next is not lastNode
            while (current.Next != lastNode)
            {
                current = current.Next; // move to next node
            }
            // current is new lastNode
            lastNode = current;
            current.Next = null;
        }
        return removeItem; // return removed data
    }
    public bool IsEmpty()
    {
        return firstNode == null;
    }
    public void Display()
    {
        if (IsEmpty())
        {
            Console.WriteLine($"Empty {name}");
        }
        else
        {
            Console.Write($"The {name} is: ");
            ListNode current = firstNode;
            // output current node data while not at end of list
            while (current != null)
            {
                Console.Write($"{current.Data} ");
                current = current.Next;
            }
            Console.WriteLine("\n");
        }
    }
}
public class EmptyListException : Exception
{
    public EmptyListException() : base("The list is empty") { }
    public EmptyListException(string name)
    : base($"The {name} is empty") { }
    public EmptyListException(string exception, Exception inner)
    : base(exception, inner) { }
}

Result


Related Tutorials