How to use the Queue in C#, using Generics type objects
Suppose we have a simple class Employees:
Code:
protected class Employees : IComparable
{
private string name = String.Empty;
private int age = 0;
private double salary = 0.0d;
public string Name
{
get { return name; }
set { name= value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
/// <summary>
/// The constructor of the class Employees
/// </summary>
/// <param name="n"></param>
/// <param name="a"></param>
/// <param name="s"></param>
public Employees(string n, int a, double s)
{
name = n;
age = a;
salary = s;
}
/// <summary>
/// Override the ToString() method
/// </summary>
/// <returns></returns>
override public string ToString()
{
return String.Format("{0:s}, {1:G} - Salary: {2:c}",name, age, salary);
}
/// <summary>
/// Override the CompareTo method,
/// compare name fields
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
Employees otherEmployees = obj as Employees;
if (otherEmployees != null)
return this.name.CompareTo(otherEmployees.name);
else
throw new ArgumentException("Object is not a Employees");
}
}
Example of how to use the functions to manage the Queue:
Code:
// Create an instance of a Queue of Employees type
Queue<Employees> employees = new Queue<Employees>();
// Insert some objects of type "Employees" in the Queue
employees.Enqueue(new Employees("Yasmine", 32, 1800.00d));
employees.Enqueue(new Employees("Gabrielle", 38, 2300.00d));
employees.Enqueue(new Employees("Lynette", 35, 2200.00d));
employees.Enqueue(new Employees("Asia", 33, 1900.00d));
// Print the contents of the Queue
Response.Write("<h2>Content of Queue:</h2>");
foreach(Employees emp in employees)
{
Response.Write(emp.ToString() + "<br />");
}
// Print the number of elements in the Queue
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
Employees employ = null;
// Extract some objects from the Queue
employ = employees.Dequeue();
Response.Write("<code>employees.Dequeue()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
employ = employees.Dequeue();
Response.Write("<code>employees.Dequeue()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
// Insert some objects of type "Employees" in the Queue
employees.Enqueue(new Employees("Daisy", 36, 2000.00d));
employees.Enqueue(new Employees("Lorelayne", 39, 2100.00d));
employees.Enqueue(new Employees("Jenny", 36, 2300.00d));
employees.Enqueue(new Employees("Lisa", 34, 2600.00d));
employees.Enqueue(new Employees("Ylenia", 37, 1700.00d));
// Print the contents of the Queue
Response.Write("<h2>Content of Queue:</h2>");
foreach (Employees emp in employees)
{
Response.Write(emp.ToString() + "<br />");
}
// Extract some objects from the Queue
employ = employees.Dequeue();
Response.Write("<code>employees.Dequeue()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
employ = employees.Dequeue();
Response.Write("<code>employees.Dequeue()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
employ = employees.Peek();
Response.Write("<code>employees.Peek()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
// Print the contents of the Queue
Response.Write("<h2>Content of Queue:</h2>");
foreach (Employees emp in employees)
{
Response.Write(emp.ToString() + "<br />");
}
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
// Copy the contents of the Queue in an array
Employees[] employeesArray= employees.ToArray();
// Print only the name and age of the employees within the array
Response.Write("<h2>Content of Employees[]:</h2>");
foreach (Employees emp in employeesArray)
{
Response.Write(String.Format("{0} ({1})<br />", emp.Name, emp.Age ));
}
Description of functions used:
employees.Enqueue(new Employees("Daisy", 36, 2000.00d)); // Adds an object to the end of the Queue
employ = employees.Dequeue(); // Removes and returns the object at the beginning of the Queue
employ = employees.Peek(); // Returns the object at the beginning of the Queue without removing it
Employees[] employeesArray= employees.ToArray(); // Copy the contents of the Queue in an array
Here is the result for the sample:
Content of Queue:
Yasmine, 32 - Salary: $1,800.00
Gabrielle, 38 - Salary: $2,300.00
Lynette, 35 - Salary: $2,200.00
Asia, 33 - Salary: $1,900.00
employees.Count
: 4
employees.Dequeue()
: Yasmine, 32 - Salary: $1,800.00
employees.Count
: 3
employees.Dequeue()
: Gabrielle, 38 - Salary: $2,300.00
employees.Count
: 2
Content of Queue:
Lynette, 35 - Salary: $2,200.00
Asia, 33 - Salary: $1,900.00
Daisy, 36 - Salary: $2,000.00
Lorelayne, 39 - Salary: $2,100.00
Jenny, 36 - Salary: $2,300.00
Lisa, 34 - Salary: $2,600.00
Ylenia, 37 - Salary: $1,700.00
employees.Dequeue()
: Lynette, 35 - Salary: $2,200.00
employees.Count
: 6
employees.Dequeue()
: Asia, 33 - Salary: $1,900.00
employees.Count
: 5
employees.Peek()
: Daisy, 36 - Salary: $2,000.00
employees.Count
: 5
Content of Queue:
Daisy, 36 - Salary: $2,000.00
Lorelayne, 39 - Salary: $2,100.00
Jenny, 36 - Salary: $2,300.00
Lisa, 34 - Salary: $2,600.00
Ylenia, 37 - Salary: $1,700.00
employees.Count
: 5
Content of Employees[]:
Daisy (36)
Lorelayne (39)
Jenny (36)
Lisa (34)
Ylenia (37)