NET Framework 类库 Stack 类 全部显示 表示对象的简单的后进先出集合。有关此类型所有成员的列表,请参阅 Stack 成员。System.Object System.Collections.Stack[Visual Basic] <Serializable> Public Class Stack Implements ICollection, IEnumerable, ICloneable[C#] [Serializable] public class Stack : ICollection, IEnumerable, ICloneable[C++] [Serializable] public __gc class Stack : public ICollection, IEnumerable, ICloneable[JScript] public Serializable class Stack implements ICollection, IEnumerable, ICloneable线程安全 此类型的公共静态(在 Visual Basic 中为 Shared)成员对于多线程操作是安全的。不能保证实例成员是线程安全的。若要保证 Stack 的线程安全,则必须通过由 Synchronized 方法返回的包装来执行所有操作。通过集合枚举在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。备注 将 Stack 实现为循环缓冲区。如果 Count 小于堆栈的容量,则 Push 为 O(1) 操作。如果需要增加容量以容纳新元素,则 Push 成为 O(n) 操作,其中 n 为 Count。Pop 为 O(1) 操作。Stack 接受空引用(Visual Basic 中为 Nothing)作为有效值并且允许重复的元素。示例 [Visual Basic, C#, C++] 下列示例说明如何创建 Stack 并向其添加值,以及如何打印出其值。[Visual Basic] Imports System Imports System.Collections Imports Microsoft.VisualBasicPublic Class SamplesStack
Public Shared Sub Main()
' Creates and initializes a new Stack. Dim myStack As New Stack() myStack.Push("Hello") myStack.Push("World") myStack.Push("!")
' Displays the properties and values of the Stack. Console.WriteLine("myStack") Console.WriteLine(ControlChars.Tab & "Count: {0}", myStack.Count) Console.Write(ControlChars.Tab & "Values:") PrintValues(myStack) End Sub
Public Shared Sub PrintValues(myCollection As IEnumerable) Dim myEnumerator As System.Collections.IEnumerator = _ myCollection.GetEnumerator() While myEnumerator.MoveNext() Console.Write(ControlChars.Tab & "{0}", myEnumerator.Current) End While Console.WriteLine() End Sub End Class' This code produces the following output. ' ' myStack ' Count: 3 ' Values: ! World Hello[C#] using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "myStack" ); Console.WriteLine( "\tCount: {0}", myStack.Count ); Console.Write( "\tValues:" ); PrintValues( myStack ); } public static void PrintValues( IEnumerable myCollection ) { System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output.myStack Count: 3 Values: ! World Hello */
表示对象的简单的后进先出集合。有关此类型所有成员的列表,请参阅 Stack 成员。System.Object
System.Collections.Stack[Visual Basic]
<Serializable>
Public Class Stack
Implements ICollection, IEnumerable, ICloneable[C#]
[Serializable]
public class Stack : ICollection, IEnumerable, ICloneable[C++]
[Serializable]
public __gc class Stack : public ICollection, IEnumerable,
ICloneable[JScript]
public
Serializable
class Stack implements ICollection, IEnumerable, ICloneable线程安全
此类型的公共静态(在 Visual Basic 中为 Shared)成员对于多线程操作是安全的。不能保证实例成员是线程安全的。若要保证 Stack 的线程安全,则必须通过由 Synchronized 方法返回的包装来执行所有操作。通过集合枚举在本质上不是一个线程安全的过程。甚至在对集合进行同步处理时,其他线程仍可以修改该集合,这会导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。备注
将 Stack 实现为循环缓冲区。如果 Count 小于堆栈的容量,则 Push 为 O(1) 操作。如果需要增加容量以容纳新元素,则 Push 成为 O(n) 操作,其中 n 为 Count。Pop 为 O(1) 操作。Stack 接受空引用(Visual Basic 中为 Nothing)作为有效值并且允许重复的元素。示例
[Visual Basic, C#, C++] 下列示例说明如何创建 Stack 并向其添加值,以及如何打印出其值。[Visual Basic]
Imports System
Imports System.Collections
Imports Microsoft.VisualBasicPublic Class SamplesStack
Public Shared Sub Main()
' Creates and initializes a new Stack.
Dim myStack As New Stack()
myStack.Push("Hello")
myStack.Push("World")
myStack.Push("!")
' Displays the properties and values of the Stack.
Console.WriteLine("myStack")
Console.WriteLine(ControlChars.Tab & "Count: {0}", myStack.Count)
Console.Write(ControlChars.Tab & "Values:")
PrintValues(myStack)
End Sub
Public Shared Sub PrintValues(myCollection As IEnumerable)
Dim myEnumerator As System.Collections.IEnumerator = _
myCollection.GetEnumerator()
While myEnumerator.MoveNext()
Console.Write(ControlChars.Tab & "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class' This code produces the following output.
'
' myStack
' Count: 3
' Values: ! World Hello[C#]
using System;
using System.Collections;
public class SamplesStack { public static void Main() { // Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push("Hello");
myStack.Push("World");
myStack.Push("!"); // Displays the properties and values of the Stack.
Console.WriteLine( "myStack" );
Console.WriteLine( "\tCount: {0}", myStack.Count );
Console.Write( "\tValues:" );
PrintValues( myStack );
}
public static void PrintValues( IEnumerable myCollection ) {
System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.myStack
Count: 3
Values: ! World Hello
*/
你给的是利用C#的Stack
但是我的问题是如何自己构建一个通用的栈类替代C#的Stack类
各位高手继续给看看