Using Stacks

Queues are first-in, first-outFIFOconstructs, which is often what you need to handle time-consuming tasks that you want to handle in a first-come, first- served manner. But there are occasions when you want to reverse the order of retrieval with first-in, last-outFILOconstructs, and in that case you use stacks . The usual example here is a spring-loaded stack of plates in cafeterias, where the first plate in the stack is the last plate taken off. You've probably heard of stacks in programming; local variables in methods are pushed onto the top of the application's internal stack to store them and popped from the top of the stack to retrieve them in reverse order. You can see the significant public properties of Stack objects in Table 6.9 and their significant public methods in Table 6.10.

Table 6.9. Significant Public Properties of Stack Objects

PROPERTY

PURPOSE

Count

Returns the number of elements contained in the stack.

IsSynchronized

Returns true if access to the stack is thread-safe.

Table 6.10. Significant Public Methods of Stack Objects

METHOD

PURPOSE

Clear

Removes all objects from the stack.

Contains

Determines whether an element is in the stack.

CopyTo

Copies the stack to an existing one-dimensional array, starting at the given array index.

GetEnumerator

Returns an IEnumerator for the stack.

Peek

Returns the object at the top of the stack without removing it.

Pop

Removes and returns the object at the top of the stack.

Push

Inserts an object at the top of the stack.

ToArray

Copies the stack to a new array.

The following parallels the queue example, except that, as you'll see, values come off the stack in reverse order. We first push a few values onto the stack:

 
 Stack stack = new Stack(); stack.Push(0); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); 

Then we pop a value and display both it and the current values on the stack:

 
 System.Console.WriteLine("Popped {0}", stack.Pop()); System.Console.Write("The stack: "); foreach(object obj in stack) {   System.Console.Write("{0} ", obj); } System.Console.WriteLine(); 

After popping a few values, we also use the stack's ToArray method to copy the stack into an array and display the values in the array:

 
 object[] array = stack.ToArray(); System.Console.Write("The stack in an array: "); foreach(object obj in array) {   System.Console.Write("{0} ", obj); } System.Console.WriteLine(); 

You can see the full code in ch06_08.cs, Listing 6.8.

Listing 6.8 Creating a Queue (ch06_08.cs)
 using System.Collections; public class ch06_08 {   static void Main()   {  Stack stack = new Stack();   System.Console.WriteLine("Pushing 0 1 2 3 4");   stack.Push(0);   stack.Push(1);   stack.Push(2);   stack.Push(3);   stack.Push(4);   System.Console.Write("The stack: ");   foreach(object obj in stack)   {   System.Console.Write("{0} ", obj);   }   System.Console.WriteLine();   System.Console.WriteLine("Popped {0}", stack.Pop());   System.Console.Write("The stack: ");   foreach(object obj in stack)   {   System.Console.Write("{0} ", obj);   }   System.Console.WriteLine();   System.Console.WriteLine("Popped {0}", stack.Pop());   System.Console.Write("The stack: ");   foreach(object obj in stack)   {   System.Console.Write("{0} ", obj);   }   System.Console.WriteLine();   object[] array = stack.ToArray();   System.Console.Write("The stack in an array: ");   foreach(object obj in array)   {   System.Console.Write("{0} ", obj);   }   System.Console.WriteLine();  } } 

Here's what you see when you run ch06_08:

 
 C:\>ch06_08 Pushing 0 1 2 3 4 The stack: 4 3 2 1 0 Popped 4 The stack: 3 2 1 0 Popped 3 The stack: 2 1 0 The stack in an array: 2 1 0 

As you see, the items on the stack come off in reverse order; compare that to queues, where they come off in the same order that they went on.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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