Stack


A stack is another container that is very similar to the queue. You just use different methods to access the stack. The item that is added last to the stack is read first. The stack is a last in, first out (LIFO) container.

Figure 10-2 shows the representation of a stack where the Push() method adds an item to the stack, and the Pop() method gets the item that was added last.

image from book
Figure 10-2

Similar to the queue classes, the nongeneric Stack class implements the interfaces ICollection, IEnumerable, and ICloneable; the generic Stack<T> class implements the interfaces IEnumerable<T>, ICollection, and IEnumerable.

Members of the Stack and Stack<T> class are listed in the following table.

Open table as spreadsheet

Stack and Stack<T> Members

Description

Push()

The Push() method adds an item on top of the stack.

Pop()

The Pop() method removes and returns item from the top of the stack. If the stack is empty, an exception of type InvalidOperationException is thrown.

Peek()

The Peek() method returns an item from the top of the stack but does not remove the item.

Count

The property Count returns the number of items in the stack.

Contains()

The Contains() method checks whether an item is in the stack and returns true if it is.

CopyTo() ToArray()

With the CopyTo() method, you can copy the items from the stack to an existing array. The method ToArray() returns a new array containing the elements of the stack.

In this example, three items are added to the stack with the Push() method. With the foreach method, all items are iterated using the IEnumerable interface. The enumerator of the stack does not remove the items; it just returns item by item.

  Stack<char> alphabet = new Stack<char>(); alphabet.Push('A'); alphabet.Push('B'); alphabet.Push('C'); foreach (char item in alphabet) {    Console.Write(item); } Console.WriteLine(); 

Because the items are read in the order from the last added to the first, the following result is produced:

 CBA

Reading the items with the enumerator does not change the state of the items. With the Pop() method, every item that is read is also removed from the stack. This way that you can iterate the collection using a while loop and verify the Count property if items are still existing:

 Stack<char> alphabet = new Stack<char>(); alphabet.Push('A'); alphabet.Push('B'); alphabet.Push('C'); Console.Write("First iteration: "); foreach (char item in alphabet) {    Console.Write(item); } Console.WriteLine(); Console.Write("Second iteration: "); while (alphabet.Count > 0) {    Console.Write(alphabet.Pop()); } Console.WriteLine(); 

The result gives two times CBA. After the second iteration, the stack is empty because the second iteration used the Pop() method:

 First iteration: CBA Second iteration: CBA




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net