Stack(T) Class represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type. : Stack « Collections Data Structure « C# / C Sharp






Stack(T) Class represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

  

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");

        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine(numbers.Pop());
        Console.WriteLine(numbers.Peek());
        Console.WriteLine( numbers.Pop());
        Stack<string> stack2 = new Stack<string>(numbers.ToArray());
        foreach( string number in stack2 )
        {
            Console.WriteLine(number);
        }
        string[] array2 = new string[numbers.Count * 2];
        numbers.CopyTo(array2, numbers.Count);

        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(stack2.Contains("four"));

        stack2.Clear();
        Console.WriteLine(stack2.Count);
    }
}

   
    
  








Related examples in the same category

1.new Stack(new int[] { 1, 2, 3, 4, 5, 6 })
2.new Stack())
3.Implements the stack data type using an arrayImplements the stack data type using an array
4.Stack demo Stack demo
5.Stack to arrayStack to array
6.illustrates the use of a Stackillustrates the use of a Stack
7.A stack class for charactersA stack class for characters
8.Demonstrate the Stack classDemonstrate the Stack class
9.Overflow Stack
10.Thread Safe Stack
11.A Stacked array is an integer array that contains a sequence of ascending integers.
12.FixedSizeStack provides an easy Stack implementation width a fixed size to prevent Stack Overflows in another sense.
13.Stack abstraction that also supports the IList interface