static void Main() { Stack <Int32> intStack = new Stack <Int32>(); for (int i = 0; i < 8; i++) { intStack.Push(i * 5); } Console.Write("intStack values:\t"); PrintValues(intStack); Console.WriteLine("\n(Pop)\t{0}", intStack.Pop()); Console.Write("intStack values:\t"); PrintValues(intStack); Console.WriteLine("\n(Pop)\t{0}", intStack.Pop()); Console.Write("intStack values:\t"); PrintValues(intStack); Console.WriteLine("\n(Peek)\t{0}", intStack.Peek()); Console.Write("intStack values:\t"); PrintValues(intStack); int[] targetArray = new int[12]; for (int i = 0; i < targetArray.Length; i++) { targetArray[i] = i * 100 + 100; } Console.WriteLine("\nTarget array: "); PrintValues(targetArray); intStack.CopyTo(targetArray, 6); Console.WriteLine("\nTarget array after copy: "); PrintValues(targetArray); }
static void Main() { Stack <Int32> intStack = new Stack <Int32>(); // populate the array for (int i = 0; i < 8; i++) { intStack.Push(i * 5); } // Display the Stack. Console.Write("intStack values:\t"); PrintValues(intStack); // Remove an element from the stack. Console.WriteLine("\n(Pop)\t{0}", intStack.Pop()); // Display the Stack. Console.Write("intStack values:\t"); PrintValues(intStack); // Remove another element from the stack. Console.WriteLine("\n(Pop)\t{0}", intStack.Pop()); // Display the Stack. Console.Write("intStack values:\t"); PrintValues(intStack); // View the first element in the // Stack but do not remove. Console.WriteLine("\n(Peek) \t{0}", intStack.Peek()); // Display the Stack. Console.Write("intStack values:\t"); PrintValues(intStack); // declare an array object which will // hold 12 integers int[] targetArray = new int[12]; for (int i = 0; i < targetArray.Length; i++) { targetArray[i] = i * 100 + 100; } // Display the values of the target Array instance. Console.WriteLine("\nTarget array: "); PrintValues(targetArray); // Copy the entire source Stack to the // target Array instance, starting at index 6. intStack.CopyTo(targetArray, 6); // Display the values of the target Array instance. Console.WriteLine("\nTarget array after copy: "); PrintValues(targetArray); }
public static void Main() { // "Represents a variable size last-in-first-out (LIFO) collection" (mdoc). Stack <string> numbers = new Stack <string>(); // "Inserts an object at the top of the Stack<T>" (mdoc). numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); // Prints each elements in a stack. foreach (string number in numbers) { Console.WriteLine(number); } // "Removes and returns the object at the top of the Stack<T>" (mdoc). Console.WriteLine("\nPopping '{0}'", numbers.Pop()); // "Returns the object at the top of the Stack<T> without removing it" (mdoc). Console.WriteLine("Peek at next item to destack: {0}", numbers.Peek()); Console.WriteLine("Popping '{0}'", numbers.Pop()); // Makes an Array out of "numbers" stack and feed it into a stack // which results in a reverse stack. Stack <string> stack2 = new Stack <string>(numbers.ToArray()); // Prints reversed stack. Console.WriteLine("\nContents of the first copy:"); foreach (string number in stack2) { Console.WriteLine(number); } // Makes an Array of string with twice the size of "numbers" Stack. string[] array2 = new string[numbers.Count * 2]; // Copies numbers Stack to an Array starting at index numbers.Count. numbers.CopyTo(array2, numbers.Count); // Makes a Stack out of an Array. Stack <string> stack3 = new Stack <string>(array2); // Prints a new Stack. Console.WriteLine("\nContents of the second copy, with duplicates and nulls:"); foreach (string number in stack3) { Console.WriteLine(number); } // Prints "True" if a Stack contains "four". Otherwise, print "False". Console.WriteLine("\nstack2.Contains(\"four\") = {0}", stack2.Contains("four")); Console.WriteLine("\nstack2.Clear()"); // "Removes all objects from the Stack<T>" (mdoc). stack2.Clear(); // "Gets the number of elements contained in the Stack<T>" (mdoc). Console.WriteLine("\nstack2.Count = {0}", stack2.Count); }