C# stack (Stack)

Table of contents

I. Overview

2. Basic usage

1. Push into the stack

2. Pop out

Pop method

Peek method

3. Determine whether the element exists

4. Get the length of the Stack

5. Traverse the Stack

6. Empty the container

7.Stack generic class

3. End


I. Overview

A stack represents a simple last-in-first-out (LIFO) non-generic collection of objects.

Stack, like List, is a storage container. It follows the principle of first-in, last-out. It can store any type, but it cannot get to the specified location. It can only be stored and taken out. After taking out the elements, the elements inside the Stack are automatically deleted.

Detailed API reference: Stack class (System.Collections) | Microsoft Learn

2. Basic usage

1. Push into the stack

The push method is used to push into the stack, and any type can be added here

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            Console.ReadKey();
        }
    }
}

2. Pop out

There are two commonly used methods for popping the stack, the first one:

Pop method

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            var value = stack.Pop();
            Console.WriteLine(value);
            Console.WriteLine("count:" + stack.Count);

            Console.ReadKey();
        }
    }
}

run

It can be seen here that the last added 4.5 was taken out first, and it was also deleted from the stack at the same time. At this time, the length is 3. This is the first-in-last-out principle described in the overview, which sounds a bit incomprehensible  , In fact, whoever is the last to join will be the first to go out, and the last one in the team will be caught and pulled out.

The second type:

Peek method

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            var value = stack.Peek();
            Console.WriteLine(value);
            Console.WriteLine("count:" + stack.Count);

            Console.ReadKey();
        }
    }
}

run

Peek can take out an element according to the principle of last-in-first-out. It does not delete the element like the Pop method, but it can only take one element. See the code below. Repeated acquisition is useless. If you want to take data one by one When you come out, you still have to use the Pop method honestly.

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            var value1 = stack.Peek();
            var value2 = stack.Peek();
            var value3 = stack.Peek();
            Console.WriteLine(value1);
            Console.WriteLine(value2);
            Console.WriteLine(value3);

            Console.ReadKey();
        }
    }
}

run

3. Determine whether the element exists

Use the Contains method to determine whether an element exists, as shown in the following code

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            Console.WriteLine("是否存在:" + stack.Contains(4.5));

            Console.ReadKey();
        }
    }
}

run

4. Get the length of the Stack

The acquisition of the length is the same as List, using the Count property

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            Console.WriteLine("长度:" + stack.Count);

            Console.ReadKey();
        }
    }
}

run

5. Traverse the Stack

Stack can be traversed using foreach without removing elements

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("长度:" + stack.Count);

            Console.ReadKey();
        }
    }
}

 run

6. Empty the container

Clear the stack using the Clear method

using System;
using System.Collections;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push("a");
            stack.Push("b");
            stack.Push(3);
            stack.Push(4.5);

            stack.Clear();

            Console.WriteLine("长度:" + stack.Count);

            Console.ReadKey();
        }
    }
}

run

7.Stack generic class

The usage of the Stack generic class and Stack is actually no different. The Stack generic class just has an additional constraint when used. It cannot be the same as the standard form of Stack. You can add any type to the stack, but use a fixed element type

using System;
using System.Collections.Generic;

namespace Stack_Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack<string> stack = new Stack<string>();
            //将元素入栈
            stack.Push("a");
            stack.Push("b");
            stack.Push("c");

            //栈的元素个数
            int count = stack.Count;
            //是否包含指定的元素
            bool b = stack.Contains("a");

            //Stack.Peek() 方法返回顶部的对象而不将其从堆栈中移除
            string name = stack.Peek();

            // Pop 把元素出栈,栈中就没有这个元素了
            string s1 = stack.Pop();
            Console.WriteLine(s1);
            string s2 = stack.Pop();
            Console.WriteLine(s2);
            string s3 = stack.Pop();
            Console.WriteLine(s3);

            Console.ReadKey();
        }
    }
}

3. End

Finally, let's take a look at the features of Stack:

First in, last out, there is boxing and unboxing, and any type is stored. You cannot use the for loop to traverse and view elements, and you cannot obtain the element at the specified position. You can only view and obtain the top element of the stack.

end

Guess you like

Origin blog.csdn.net/qq_38693757/article/details/130880385