當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Stack用法及代碼示例

堆棧代表一個last-in,先出對象集合。當您需要 last-in、first-out 訪問項目時使用它。當您在列表中添加項目時,稱為推送該項目;當您刪除該項目時,稱為彈出該項目。此類位於 System.Collections 命名空間下。

Stack類的特點:

  • 棧的容量是指棧可以容納的元素的數量。當元素添加到堆棧時,容量會根據需要通過重新分配自動增加。
  • 如果 Count 小於堆棧的容量,則 Push 是O(1)操作。如果需要增加容量來容納新元素,Push 就變成了在)操作,其中n是計數。流行音樂是一個O(1)操作。
  • Stack 接受 null 作為有效值並允許重複元素。

Constructors

構造函數 說明
Stack() 初始化 Stack 類的一個新實例,該實例為空且具有默認初始容量。
Stack(ICollection) 初始化 Stack 類的新實例,該實例包含從指定集合複製的元素,並具有與複製的元素數相同的初始容量。
堆棧(Int32) 初始化 Stack 類的新實例,該實例為空且具有指定初始容量或默認初始容量(以較大者為準)。

例子:


// C# code to create a Stack 
using System; 
using System.Collections; 
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a Stack 
        Stack myStack = new Stack(); 
  
        // Inserting the elements into the Stack 
        myStack.Push("1st Element"); 
        myStack.Push("2nd Element"); 
        myStack.Push("3rd Element"); 
        myStack.Push("4th Element"); 
        myStack.Push("5th Element"); 
        myStack.Push("6th Element"); 
  
        // Displaying the count of elements 
        // contained in the Stack 
        Console.Write("Total number of elements in the Stack are : "); 
  
        Console.WriteLine(myStack.Count); 
  
        // Displaying the top element of Stack 
        // without removing it from the Stack 
        Console.WriteLine("Element at the top is : " + myStack.Peek()); 
  
        // Displaying the top element of Stack 
        // without removing it from the Stack 
        Console.WriteLine("Element at the top is : " + myStack.Peek()); 
  
        // Displaying the count of elements 
        // contained in the Stack 
        Console.Write("Total number of elements in the Stack are : "); 
  
        Console.WriteLine(myStack.Count); 
    } 
} 

輸出:

Total number of elements in the Stack are : 6
Element at the top is : 6th Element
Element at the top is : 6th Element
Total number of elements in the Stack are : 6

Properties

屬性 說明
Stack.Count 獲取 Stack 中包含的元素數量。
Stack.IsSynchronized 獲取一個值,該值指示對 Stack 的訪問是否同步(線程安全)。
SyncRoot 獲取可用於同步對 Stack 的訪問的對象。

例子:


// C# code to Get the number of 
// elements contained in the Stack 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a Stack 
        Stack myStack = new Stack(); 
  
        // Inserting the elements into the Stack 
        myStack.Push("Chandigarh"); 
        myStack.Push("Delhi"); 
        myStack.Push("Noida"); 
        myStack.Push("Himachal"); 
        myStack.Push("Punjab"); 
        myStack.Push("Jammu"); 
  
        // Displaying the count of elements 
        // contained in the Stack 
        Console.Write("Total number of elements in the Stack are : "); 
  
        Console.WriteLine(myStack.Count); 
    } 
} 

輸出:

Total number of elements in the Stack are : 6

Methods

方法 說明
Stack.Clear 從堆棧中刪除所有對象。
Stack.Clone() 創建堆棧的淺拷貝。
Stack.Contains() 判斷一個元素是否在棧中。
Stack.CopyTo() 從指定的數組索引開始,將 Stack 複製到現有的一維數組。
Stack.Equals() 確定指定對象是否等於當前對象。
Stack.GetEnumerator 返回堆棧的 IEnumerator。
GetHashCode() 用作默認的哈希函數。
GetType() 獲取當前實例的類型。
MemberwiseClone() 創建當前對象的淺拷貝。
Stack.Peek 返回堆棧頂部的對象而不刪除它。
Stack.Pop() 移除並返回 Stack 頂部的對象。
Stack.Push() 將一個對象插入到 Stack 的頂部。
Stack.Synchronized() 返回堆棧的同步(線程安全)包裝器。
Stack.ToArray() 將堆棧複製到新數組。
Stack.ToString() 返回表示當前對象的字符串。

例子:


// C# code to Remove all 
// objects from the Stack 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a Stack  
        Stack myStack = new Stack(); 
  
        // Inserting the elements into the Stack 
        myStack.Push("1st Element"); 
        myStack.Push("2nd Element"); 
        myStack.Push("3rd Element"); 
        myStack.Push("4th Element"); 
        myStack.Push("5th Element"); 
        myStack.Push("6th Element"); 
  
        // Displaying the count of elements 
        // contained in the Stack before 
        // removing all the elements 
        Console.Write("Total number of elements in the Stack are : "); 
  
        Console.WriteLine(myStack.Count); 
  
        // Removing all elements from Stack 
        myStack.Clear(); 
  
        // Displaying the count of elements 
        // contained in the Stack after 
        // removing all the elements 
        Console.Write("Total number of elements in the Stack are : "); 
  
        Console.WriteLine(myStack.Count); 
    } 
} 
輸出:
Total number of elements in the Stack are : 6
Total number of elements in the Stack are : 0

例子:


// C# code to Check if a Stack 
// contains an element 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating a Stack of strings 
        Stack myStack = new Stack(); 
  
        // Inserting the elements into the Stack 
        myStack.Push("Geeks"); 
        myStack.Push("Geeks Classes"); 
        myStack.Push("Noida"); 
        myStack.Push("Data Structures"); 
        myStack.Push("GeeksforGeeks"); 
  
        // Checking whether the element is 
        // present in the Stack or not 
        // The function returns True if the 
        // element is present in the Stack, else 
        // returns False 
        Console.WriteLine(myStack.Contains("GeeksforGeeks")); 
    } 
} 
輸出:
True

參考:



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 C# | Stack Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。