首頁  >  文章  >  後端開發  >  C# 堆疊

C# 堆疊

WBOY
WBOY原創
2024-09-03 15:30:28672瀏覽

以後進先出表示的物件集合稱為堆疊,它是一個根據程式的需要隨著向堆疊中添加元素而增加的集合,因此它是一個動態集合堆疊中可以儲存相同類型和不同類型的元素,將元素加入堆疊的過程稱為將元素壓入棧,將元素從堆疊中移除的過程稱為將元素從堆疊中彈出這個堆疊屬於Systems。集合命名空間。

文法:

C# Stack 的語法如下:

Stack stack_name = new Stack();

其中 stack_name 是 stack.l 的名稱

C# 中的堆疊函數

  • 每當我們需要按照後進先出的順序存取堆疊中的元素時,我們就會建立一個名為 Stack 的物件集合。
  • 向 Stack 新增元素的過程稱為將元素壓入堆疊,從堆疊中移除元素的過程稱為從 Stack 彈出元素。
  • 堆疊是元素的動態集合,因為堆疊的大小隨著向堆疊中添加元素而增加。
  • 堆疊可以容納的元素數量稱為堆疊的容量。隨著堆疊的大小隨著向堆疊中添加元素而增加,堆疊的容量也會透過重新分配而增加。
  • 堆疊中允許存在重複元素。
  • 堆疊接受 Null 作為型別、引用的有效值。

C#中Stack有多個建構子。他們是:

  • Stack(): 堆疊類別的一個新實例被初始化,該實例為空,初始容量為預設值。
  • Stack(ICollection):初始化stack 類別的新實例,該實例由從指定為參數的集合中取出的元素組成,初始容量與取出的元素數量相同來自指定為參數的集合。
  • Stack(Int32): 堆疊類別的新實例被初始化,該實例為空,其初始容量可以是參數指定的初始容量,也可以是預設的初始容量。

C# 堆疊中的方法

C#中Stack有多種方法。他們是:

  • Clear():使用 Clear() 方法刪除堆疊中的物件。
  • Push(Object):使用 Push(Object) 方法將指定為參數的物件插入到堆疊頂部。
  • Contains(Object):Contains(Object) 方法用於確定堆疊中是否存在元素。
  • Peek():傳回堆疊頂端指定的對象,但不會使用 Peek() 方法刪除。
  • Pop():傳回堆疊頂部指定的對象,並使用 Pop() 方法將其刪除。

範例

以下是c#堆疊的範例:

範例#1

考慮下面的範例程式來示範 Push() 方法、Pop() 方法、Peek() 方法、Contains() 方法和 Clear() 方法:

代碼:

using System;
using System.Collections;
//a class called program is defined
class program
{
//main method is called
public static void Main()
{
//a new stack is created
Stack mystk = new Stack();
//Adding the elements to the newly created stack
mystk.Push("India");
mystk.Push("USA");
mystk.Push("Canada");
mystk.Push("Germany");
//displaying the elements of the stack using foreach loop
Console.Write("The elements in the Stack are : ");
foreach(varele in mystk)
{
Console.WriteLine(ele);
}
//using contains() method to check if an element is present in the stack or not
Console.WriteLine(mystk.Contains("Germany"));
// The count of the elements in the stack is displayed
Console.Write("The count of elements in the Stack are : ");
Console.WriteLine(mystk.Count);
// displaying the top most element of the stack using Peek() method
Console.WriteLine("The topmost element in the stack is  : " + mystk.Peek());
//Using pop() method to remove the top element in the stack
Console.WriteLine("the element of the stack that is going to be removed" + " is: {0}",mystk.Pop());
Console.Write("The elements in the Stack after using pop() method are : ");
foreach(var el in mystk)
{
Console.WriteLine(el);
}
Console.Write("The count of elements in the Stack after using pop() method is : ");
Console.WriteLine(mystk.Count);
//using Clear() method to remove all the elements in the stack
mystk.Clear();
Console.Write("The count of elements in the Stack after using Clear() method is : ");
Console.WriteLine(mystk.Count);
}
}

輸出:

C# 堆疊

