using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp_stack1 { /// /// System.Collections.Generic名前空間のStackクラスを呼び出すStackMainクラスの定義。 /// class StackMain { /// /// アプリケーションのメインエントリポイント /// /// [STAThread] static void Main(string[] args) { // スタック(stack)の作成 // スタック(stack)とは、LIFO(Last In First Out)型のデータ構造 // テキストP17の応用 Stack st = new Stack(); // スタック(stack)にデータを格納 // StackクラスのPush()メソッドによるデータの追加 st.Push("ADSL"); st.Push("BIOS"); st.Push("CATV"); // スタックサイズの確認 int nSize = st.Count; Console.WriteLine("Stack size : {0}", nSize); // 列挙子(Enumerator)によってデータを削除せずに取り出す IEnumerator ie = st.GetEnumerator(); while (ie.MoveNext()) Console.WriteLine(ie.Current); // スタック(stack)から順にデータを削除しながら取り出す // Pop()メソッドをコールするとサイズが変化する。 nSize = st.Count; for (int i = 0; i < nSize; i++) { Console.WriteLine(st.Pop()); } // スタック(stack)サイズの確認 nSize = st.Count; Console.WriteLine("Stack size : {0}", nSize); // コンソール画面を一時停止する ConsoleKeyInfo conKeyInfo = Console.ReadKey(true); ConsoleKey conKey = conKeyInfo.Key; } } }