堆栈表示对象的后进先出集合。当您需要对项目进行后进先出的访问时使用。在列表中添加项目时,称为推送项目,而在删除项目时,则称为弹出项目。此类位于System.Collections命名空间下。
堆栈类的特征:
- 堆栈的容量是堆栈可以容纳的元素数量。将元素添加到堆栈后,容量会根据需要通过重新分配自动增加。
- 如果Count小于堆栈的容量,则Push是O(1)运算。如果需要增加容量以容纳新元素,则Push变为O(n)操作,其中n为Count。 Pop是O(1)运算。
- 堆栈接受null作为有效值,并允许重复的元素。
建设者
Constructor | Description |
---|---|
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 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 the default initial capacity, whichever is greater. |
例子:
// C# code to create a Stack
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("1st Element");
myStack.Push("2nd Element");
myStack.Push("3rd Element");
myStack.Push("4th Element");
myStack.Push("5th Element");
myStack.Push("6th Element");
// Displaying the count of elements
// contained in the Stack
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
// Displaying the top element of Stack
// without removing it from the Stack
Console.WriteLine("Element at the top is : " + myStack.Peek());
// Displaying the top element of Stack
// without removing it from the Stack
Console.WriteLine("Element at the top is : " + myStack.Peek());
// Displaying the count of elements
// contained in the Stack
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
}
}
输出:
Total number of elements in the Stack are : 6
Element at the top is : 6th Element
Element at the top is : 6th Element
Total number of elements in the Stack are : 6
特性
Property | Description |
---|---|
Count | Gets the number of elements contained in the Stack. |
IsSynchronized | Gets a value indicating whether access to the Stack is synchronized (thread safe). |
SyncRoot | Gets an object that can be used to synchronize access to the Stack. |
例子:
// C# code to Get the number of
// elements contained in the Stack
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("Chandigarh");
myStack.Push("Delhi");
myStack.Push("Noida");
myStack.Push("Himachal");
myStack.Push("Punjab");
myStack.Push("Jammu");
// Displaying the count of elements
// contained in the Stack
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
}
}
输出:
Total number of elements in the Stack are : 6
方法
Method | Description |
---|---|
Clear() | Removes all objects from the Stack. |
Clone() | Creates a shallow copy of the Stack. |
Contains(Object) | Determines whether an element is in the Stack. |
CopyTo(Array, Int32) | Copies the Stack to an existing one-dimensional Array, starting at the specified array index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an IEnumerator for the Stack. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
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(Object) | Inserts an object at the top of the Stack. |
Synchronized(Stack) | Returns a synchronized (thread safe) wrapper for the Stack. |
ToArray() | Copies the Stack to a new array. |
ToString() | Returns a string that represents the current object. |
例子 :
// C# code to Remove all
// objects from the Stack
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("1st Element");
myStack.Push("2nd Element");
myStack.Push("3rd Element");
myStack.Push("4th Element");
myStack.Push("5th Element");
myStack.Push("6th Element");
// Displaying the count of elements
// contained in the Stack before
// removing all the elements
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
// Removing all elements from Stack
myStack.Clear();
// Displaying the count of elements
// contained in the Stack after
// removing all the elements
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
}
}
输出:
Total number of elements in the Stack are : 6
Total number of elements in the Stack are : 0
例子 :
// C# code to Check if a Stack
// contains an element
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack of strings
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("Geeks");
myStack.Push("Geeks Classes");
myStack.Push("Noida");
myStack.Push("Data Structures");
myStack.Push("GeeksforGeeks");
// Checking whether the element is
// present in the Stack or not
// The function returns True if the
// element is present in the Stack, else
// returns False
Console.WriteLine(myStack.Contains("GeeksforGeeks"));
}
}
输出:
True
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.stack?view=netframework-4.7.2