スタック
スタックとは、データ構造の一つであるリストの中で、特に挿入、削除がリストの先頭からしかできないものである。このような構造は、LIFO(後入れ先出し)と呼ばれる。
スタックは、例で言えば、机上に積み上げられた本のような構造をしている。本は上に積み上げられていき、読みたい本は上から順に取っていかなければ取れないという点が、スタックと似ている。
スタックは、情報処理のさまざまな場面に欠かせないデータ構造であるといえる。プログラムがサブルーチンを実行後に呼び出し元に戻る仕組みには、スタックが使われる。サブルーチンを実行する前に、スタックに戻り先を積み上げ(PUSH)、サブルーチンが処理を終わると、戻り先、つまり次にプログラムを実行する番地を取り出す(POP)。文書編集操作の「やり直し」や「元にもどす」などもスタックを利用して実現されているのが一般的である。
Stack クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class Stack Implements ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public class Stack : ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Stack : ICollection, IEnumerable, ICloneable

このコレクションのジェネリック バージョンについては、「System.Collections.Generic.Stack」を参照してください。
Stack の容量は、Stack が保持できる要素数になります。Stack の既定の初期量は 10 です。Stack に要素を追加すると、必要に応じて、再割り当てによって容量が自動的に増加します。
Count がスタックの容量よりも小さい場合、Push は O(1) 操作になります。新しい要素を格納するために容量を増やす必要がある場合、Push は O(n) 操作になります。ここで、n は Count です。Pop は O(1) 操作です。
Stack は、null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。