在上面的程式中,定義了一個名為program的類別。然後呼叫main方法。然後創建一個新的堆疊。然後使用 Push() 方法將元素新增到新建立的堆疊中。然後使用 foreach 循環顯示新建立的堆疊的元素。然後 contains() 方法用來檢查堆疊中是否存在元素。然後使用 count() 方法顯示堆疊中元素的數量。然後使用 Peek() 方法顯示堆疊最頂層的元素。然後使用 Pop() 方法刪除堆疊最頂層的元素。然後使用 Pop() 方法後再次顯示元素的計數和堆疊的元素。然後使用Clear()方法刪除堆疊中的所有元素。使用 Clear() 方法後,再次顯示元素的計數和堆疊的元素。程式的輸出如上面的快照所示。

  • Clone(): A shallow copy of the stack is created using the Clone() method.
  • Equals(Object): The Equals(Object) method is used to determine if the object specified as the parameter is equal to the current object.
  • Synchronized(Stack): A synchronized wrapper for the stack is returned using the Synchronized(Stack) method.
  • CopyTo(Array,Int32): The stack is copied into an array which is one dimensional with the index of the array specified as a parameter.
  • ToArray(): The stack is copied to a new array using ToArray() method.
  • GetType(): The type of the current instance is obtained using GetType() method.
  • ToString(): A string representing the current object is returned using ToString() method.
  • GetEnumerator(): An IEnumerator for the stack is returned using GetEnumerator() method.
  • GetHashCode(): The GetHashCode() method is the hash function by default.
  • MemberwiseClone(): A shallow copy of the current object is created using MemberwiseClone() method.

Example #2

Consider the example program below to demonstrate Clone() method, Equals() method, Synchronized() method, CopyTo() method, ToArray() method, GetType() method and GetEnumerator() method:

Code:

using System;
using System.Collections;
//a class called program is defined
class program
{
// Main Method is called
public static void Main(string[] args)
{
// creating a new stack
Stack mystk = new Stack();
mystk.Push("India");
mystk.Push("USA");
mystk.Push("Canada");
mystk.Push("Germany");
Console.Write("The elements in the Stack are : ");
foreach(varele in mystk)
{
Console.WriteLine(ele);
}
// a clone of the newly created stack is created
Stack mystk1 = (Stack)mystk.Clone();
// the top most element of the clone of the newly created stack is removed using pop() method
mystk1.Pop();
Console.Write("The elements in the clone of the Stack after using pop() method are : ");
//the elements of the clone of the newly created stack is displayed
foreach(Object ob in mystk1)
Console.WriteLine(ob);
//checking if the elements of the clone of the newly created stack and the newly created stack are equal or not
Console.Write("The elements in the clone of the Stack and the stack are equal or not : ");
Console.WriteLine(mystk.Equals(mystk1));
//Checking if the clone of the newly created stack and the newly created stack is synchronised or not
Console.WriteLine("The Clone of the newly created stack is {0}.", mystk1.IsSynchronized ? "Synchronized" : "Not Synchronized");
Console.WriteLine("The newly created stack is {0}.", mystk.IsSynchronized ? "Synchronized" : "Not Synchronized");
//a new array of strings is created and the newly created stack is assigned to this array
string[] arra = new string[mystk.Count];
// The elements of the newly created stack is copied to the array
mystk.CopyTo(arra, 0);
// the elements of the array are displayed
Console.Write("The elements of the array copied from the newly created stack are : ");
foreach(string st in arra)
{
Console.WriteLine(st);
}
//converting the elements of the newly created stack to array using toarray() method
Object[] ar1 = mystk.ToArray();
Console.Write("The elements of the array copied from the newly created stack by using ToArray() method are :");
//the elements of the array are displayed
foreach(Object st1 in ar1)
{
Console.WriteLine(st1);
}
Console.WriteLine("The type of newly created stack before using "+
"ToStringMethod is: "+mystk.GetType());
Console.WriteLine("The type of newly created stack after using "+
"ToString Method is: "+mystk.ToString().GetType());
Console.Write("The elements of the newly created stack after using GetEnumerator() method are : ");
//Getenumerator() method is used to obtain the enumerator of thestack
IEnumeratorenume = mystk.GetEnumerator();
while (enume.MoveNext())
{
Console.WriteLine(enume.Current);
}
}
}

Output:

C# 堆疊

In the above program, a class called program is defined. Then the main method is called. Then a new stack is created. Then the clone of the newly created stack is created by using the clone() method. Then the topmost element of the clone of the newly created stack is removed using the pop() method. Then the elements of the clone of the newly created method are displayed. Then Equals() method is used to check if the newly created stack and the clone of the newly created stack are equal or not. Then the synchronized() method is used to check if the newly created stack and the clone of the newly created stack are synchronized or not. Then Copyto() method is used to copy the newly created stack to an array and the elements of the array are displayed. Then ToArray() method is used to copy the newly created stack to another array and then the elements of the array are displayed. Then GetType() method is used to obtain the type of the newly created stack. Then ToString() method is used to convert the type stack to string. Then GetEnumerator() method is used to obtain the IEnumerator of the stack. The output of the program is shown in the snapshot above.

以上是C# 堆疊的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的元數據下一篇:C# 中的元數據