Stacks
A stack is a last-in, first-out (LIFO) collection, like a stack of dishes at a buffet table or a stack of coins on your desk. An item added on top is the first item you take off the stack.
The principal methods for adding to and removing from a stack are Push( ) and Pop( ); Stack also offers a Peek( ) method, very much like Queue. Table 9-5 shows the significant methods and properties for Stack.
Table 9-5. Stack methods and properties
Method or property | Purpose |
|---|---|
| Public property that gets the number of elements in the |
| Removes all objects from the |
| Determines whether an element is in the |
| Copies the |
| Returns an enumerator for the |
| Returns the object at the top of the |
| Removes and returns the object at the top of the |
| Inserts an object at the top of the |
| Copies the elements to a new array |
| If the number of elements in the current stack is less than 90 percent of its capacity, reduces the current stack's capacity to the actual number of elements in the stack |
The List, Queue, and Stack types contain overloaded CopyTo( ) and ToArray( ) methods for copying their elements to an array. In the case of a Stack, the CopyTo( ) method will copy its elements to an existing one-dimensional array, overwriting the contents of the array beginning at the index you specify. The ToArray( ) method returns a new array ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access