Stack<t> class
Stack<The t> is implemented as an array.
The capacity of stack<t> is the number of elements that stack<t> can include.
When elements are added to Stack<t>, the internal array is allocated once again to increase the capacity on its own initiative.
You can reduce capacity by calling TrimExcess. Assuming Count is less than the capacity of the stack, the operation complexity of Push is O (1). Assuming that you need to add capacity to accommodate new elements, the operation complexity of Push becomes O (n). where n is Count. The operation complexity of the POP is O (1).
Stack<t> accepts NULL as a valid value for a reference type and agrees to have repeated elements.
named control: System.Collections.Generic
Assembly: System (in System.dll)
syntax: public class Stack<t>:ienumerable<T>, ICollection, IEnumerable
List<t> implements IList<t>, ICollection<t>, IEnumerable<T >, IList, ICollection, IEnumerable interface
so we can see that compared with list1t>:
stack<t> does not inherit the ICollection<t> interface, because the Add () and remove () methods defined by this interface cannot be used for stacks;
stack<t> does not inherit the IList<t> interface, so you cannot use the indexer to access the stack.
so the queue simply agrees to add elements to the top of the stack and delete the elements.
members of the stack<t> class that are used frequently:
Count: Returns the number of elements in the stack.
Push (): Adds an element to the top of the stack.
Pop (): Deletes an element from the top of the stack.
Assuming that the stack is empty, an exception InvalidOperationException exception is thrown.
Peek (): Returns the element at the top of the stack, but does not delete it.
Contains (): Determines whether an element is in the stack. Assuming yes, returns TRUE.
/********************************************************************************************************** ********************/
The source code for the member functions of the Stack1t> class is often used such as the following:
Public bool Contains (T Item)
{
int index = this._size;
Equalitycomparer<t> comparer = Equalitycomparer<t>. Default;
While (index--> 0)
{
if (item = = NULL)
{
if (this._array[index] = = null)
{
return true;
}
}
else if ((This._array[index]! = null) && comparer. Equals (This._array[index], item))
{
return true;
}
}
return false;
}
Public T-Peek ()
{
if (this._size = = 0)
{
throwhelper.throwinvalidoperationexception (exceptionresource.invalidoperation_emptystack);
}
return this._array[this._size-1];
}
Public T Pop ()
{
if (this._size = = 0)
{
throwhelper.throwinvalidoperationexception (exceptionresource.invalidoperation_emptystack);
}
this._version++;
T local = this._array[--this._size];
This._array[this._size] = default (T);
return local;
}
Public void Push (T item)
{
if (this._size = = This._array. Length)
{
t[] Destinationarray = new t[(This._array. Length = = 0)? 4: (2 * this._array. Length)];
array.copy (this._array, 0, Destinationarray, 0, this._size);
This._array = destinationarray;
}
this._array[this._size++] = Item;
this._version++;
}
/********************************************************************************************************** *******************************/
The following code shows a sample demo of the StackSeveral methods of generic classes. This code demonstrates the sample creation of a string stack with default capacity, and uses the Push method to press five strings into the stack.
Enumerates the elements of the stack, which does not change the state of the stack.
Use the Pop method to pop the first string out of the stack. Use the Peek method to view the next item in the stack, and then use the Pop method to eject it.
Use the ToArray method to create an array and copy the stack elements into it, and then pass the array to the stack constructor with IEnumerable, creating a stack copy in the reverse order of the elements.
The elements of the copy are displayed.
Create an array of size twice times the stack size, and use the CopyTo method to start copying the array elements from the middle of the array.
Use the stack constructor again to create a stack copy in the reverse order of the elements; Three empty elements are located at the bottom of the stack.
Use the Contains method to display the string "Four" in the first stack copy. The copy is then cleared by using the clear method, and the Count property shows that the stack is empty.
Using System;
Using System.Collections.Generic;
Class Example
{
public static void Main ()
{
Stack<string> numbers = new Stack<String> ();
Numbers. Push ("one");
Numbers. Push ("both");
Numbers. Push ("three");
Numbers. Push ("four");
Numbers. Push ("five");
A stack can is enumerated without disturbing its contents.
foreach (string number in numbers)
{
Console.WriteLine (number);
}
Console.WriteLine ("\npopping ' {0} '", numbers. Pop ());
Console.WriteLine ("Peek at next item to Destack: {0}", numbers. Peek ());
Console.WriteLine ("Popping ' {0} '", numbers. Pop ());
Create a copy of the stack, using the ToArray method and the
Constructor that accepts an IEnumerable.
Stack stack2 = new stack (numbers. ToArray ());
Console.WriteLine ("\ncontents of the first copy:");
foreach (string number in Stack2)
{
Console.WriteLine (number);
}
Create an array twice the size of the stack and copy the
Elements of the stack, starting at the middle of the
Array.
string[] array2 = new string[numbers. Count * 2];
Numbers. CopyTo (array2, numbers. Count);
Create a second stack, using the constructor that accepts an
IEnumerable (of T).
Stack stack3 = new stack (array2);
Console.WriteLine ("\ncontents of the second copy, with duplicates and nulls:");
foreach (string number in Stack3)
{
Console.WriteLine (number);
}
Console.WriteLine ("\nstack2. Contains (\ "four\") = {0} ", Stack2. Contains ("four"));
Console.WriteLine ("\nstack2. Clear () ");
Stack2. Clear ();
Console.WriteLine ("\nstack2. Count = {0} ", Stack2. Count);
}
}
/* This code example produces the following output:
Five
Four
Three
Both
One
Popping ' five '
Peek at next item to Destack:four
Popping ' four '
Contents of the first copy:
One
Both
Three
Contents of the second copy, with duplicates and nulls:
One
Both
Three
Stack2. Contains ("four") = False
Stack2. Clear ()
Stack2. Count = 0
*/
C # in stack< The use of t> class and the source code analysis of some member functions