Home

C# Collections: The .NET Framework Built-In Queue Collection Class

     

Introduction

To support queues, the .NET Framework provides the serialized Queue class that implements both the ICollection and the IEnumerable interfaces.

   

Creating a Queue

To create a queue, you can declare a Queue variable using one of its four constructors. The default constructor allows you to start a list without primarily adding an item to it. Here is an example:

public static class Program
{
    public static int Main(string[] args)
    {
        Queue que = new Queue();

        return 0;
    }
}

After this declaration, the compiler reserves some default memory space for the variable. If you want to specify how much space should be primarily allocated for the variable, you can use the following constructor:

public Queue(int capacity);

Building a Queue

The action that consists of adding an item to the queue is called "enqueue". To perform this operation, you can call the Queue.Enqueue() method whose syntax is:

public virtual void Enqueue(object obj);

This method takes as argument the object you want to add to the queue and places it as the first candidate to be removed. Here are examples of calling the method:

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;

public enum Majors
{
    English,
    Nursing,
    Accounting,
    ComputerSciences,
    BusinessManagement,
    MathematicalSciences,
    HumanResourceDevelopment
}

[Serializable]
public class Student
{
    public string FullName;
    public DateTime DateOfBirth;
    public Majors Major;
}

public static class Program
{
    public static int Main(string[] args)
    {
        Queue que = new Queue();
        Student std = null;

        std = new Student();
        std.FullName = "Donald Palau";
        std.DateOfBirth = new DateTime(1988, 5, 12);
        std.Major = Majors.ComputerSciences;
        que.Enqueue(std);

        std = new Student();
        std.FullName = "Arlene Kafka";
        std.DateOfBirth = new DateTime(1992, 8, 4);
        std.Major = Majors.BusinessManagement;
        que.Enqueue(std);

        std = new Student();
        std.FullName = "Hortense Moons";
        std.DateOfBirth = new DateTime(1994, 10, 7);
        std.Major = Majors.MathematicalSciences;
        que.Enqueue(std);

        SaveStudents(que);
        return 0;
    }

    public static void SaveStudents(Queue q)
    {
        FileStream fsStudent = new FileStream("Students.roh",
                                            FileMode.Create,
                                            FileAccess.Write);
        BinaryFormatter bfStudent = new BinaryFormatter();

        bfStudent.Serialize(fsStudent, q);
        fsStudent.Close();
    }

    public static Queue OpenStudents()
    {
        FileStream fsStudent = new FileStream("Students.roh",
                                            FileMode.Open,
                                            FileAccess.Read);
        BinaryFormatter bfStudent = new BinaryFormatter();

        Queue pupils = (Queue)bfStudent.Deserialize(fsStudent);
        fsStudent.Close();

        return pupils;
    }
}

If you already have a list created from a class that implements the ICollection interface, you can create a queue based on that list. To do this, you would use the following constructor of the Queue class:

public Queue(ICollection col);

This constructor accepts an ICollection implementer as argument. Here is an example:

using System;

public enum Majors
{
    English,
    Nursing,
    Accounting,
    ComputerSciences,
    BusinessManagement,
    MathematicalSciences,
    HumanResourceDevelopment
}

public class Student
{
    public string FullName;
    public DateTime DateOfBirth;
    public Majors Major;
}

public static class Program
{
    public static int Main(string[] args)
    {
        Student std = null;
        ArrayList lstStudents = new ArrayList();

        std = new Student();
        std.FullName = "Hermine Justice";
        std.DateOfBirth = new DateTime(1990, 3, 24);
        std.Major = Majors.English;
        lstStudents.Add(std);

        std = new Student();
        std.FullName = "Patricia Palermo";
        std.DateOfBirth = new DateTime(1989, 12, 20);
        std.Major = Majors.BusinessManagement;
        lstStudents.Add(std);

        Queue que = new Queue(lstStudents);
        
        return 0;
    }
}

Retrieving the Items From a Queue

