📜  C#|从集合创建堆栈

📅  最后修改于: 2021-05-29 18:03:33             🧑  作者: Mango

堆栈表示对象的后进先出集合。当您需要对项目进行后进先出的访问时使用。在列表中添加项目时,称为推送项目,而在删除项目时,则称为弹出项目。创建堆栈意味着将项目添加到堆栈中。 Stack .Push(Object)方法用于在堆栈顶部插入一个对象。

特性:

  • 堆栈的容量是堆栈可以容纳的元素数量。将元素添加到堆栈后,容量会根据需要通过重新分配自动增加。
  • 如果Count小于堆栈的容量,则Push是O(1)运算。如果需要增加容量以容纳新元素,则Push变为O(n)操作,其中n为Count。 Pop是O(1)运算。
  • 堆栈接受null作为有效值,并允许重复的元素。

句法:

public virtual void Push (object obj);

范围:

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to Create a Stack
// from a collection
using System;
using System.Collections.Generic;
  
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack of strings
        Stack myStack1 = new Stack();
  
        // Inserting the elements into the Stack
        myStack1.Push("GeeksforGeeks");
        myStack1.Push("is");
        myStack1.Push("the");
        myStack1.Push("best");
        myStack1.Push("website");
  
        // Displaying the count of elements
        // contained in the myStack1
        Console.Write("Total number of elements in the Stack 1 are : ");
  
        Console.WriteLine(myStack1.Count);
  
        // Displaying the elements in Stack myStack1
        foreach(string str in myStack1)
        {
            Console.WriteLine(str);
        }
  
        // Creating a Stack from a collection
        Stack myStack2 = new Stack(myStack1.ToArray());
  
        // Displaying the count of elements
        // contained in the myStack2
        Console.Write("Total number of elements in the Stack 2 are : ");
  
        Console.WriteLine(myStack2.Count);
  
        // Displaying the elements in Stack myStack2
        foreach(string str in myStack2)
        {
            Console.WriteLine(str);
        }
    }
}
输出:
Total number of elements in the Stack 1 are : 5
website
best
the
is
GeeksforGeeks
Total number of elements in the Stack 2 are : 5
GeeksforGeeks
is
the
best
website

范例2:

// C# code to Create a Stack
// from a collection
using System;
using System.Collections.Generic;
  
class GFG {
    // Driver code
    public static void Main()
    {
        // Creating a Stack of Integers
        Stack myStack1 = new Stack();
  
        // Inserting the elements into the Stack
        myStack1.Push(5);
        myStack1.Push(10);
        myStack1.Push(15);
        myStack1.Push(20);
        myStack1.Push(25);
  
        // Displaying the count of elements
        // contained in the myStack1
        Console.Write("Total number of elements in the Stack 1 are : ");
  
        Console.WriteLine(myStack1.Count);
  
        // Displaying the elements in Stack myStack1
        foreach(int i in myStack1)
        {
            Console.WriteLine(i);
        }
  
        // Creating a Stack from a collection
        Stack myStack2 = new Stack(myStack1.ToArray());
  
        // Displaying the count of elements
        // contained in the myStack2
        Console.Write("Total number of elements in the Stack 2 are : ");
  
        Console.WriteLine(myStack2.Count);
  
        // Displaying the elements in Stack myStack2
        foreach(int i in myStack2)
        {
            Console.WriteLine(i);
        }
    }
}
输出:
Total number of elements in the Stack 1 are : 5
25
20
15
10
5
Total number of elements in the Stack 2 are : 5
5
10
15
20
25

参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.stack.push?view=netframework-4.7.2