Categories

Tools

bitdefender

1&1 Web Hosting

How to use the Stack 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 Stack:

Code:

// Create an instance of a stack of Employees type
Stack<Employees> employees = new Stack<Employees>();

// Insert some objects of type "Employees" in the stack
employees.Push(new Employees("Yasmine", 32, 1800.00d));
employees.Push(new Employees("Gabrielle", 38, 2300.00d));
employees.Push(new Employees("Lynette", 35, 2200.00d));
employees.Push(new Employees("Asia", 33, 1900.00d));

// Print the contents of the stack
Response.Write("<h2>Content of Stack:</h2>");
foreach(Employees emp in employees)
{
	Response.Write(emp.ToString() + "<br />");
}

// Print the number of elements in the stack
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");


Employees employ = null;

// Extract some objects from the stack
employ = employees.Pop();
Response.Write("<code>employees.Pop()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
employ = employees.Pop();
Response.Write("<code>employees.Pop()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");

// Insert some objects of type "Employees" in the stack
employees.Push(new Employees("Daisy", 36, 2000.00d));
employees.Push(new Employees("Lorelayne", 39, 2100.00d));
employees.Push(new Employees("Jenny", 36, 2300.00d));
employees.Push(new Employees("Lisa", 34, 2600.00d));
employees.Push(new Employees("Ylenia", 37, 1700.00d));

// Print the contents of the stack
Response.Write("<h2>Content of Stack:</h2>");
foreach (Employees emp in employees)
{
	Response.Write(emp.ToString() + "<br />");
}

// Extract some objects from the stack
employ = employees.Pop();
Response.Write("<code>employees.Pop()</code>: " + employ.ToString() + "<br />");
Response.Write("<code>employees.Count</code>: " + employees.Count + "<br />");
employ = employees.Pop();
Response.Write("<code>employees.Pop()</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 stack
Response.Write("<h2>Content of Stack:</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 stack 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.Push(new Employees("Daisy", 36, 2000.00d)); // Inserts an object at the top of the Stack

employ = employees.Pop(); // Removes and returns the object at the beginning of the Stack

employ = employees.Peek(); // Returns the object at the beginning of the Stack without removing it

Employees[] employeesArray= employees.ToArray(); // Copy the contents of the Stack in an array

 

Here is the result for the sample:

Content of Stack:

Asia, 33 - Salary: $1,900.00
Lynette, 35 - Salary: $2,200.00
Gabrielle, 38 - Salary: $2,300.00
Yasmine, 32 - Salary: $1,800.00
employees.Count: 4
employees.Pop(): Asia, 33 - Salary: $1,900.00
employees.Count: 3
employees.Pop(): Lynette, 35 - Salary: $2,200.00
employees.Count: 2

Content of Stack:

Ylenia, 37 - Salary: $1,700.00
Lisa, 34 - Salary: $2,600.00
Jenny, 36 - Salary: $2,300.00
Lorelayne, 39 - Salary: $2,100.00
Daisy, 36 - Salary: $2,000.00
Gabrielle, 38 - Salary: $2,300.00
Yasmine, 32 - Salary: $1,800.00
employees.Pop(): Ylenia, 37 - Salary: $1,700.00
employees.Count: 6
employees.Pop(): Lisa, 34 - Salary: $2,600.00
employees.Count: 5
employees.Peek(): Jenny, 36 - Salary: $2,300.00
employees.Count: 5

Content of Stack:

Jenny, 36 - Salary: $2,300.00
Lorelayne, 39 - Salary: $2,100.00
Daisy, 36 - Salary: $2,000.00
Gabrielle, 38 - Salary: $2,300.00
Yasmine, 32 - Salary: $1,800.00
employees.Count: 5

Content of Employees[]:

Jenny (36)
Lorelayne (39)
Daisy (36)
Gabrielle (38)
Yasmine (32)

Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.