Remember that the first item added to a queue stays in front of all others that would be added. To get (only) the first item of a queue, the Queue class is equipped with a method named Peek(). Its syntax is:

public virtual object Peek();

This method returns an Object object that represents the first item of the queue (remember to cast it to your particular object if necessary). Here are two examples of calling it:

using System;
using System.Collections;

public enum Majors
{
    English,
    Nursing,
    Accounting,
    ComputerSciences,
    BusinessManagement,
    MathematicalSciences,
    HumanResourceDevelopment
}

public class Student
{
    public string FullName;
    public DateTime DateOfBirth;
    public Majors Major;
}

public static class Program
{
    public static int Main(string[] args)
    {
        Queue que = new Queue();
        Student std = null;

        std = new Student();
        std.FullName = "Donald Palau";
        std.DateOfBirth = new DateTime(1988, 5, 12);
        std.Major = Majors.ComputerSciences;
        que.Enqueue(std);

        std = new Student();
        std.FullName = "Arlene Kafka";
        std.DateOfBirth = new DateTime(1992, 8, 4);
        std.Major = Majors.BusinessManagement;
        que.Enqueue(std);

        Console.WriteLine("The current first item of the queue is");
        Student s1 = (Student)que.Peek();
        Console.WriteLine("== Student Information ==");
        Console.WriteLine("Full Name:     {0}", s1.FullName);
        Console.WriteLine("Date of Birth: {0:d}", s1.DateOfBirth);
        Console.Write("Major:         ");
        switch (s1.Major)
        {
            case Majors.English:
                Console.WriteLine("English");
                break;
            case Majors.Nursing:
                Console.WriteLine("Nursing");
                break;
            case Majors.Accounting:
                Console.WriteLine("Accounting");
                break;
            case Majors.ComputerSciences:
                Console.WriteLine("Computer Sciences");
                break;
            case Majors.BusinessManagement:
                Console.WriteLine("Business Management");
                break;
            case Majors.MathematicalSciences:
                Console.WriteLine("Mathematical Sciences");
                break;
            case Majors.HumanResourceDevelopment:
                Console.WriteLine("Human Resource Development");
                break;
        }

        Console.WriteLine("--------------------------------------------");

        std = new Student();
        std.FullName = "Hortense Moons";
        std.DateOfBirth = new DateTime(1994, 10, 7);
        std.Major = Majors.MathematicalSciences;
        que.Enqueue(std);

        Console.WriteLine("The current first item of the queue is");
        Student s2 = (Student)que.Peek();
        Console.WriteLine("== Student Information ==");
        Console.WriteLine("Full Name:     {0}", s2.FullName);
        Console.WriteLine("Date of Birth: {0:d}", s2.DateOfBirth);
        Console.Write("Major:         ");
        switch (s2.Major)
        {
            case Majors.English:
                Console.WriteLine("English");
                break;
            case Majors.Nursing:
                Console.WriteLine("Nursing");
                break;
            case Majors.Accounting:
                Console.WriteLine("Accounting");
                break;
            case Majors.ComputerSciences:
                Console.WriteLine("Computer Sciences");
                break;
            case Majors.BusinessManagement:
                Console.WriteLine("Business Management");
                break;
            case Majors.MathematicalSciences:
                Console.WriteLine("Mathematical Sciences");
                break;
            case Majors.HumanResourceDevelopment:
                Console.WriteLine("Human Resource Development");
                break;
        }

        Console.WriteLine("--------------------------------------------");

        return 0;
    }
}

This would produce:

The current first item of the queue is
== Student Information ==
Full Name:     Donald Palau
Date of Birth: 5/12/1988
Major:         Computer Sciences
--------------------------------------------
The current first item of the queue is
== Student Information ==
Full Name:     Donald Palau
Date of Birth: 5/12/1988
Major:         Computer Sciences
--------------------------------------------
Press any key to continue . . .

To give you access to the items of a queue, the Queue class overrides the GetEnumerator() method of the IEnumerable class that it implements. This allows you to use the foreach loop to continuously enumerate the members of a queue. Here is an example:

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;

