C# 堆栈<T>

Stack<T> 是一个泛型类,它使用 **后进先出(LIFO)** 原理来组织指定数据类型的元素。例如,

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a stack 
        Stack<string> country = new Stack<string>();

        // push "USA" and "India" to the country stack
        country.Push("USA");
        country.Push("India");

        // print elements inside the country Stack 
        foreach (string item in country)
        {
            Console.WriteLine(item);
        }
    }
}

输出

India
USA

在这里,country 是一个包含字符串元素("USA""India")的堆栈。

我们将详细了解 Stack<T>

注意: 在 C# 中,有两种类型的堆栈集合类

  • Stack<T> - 泛型
  • Stack - 非泛型

建议使用泛型 Stack<T> 类。


堆栈实现

在堆栈中,元素以 **后进先出**(LIFO)方式存储和访问。也就是说,元素被添加到堆栈的顶部,并从堆栈的顶部移除。

Working of stack data structure
堆栈数据结构的工作原理

创建堆栈

要在 C# 中创建 Stack<T>,我们需要使用 System.Collection.Generic 命名空间。以下是在 C# 中创建 Stack<T> 的方法:

Stack<dataType> stackName = new Stack<dataType>();

在这里,dataType 表示堆栈的类型。例如,

// create Integer type stack
Stack<int> stack1 = new Stack<int>();

// create String type stack
Stack<string> stack2 = new Stack<string>();

C# 堆栈方法

C# 提供了 **3** 个主要的 Stack<T> 方法。这些方法是:

  1. Push() - 向堆栈顶部添加元素
  2. Pop() - 从堆栈顶部移除并返回一个元素
  3. Peek() - 返回堆栈顶部的元素而不移除它

让我们详细了解每种方法。


Push() 方法

要向堆栈顶部添加元素,我们使用 Push() 方法。例如,

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a stack and push two elements 
        Stack<int> numbers = new Stack<int>();

// add two elements to the stack numbers.Push(1); numbers.Push(5);
// print elements inside the numbers Stack foreach (int item in numbers) { Console.WriteLine(item); } } }

输出

5
1

在上面的示例中,我们创建了一个名为 numbersStack<T> 类。

然后,我们使用 Push() 方法将 15 添加到堆栈中。之后,我们使用 foreach 循环打印了这些元素。

由于堆栈遵循 LIFO 原理,最后推入的元素(5)在输出中首先显示。


Pop() 方法

要从堆栈顶部移除元素,我们使用 Pop() 方法。例如,

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a stack  
        Stack<string> subject = new Stack<string>();
        subject.Push("Math");
        subject.Push("English");

// pop the element from top of the stack Console.WriteLine("Popped element: " + subject.Pop());
} }

输出

Popped element: English

在上面的示例中,我们使用 Pop() 方法从 subject 堆栈的顶部移除了一个元素。


Peek() 方法

Peek() 方法返回堆栈顶部的对象而不将其移除。例如,

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // create a stack  
        Stack<string> cars = new Stack<string>();
        cars.Push("Toyota");
        cars.Push("BMW");

        // return the element at top of the Stack 
Console.WriteLine("Element at top of stack: " + cars.Peek());
} }

输出

Element at top of stack: BMW

在这里,我们显示了 cars 堆栈顶部的元素。


常见问题

如何检查堆栈中是否存在某个元素?

我们可以使用 Contains() 方法来检查堆栈中是否存在某个元素。

如果指定的元素存在于堆栈中,该方法将返回 True。例如,

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        Stack<string> days = new Stack<string>();
        days.Push("Monday");
        days.Push("Wednesday");

// check if the stack contains "Wednesday" Console.WriteLine(days.Contains("Wednesday"));
} }

输出

True
如何从堆栈中移除所有元素?

C# 提供了 Clear() 方法,我们可以使用它从堆栈中移除所有元素。

让我们看下面的示例:

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        Stack<int> numbers = new Stack<int>();
        numbers.Push(100);
        numbers.Push(202);

        Console.WriteLine("Total number of elements in original stack: " + numbers.Count);

// removing all elements from stack using Clear() numbers.Clear();
// total number of element inside stack after using CLear() Console.WriteLine("Total number of elements in updated stack: " + numbers.Count); } }

输出

Total number of elements in original stack: 2
Total number of elements in updated stack: 0
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战