Stack を作成して値を追加する方法と、その値を出力する方法の例を次に示します。
Imports System Imports System.Collections Imports Microsoft.VisualBasic Public 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 obj As [Object] For Each obj In myCollection Console.Write(" {0}", obj) Next obj Console.WriteLine() End Sub 'PrintValues End Class ' This code produces the following output. ' ' myStack ' Count: 3 ' Values: ! World Hello
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 ) { foreach ( Object obj in myCollection ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. myStack Count: 3 Values: ! World Hello */
using namespace System; using namespace System::Collections; void PrintValues( IEnumerable^ myCollection ); int main() { // Creates and initializes a new Stack. Stack^ myStack = gcnew 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 ); } void PrintValues( IEnumerable^ myCollection ) { IEnumerator^ myEnum = myCollection->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ obj = safe_cast<Object^>(myEnum->Current); Console::Write( " {0}", obj ); } Console::WriteLine(); } /* This code produces the following output. myStack Count: 3 Values: ! World Hello */
import System.*; import System.Collections.*; public class SamplesStack { public static void main(String[] args) { // 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}", System.Convert.ToString(myStack.get_Count())); Console.Write("\tValues:"); PrintValues(myStack); } //main public static void PrintValues(IEnumerable myCollection) { IEnumerator objEnum = myCollection.GetEnumerator(); while (objEnum.MoveNext()) { Console.Write(" {0}", objEnum.get_Current()); } Console.WriteLine(); } //PrintValues } //SamplesStack /* This code produces the following output. myStack Count: 3 Values: ! World Hello */

System.Collections.Stack
Microsoft.VisualC.SymbolTableStack

この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
Stack を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)


Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ ()
アセンブリ: System (system.dll 内)


Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

このコンストラクタおよび Stack ジェネリック クラスのいくつかのメソッドを使用したコード例を次に示します。
このコード例では、既定の容量で文字列のスタックを作成し、Push メソッドを使用して 5 つの文字列をスタックにプッシュします。スタックの要素は列挙されますが、スタックの状態は変化しません。Pop メソッドを使用して、最初の文字列をスタックからポップします。Peek メソッドを使用してスタック内の次の項目を調べてから、Pop メソッドを使用してその項目をスタックからポップします。
ToArray メソッドを使用して配列を作成し、その配列にスタック要素をコピーしてから、IEnumerable を受け取る Stack コンストラクタに配列を渡して、要素の順序を反転したスタックのコピーを作成します。コピーの要素が表示されます。
スタックの 2 倍のサイズの配列が作成され、CopyTo メソッドを使用して配列の中央部で始まる配列要素をコピーします。Stack コンストラクタを再度使用して、要素の順序を反転したスタックのコピーを作成します。その結果、3 つの null 要素が末尾に配置されます。
Contains メソッドを使用して、文字列 "four" がスタックの最初のコピーにあることを示します。その後で、Clear メソッドがコピーをクリアすると、Count プロパティによってスタックが空であることが示されます。
Imports System Imports System.Collections.Generic Module Example Sub Main Dim numbers As New Stack(Of String) numbers.Push("one") numbers.Push("two") numbers.Push("three") numbers.Push("four") numbers.Push("five") ' A stack can be enumerated without disturbing its contents. For Each number As String In numbers Console.WriteLine(number) Next Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop()) Console.WriteLine("Peek at next item to pop: {0}", _ numbers.Peek()) Console.WriteLine("Popping '{0}'", numbers.Pop()) ' Create another stack, using the ToArray method and the ' constructor that accepts an IEnumerable(Of T). Note that ' the order of items on the new stack is reversed. Dim stack2 As New Stack(Of String)(numbers.ToArray()) Console.WriteLine(vbLf & "Contents of the first copy:") For Each number As String In stack2 Console.WriteLine(number) Next ' Create an array twice the size of the stack, compensating ' for the fact that Visual Basic allocates an extra array ' element. Copy the elements of the stack, starting at the ' middle of the array. Dim array2((numbers.Count * 2) - 1) As String numbers.CopyTo(array2, numbers.Count) ' Create a second stack, using the constructor that accepts an ' IEnumerable(Of T). The elements are reversed, with the null ' elements appearing at the end of the stack when enumerated. Dim stack3 As New Stack(Of String)(array2) Console.WriteLine(vbLf & _ "Contents of the second copy, with duplicates and nulls:") For Each number As String In stack3 Console.WriteLine(number) Next Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _ stack2.Contains("four")) Console.WriteLine(vbLf & "stack2.Clear()") stack2.Clear() Console.WriteLine(vbLf & "stack2.Count = {0}", _ stack2.Count) End Sub End Module ' This code example produces the following output: ' 'five 'four 'three 'two 'one ' 'Popping 'five' 'Peek at next item to pop: four 'Popping 'four' ' 'Contents of the first copy: 'one 'two 'three ' 'Contents of the second copy, with duplicates and nulls: 'one 'two 'three ' ' ' ' 'stack2.Contains("four") = False ' 'stack2.Clear() ' 'stack2.Count = 0
using System; using System.Collections.Generic; class Example { public static void Main() { Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); // A stack can be 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<T>. Stack<string> stack2 = new Stack<string>(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<string> stack3 = new Stack<string>(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 two one Popping 'five' Peek at next item to destack: four Popping 'four' Contents of the first copy: one two three Contents of the second copy, with duplicates and nulls: one two three stack2.Contains("four") = False stack2.Clear() stack2.Count = 0 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)



Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ (ICollection)
アセンブリ: mscorlib (mscorlib.dll 内)



Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
要素は、ICollection の IEnumerator によって読み取られる順序と同じ順序で、Stack にコピーされます。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ (ジェネリック IEnumerable)
アセンブリ: System (system.dll 内)

Public Sub New ( _ collection As IEnumerable(Of T) _ )
Dim collection As IEnumerable(Of T) Dim instance As New Stack(Of T)(collection)
public Stack ( IEnumerable<T> collection )
public: Stack ( IEnumerable<T>^ collection )
public Stack ( IEnumerable<T> collection )
public function Stack ( collection : IEnumerable<T> )

例外の種類 | 条件 |
---|---|
ArgumentNullException | collection が null 参照 (Visual Basic では Nothing) です。 |

Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
要素は、コレクションの IEnumerator によって読み取られる順序と同じ順序で、Stack にコピーされます。
このコンストラクタは O(n) 操作です。ここで、n は collection 内の要素数です。

このコンストラクタおよび Stack ジェネリック クラスのいくつかのメソッドを使用したコード例を次に示します。
このコード例では、既定の容量で文字列のスタックを作成し、Push メソッドを使用して 5 つの文字列をスタックにプッシュします。スタックの要素は列挙されますが、スタックの状態は変化しません。Pop メソッドを使用して、最初の文字列をスタックからポップします。Peek メソッドを使用してスタック内の次の項目を調べてから、Pop メソッドを使用してその項目をスタックからポップします。
ToArray メソッドを使用して配列を作成し、その配列にスタック要素をコピーしてから、IEnumerable を受け取る Stack コンストラクタに配列を渡して、要素の順序を反転したスタックのコピーを作成します。コピーの要素が表示されます。
スタックの 2 倍のサイズの配列が作成され、CopyTo メソッドを使用して配列の中央部で始まる配列要素をコピーします。Stack コンストラクタを再度使用して、要素の順序を反転したスタックのコピーを作成します。その結果、3 つの null 要素が末尾に配置されます。
Contains メソッドを使用して、文字列 "four" がスタックの最初のコピーにあることを示します。その後で、Clear メソッドがコピーをクリアすると、Count プロパティによってスタックが空であることが示されます。
Imports System Imports System.Collections.Generic Module Example Sub Main Dim numbers As New Stack(Of String) numbers.Push("one") numbers.Push("two") numbers.Push("three") numbers.Push("four") numbers.Push("five") ' A stack can be enumerated without disturbing its contents. For Each number As String In numbers Console.WriteLine(number) Next Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop()) Console.WriteLine("Peek at next item to pop: {0}", _ numbers.Peek()) Console.WriteLine("Popping '{0}'", numbers.Pop()) ' Create another stack, using the ToArray method and the ' constructor that accepts an IEnumerable(Of T). Note that ' the order of items on the new stack is reversed. Dim stack2 As New Stack(Of String)(numbers.ToArray()) Console.WriteLine(vbLf & "Contents of the first copy:") For Each number As String In stack2 Console.WriteLine(number) Next ' Create an array twice the size of the stack, compensating ' for the fact that Visual Basic allocates an extra array ' element. Copy the elements of the stack, starting at the ' middle of the array. Dim array2((numbers.Count * 2) - 1) As String numbers.CopyTo(array2, numbers.Count) ' Create a second stack, using the constructor that accepts an ' IEnumerable(Of T). The elements are reversed, with the null ' elements appearing at the end of the stack when enumerated. Dim stack3 As New Stack(Of String)(array2) Console.WriteLine(vbLf & _ "Contents of the second copy, with duplicates and nulls:") For Each number As String In stack3 Console.WriteLine(number) Next Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _ stack2.Contains("four")) Console.WriteLine(vbLf & "stack2.Clear()") stack2.Clear() Console.WriteLine(vbLf & "stack2.Count = {0}", _ stack2.Count) End Sub End Module ' This code example produces the following output: ' 'five 'four 'three 'two 'one ' 'Popping 'five' 'Peek at next item to pop: four 'Popping 'four' ' 'Contents of the first copy: 'one 'two 'three ' 'Contents of the second copy, with duplicates and nulls: 'one 'two 'three ' ' ' ' 'stack2.Contains("four") = False ' 'stack2.Clear() ' 'stack2.Count = 0
using System; using System.Collections.Generic; class Example { public static void Main() { Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); // A stack can be 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<T>. Stack<string> stack2 = new Stack<string>(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<string> stack3 = new Stack<string>(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 two one Popping 'five' Peek at next item to destack: four Popping 'four' Contents of the first copy: one two three Contents of the second copy, with duplicates and nulls: one two three stack2.Contains("four") = False stack2.Clear() stack2.Count = 0 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack ジェネリック クラス
Stack メンバ
System.Collections.Generic 名前空間
ICollection ジェネリック インターフェイス
IEnumerator ジェネリック インターフェイス
Stack コンストラクタ (Int32)
アセンブリ: System (system.dll 内)



Stack の容量は、Stack が保持できる要素数になります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、Stack に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack コンストラクタ

名前 | 説明 |
---|---|
Stack () | 空で、既定の初期量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Stack (ICollection) | 指定したコレクションからコピーした要素を格納し、コピーされる要素の数と同じ初期量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Stack (Int32) | 空で、指定した初期量または既定の初期量のうち大きい方の初期量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

Stack コンストラクタ

名前 | 説明 |
---|---|
Stack () | 空で、既定の初期量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Stack (ジェネリック IEnumerable) | 指定したコレクションからコピーした要素を格納し、コピーされる要素の数を格納できるだけの容量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Stack (Int32) | 空で、指定した初期量または既定の初期量のうち大きい方の初期量を備えた、Stack クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

Stack ジェネリック クラス
アセンブリ: System (system.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(False)> _ Public Class Stack(Of T) Implements IEnumerable(Of T), ICollection, _ IEnumerable
[SerializableAttribute] [ComVisibleAttribute(false)] public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
[SerializableAttribute] [ComVisibleAttribute(false)] generic<typename T> public ref class Stack : IEnumerable<T>, ICollection, IEnumerable

Stack の容量は、Stack が保持できる要素数になります。この実装では、Stack の既定の初期量は 10 ですが、この既定値は将来の .NET Framework SDK のバージョンで変更される可能性があります。Stack に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。容量を減らすには、TrimExcess を呼び出します。
Count がスタックの容量よりも小さい場合、Push は O(1) 操作になります。新しい要素を格納するために容量を増やす必要がある場合、Push は O(n) 操作になります。ここで、n は Count です。Pop は O(1) 操作です。
Stack は、null 参照 (Visual Basic では Nothing) を参照型に対して有効な値として受け取り、要素の重複を許可します。

Stack ジェネリック クラスのいくつかのメソッドを使用したコード例を次に示します。このコード例では、既定の容量で文字列のスタックを作成し、Push メソッドを使用して 5 つの文字列をスタックにプッシュします。スタックの要素は列挙されますが、スタックの状態は変化しません。Pop メソッドを使用して、最初の文字列をスタックからポップします。Peek メソッドを使用してスタック内の次の項目を調べてから、Pop メソッドを使用してその項目をスタックからポップします。
ToArray メソッドを使用して配列を作成し、その配列にスタック要素をコピーしてから、IEnumerable を受け取る Stack コンストラクタに配列を渡して、要素の順序を反転したスタックのコピーを作成します。コピーの要素が表示されます。
スタックの 2 倍のサイズの配列が作成され、CopyTo メソッドを使用して配列の中央部で始まる配列要素をコピーします。Stack コンストラクタを再度使用して、要素の順序を反転したスタックのコピーを作成します。その結果、3 つの null 要素が末尾に配置されます。
Contains メソッドを使用して、文字列 "four" がスタックの最初のコピーにあることを示します。その後で、Clear メソッドがコピーをクリアすると、Count プロパティによってスタックが空であることが示されます。
Imports System Imports System.Collections.Generic Module Example Sub Main Dim numbers As New Stack(Of String) numbers.Push("one") numbers.Push("two") numbers.Push("three") numbers.Push("four") numbers.Push("five") ' A stack can be enumerated without disturbing its contents. For Each number As String In numbers Console.WriteLine(number) Next Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop()) Console.WriteLine("Peek at next item to pop: {0}", _ numbers.Peek()) Console.WriteLine("Popping '{0}'", numbers.Pop()) ' Create another stack, using the ToArray method and the ' constructor that accepts an IEnumerable(Of T). Note that ' the order of items on the new stack is reversed. Dim stack2 As New Stack(Of String)(numbers.ToArray()) Console.WriteLine(vbLf & "Contents of the first copy:") For Each number As String In stack2 Console.WriteLine(number) Next ' Create an array twice the size of the stack, compensating ' for the fact that Visual Basic allocates an extra array ' element. Copy the elements of the stack, starting at the ' middle of the array. Dim array2((numbers.Count * 2) - 1) As String numbers.CopyTo(array2, numbers.Count) ' Create a second stack, using the constructor that accepts an ' IEnumerable(Of T). The elements are reversed, with the null ' elements appearing at the end of the stack when enumerated. Dim stack3 As New Stack(Of String)(array2) Console.WriteLine(vbLf & _ "Contents of the second copy, with duplicates and nulls:") For Each number As String In stack3 Console.WriteLine(number) Next Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _ stack2.Contains("four")) Console.WriteLine(vbLf & "stack2.Clear()") stack2.Clear() Console.WriteLine(vbLf & "stack2.Count = {0}", _ stack2.Count) End Sub End Module ' This code example produces the following output: ' 'five 'four 'three 'two 'one ' 'Popping 'five' 'Peek at next item to pop: four 'Popping 'four' ' 'Contents of the first copy: 'one 'two 'three ' 'Contents of the second copy, with duplicates and nulls: 'one 'two 'three ' ' ' ' 'stack2.Contains("four") = False ' 'stack2.Clear() ' 'stack2.Count = 0
using System; using System.Collections.Generic; class Example { public static void Main() { Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); // A stack can be 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<T>. Stack<string> stack2 = new Stack<string>(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<string> stack3 = new Stack<string>(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 two one Popping 'five' Peek at next item to destack: four Popping 'four' Contents of the first copy: one two three Contents of the second copy, with duplicates and nulls: one two three stack2.Contains("four") = False stack2.Clear() stack2.Count = 0 */

System.Collections.Generic.Stack

この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
コレクションが変更されない限り、Stack では、複数の読み込み操作が同時に発生しても問題ありません。ただし、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack プロパティ
Stack プロパティ
Stack メソッド

名前 | 説明 | |
---|---|---|
![]() | Clear | Stack からすべてのオブジェクトを削除します。 |
![]() | Contains | ある要素が Stack 内に存在するかどうかを判断します。 |
![]() | CopyTo | 既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetEnumerator | Stack の列挙子を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | Peek | Stack の先頭にあるオブジェクトを削除せずに返します。 |
![]() | Pop | Stack の先頭にあるオブジェクトを削除し、返します。 |
![]() | Push | Stack の先頭にオブジェクトを挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToArray | Stack を新しい配列にコピーします。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
![]() | TrimExcess | Stack 内にある実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | System.Collections.Generic.IEnumerable<T>.GetEnumerator | コレクションを反復処理する列挙子を返します。 |
![]() | System.Collections.ICollection.CopyTo | ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。 |
![]() | System.Collections.IEnumerable.GetEnumerator | コレクションを反復処理する列挙子を返します。 |

Stack メソッド

名前 | 説明 | |
---|---|---|
![]() | Clear | Stack からすべてのオブジェクトを削除します。 |
![]() | Clone | Stack の簡易コピーを作成します。 |
![]() | Contains | ある要素が Stack 内に存在するかどうかを判断します。 |
![]() | CopyTo | 既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetEnumerator | Stack の IEnumerator を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | Peek | Stack の先頭にあるオブジェクトを削除せずに返します。 |
![]() | Pop | Stack の先頭にあるオブジェクトを削除し、返します。 |
![]() | Push | Stack の先頭にオブジェクトを挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Synchronized | Stack 用の同期された (スレッド セーフな) ラッパーを返します。 |
![]() | ToArray | Stack を新しい配列にコピーします。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

Stack メンバ
任意の同じ型のインスタンスの、可変サイズの後入れ先出し (LIFO) コレクションを表します。
Stack ジェネリック型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Clear | Stack からすべてのオブジェクトを削除します。 |
![]() | Contains | ある要素が Stack 内に存在するかどうかを判断します。 |
![]() | CopyTo | 既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetEnumerator | Stack の列挙子を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | Peek | Stack の先頭にあるオブジェクトを削除せずに返します。 |
![]() | Pop | Stack の先頭にあるオブジェクトを削除し、返します。 |
![]() | Push | Stack の先頭にオブジェクトを挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToArray | Stack を新しい配列にコピーします。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
![]() | TrimExcess | Stack 内にある実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | System.Collections.Generic.IEnumerable<T>.GetEnumerator | コレクションを反復処理する列挙子を返します。 |
![]() | System.Collections.ICollection.CopyTo | ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。 |
![]() | System.Collections.IEnumerable.GetEnumerator | コレクションを反復処理する列挙子を返します。 |
![]() | System.Collections.ICollection.SyncRoot | ICollection へのアクセスを同期するために使用できるオブジェクトを取得します。 |

Stack メンバ
オブジェクトの単純な後入れ先出し (LIFO) 非ジェネリック コレクションを表します。
Stack データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Clear | Stack からすべてのオブジェクトを削除します。 |
![]() | Clone | Stack の簡易コピーを作成します。 |
![]() | Contains | ある要素が Stack 内に存在するかどうかを判断します。 |
![]() | CopyTo | 既存の 1 次元の Array に Stack をコピーします。コピー操作は、配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetEnumerator | Stack の IEnumerator を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | Peek | Stack の先頭にあるオブジェクトを削除せずに返します。 |
![]() | Pop | Stack の先頭にあるオブジェクトを削除し、返します。 |
![]() | Push | Stack の先頭にオブジェクトを挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Synchronized | Stack 用の同期された (スレッド セーフな) ラッパーを返します。 |
![]() | ToArray | Stack を新しい配列にコピーします。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Stack
stack(スタック)は、特定の数のチップを指すこともある。ほとんどのchip racks(チップラック)は、20枚を1スタックの形で収容するタイプのものである。プレーヤーの中には、自分のチップを好みの大きさのスタックにして並べている人も多い。私(Dan)は、10チップのスタックが好きであるが、20とか30チップのスタックにしている人が多いようである。
I was doing well earlier, but my stacks have been dwindling.
(最初は好調だったんだけど、チップの量はほとんど変化せずそのままだねえ)。
Stack
スタック (曖昧さ回避)
スタック、スタッキング
stack
- 積み重ね、山積み、棚。
- スタック - コンピュータのデータ構造の一種。特に、コールスタック。
- 有限会社スタック - ゲームメーカー。
- π-スタック - 有機分子で、2つの芳香環が重なること。
- スタック (層理論) - スキームや代数的空間の一般化。
- スポーツスタッキングでの用語。
- 煙突の集合体。
- 複数のアンテナを、垂直や水平に一定間隔を置いて配置して、出力を合流させること。
- プロトコル・スタック - コンピュータネットワーク用通信プロトコルスイートのソフトウェア実装。
- ソリューションスタック(ソフトウェアスタック)- アプリケーションのサポートに必要なソフトウェア要素の集合体。
- HyperCardで記述されたハイパーテキストデータの呼称(カードの集合体から)。
- アルティメットでのオフェンスのポジショニング。
- トレーディングカードゲームなどで、複数のカードの効果を後入先出法で処理するルール、またはそのための領域のこと。コンピュータのデータ構造のスタックに由来。
- ウォー・シミュレーションゲームなどで、複数の駒が同一の升目にある状態。駒を積み重ねることから。
- 時系列データを一定データ数ずつまたは一定データ数周期で加算すること。→「移動平均」を参照
- Adobe Photoshop Lightroomで、1つのサムネイルに複数の写真を登録すること。
- 英語圏の姓。
- Stack (Haskell) - Haskellのパッケージ管理ツール
stuck
- 英語 stick の過去形または過去分詞[1]。
- ドイツ語圏の姓。シュトゥック。
- ハンス=ヨアヒム・スタック (Hans-Joachim Stuck) - ドイツ出身のレーシングドライバー。
脚注
関連項目
stack
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2019/04/29 13:43 UTC 版)
「Standard Template Library」の記事における「stack」の解説
スタック。(FILO; First In, Last Out)
※この「stack」の解説は、「Standard Template Library」の解説の一部です。
「stack」を含む「Standard Template Library」の記事については、「Standard Template Library」の概要を参照ください。
- stackのページへのリンク