public enum Majors
{
    English,
    Nursing,
    Accounting,
    ComputerSciences,
    BusinessManagement,
    MathematicalSciences,
    HumanResourceDevelopment
}

[Serializable]
public class Student
{
    public string FullName;
    public DateTime DateOfBirth;
    public Majors Major;
}

public static class Program
{
    public static int Main(string[] args)
    {
        Student std = null;
        Queue lstStudents = OpenStudents();

        std = new Student();
        std.FullName = "Hermine Justice";
        std.DateOfBirth = new DateTime(1990, 3, 24);
        std.Major = Majors.English;
        lstStudents.Enqueue(std);

        std = new Student();
        std.FullName = "Patricia Palermo";
        std.DateOfBirth = new DateTime(1989, 12, 20);
        std.Major = Majors.BusinessManagement;
        lstStudents.Enqueue(std);

        SaveStudents(lstStudents);
        lstStudents = OpenStudents();
        ShowStudents(lstStudents);

        return 0;
    }

    public static void ShowStudents(Queue q)
    {
        foreach (Student s in q)
        {
            Console.WriteLine("== Student Information ==");
            Console.WriteLine("Full Name:     {0}", s.FullName);
            Console.WriteLine("Date of Birth: {0:d}", s.DateOfBirth);
            Console.Write("Major:         ");
            switch (s.Major)
            {
                case Majors.English:
                    Console.WriteLine("English");
                    break;
                case Majors.Nursing:
                    Console.WriteLine("Nursing");
                    break;
                case Majors.Accounting:
                    Console.WriteLine("Accounting");
                    break;
                case Majors.ComputerSciences:
                    Console.WriteLine("Computer Sciences");
                    break;
                case Majors.BusinessManagement:
                    Console.WriteLine("Business Management");
                    break;
                case Majors.MathematicalSciences:
                    Console.WriteLine("Mathematical Sciences");
                    break;
                case Majors.HumanResourceDevelopment:
                    Console.WriteLine("Human Resource Development");
                    break;
            }

            Console.WriteLine("--------------------------------------------");
        }
    }

    public static void SaveStudents(Queue q)
    {
        FileStream fsStudent = new FileStream("Students.roh",
                                            FileMode.Create,
                                            FileAccess.Write);
        BinaryFormatter bfStudent = new BinaryFormatter();

        bfStudent.Serialize(fsStudent, q);
        fsStudent.Close();
    }

    public static Queue OpenStudents()
    {
        FileStream fsStudent = new FileStream("Students.roh",
                                            FileMode.Open,
                                            FileAccess.Read);
        BinaryFormatter bfStudent = new BinaryFormatter();

        Queue pupils = (Queue)bfStudent.Deserialize(fsStudent);
        fsStudent.Close();

        return pupils;
    }
}

This would produce:

== Student Information ==
Full Name:     Donald Palau
Date of Birth: 5/12/1988
Major:         Computer Sciences
--------------------------------------------
== Student Information ==
Full Name:     Arlene Kafka
Date of Birth: 8/4/1992
Major:         Business Management
--------------------------------------------
== Student Information ==
Full Name:     Hortense Moons
Date of Birth: 10/7/1994
Major:         Mathematical Sciences
--------------------------------------------
== Student Information ==
Full Name:     Hermine Justice
Date of Birth: 3/24/1990
Major:         English
--------------------------------------------
== Student Information ==
Full Name:     Patricia Palermo
Date of Birth: 12/20/1989
Major:         Business Management
--------------------------------------------
Press any key to continue . . .

Removing Items From a Queue

The process of removing an item from a queue is called "dequeue". To assist you with performing this operation, the Queue class is equipped with a method named Dequeue. Its syntax is:

public virtual object Dequeue();

The primary purpose of this method is to delete the first item of the Queue list. If you are interested to know what item was deleted, this method returns it.

To remove all items from the queue, you can call the Clear() method of the class. Its syntax is:

public virtual void Clear();
 
 

Home Copyright © 2010-2011 FunctionX