Reprint: http://www.cnblogs.com/wisdomforce/archive/2010/05/23/1742098.htmlC# set of STACK1, stack definition
The System.Collections.Stack class represents a simple LIFO non-generic collection of objects.
2. Advantages
1, the collection of LIFO.
2. Stack can accept null references as valid values and allow duplicate elements
3. Stack's constructor
Constructor functions |
Comments |
Stack () |
Initializes a new instance of the Stack class that is empty and has the default initial capacity. |
Stack (ICollection) |
Initializes a new instance of the Stack class that contains the elements copied from the specified collection and has the same initial capacity as the number of elements copied. |
Stack (Int32) |
Initializes a new instance of the Stack class that is empty and has the specified initial capacity or default initial capacity (the larger of these two capacities). |
4. Stack Properties
Property name |
Comments |
Count |
Gets the number of elements contained in the Stack. |
5. Stack method
Method name |
Comments |
Void Clear () |
removes all objects from the Stack. |
Bool Contains (object obj) |
Determines whether an element is in the Stack. |
Object Clone () |
Creates a shallow copy of the Stack. |
Void CopyTo (Array array,int index) |
Copies the Stack to an existing one-dimensional array starting at the specified array index . |
Object Pop () |
Removes and returns the object at the top of the Stack. |
Void Push (object obj) |
Inserts an object into the top of the Stack. |
Object Peek () |
Returns the object at the top of the Stack without removing it. |
Object[]toarray () |
Copy the Stack to the new array. |
6. Example of stack usage
1 class Program2 {3 Public Static voidMain ()4 {5 6 //Create a stack7Stack Mystack =NewStack ();8Mystack.push (" the");//into the stack9Mystack.push ("Quick");TenMystack.push ("Brown"); OneMystack.push ("Fox"); A - - //print values from a collection theConsole.Write ("Stack values:"); -Printvalues (Mystack,'\ t'); - - //prints the first element at the top of the stack and removes it +Console.WriteLine ("(POP) \t\t{0}", Mystack.pop ()); - + //print values from a collection AConsole.Write ("Stack values:"); atPrintvalues (Mystack,'\ t'); - - //prints the first element at the top of the stack and removes it -Console.WriteLine ("(POP) \t\t{0}", Mystack.pop ()); - - //print values from a collection inConsole.Write ("Stack values:"); -Printvalues (Mystack,'\ t'); to + //the first element at the top of the print stack -Console.WriteLine ("(Peek) \t\t{0}", Mystack.peek ()); the * //print values from a collection $Console.Write ("Stack values:");Panax NotoginsengPrintvalues (Mystack,'\ t'); - Console.read (); the } + A the Public Static voidPrintvalues (IEnumerable mycollection,Charmyseparator) + { - foreach(Object objinchmycollection) $Console.Write ("{0}{1}", Myseparator, obj); $ Console.WriteLine (); - } - the}View Code
7. Notes
1. Stack capacity is the number of elements that stack can hold. The default initial capacity of the Stack is 10. When you add elements to a Stack, the capacity is automatically increased as needed by reallocation.
2, the capacity is the number of elements that Stack can store. Count is the number of elements actually stored in the Stack, and the capacity is always greater than or equal to count. If Count exceeds capacity when adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding new elements
Stack of C # collections