目录
C# 中的栈函数
C# 堆栈中的方法
示例#1
Example #2

C# 堆栈

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

以后进先出表示的对象集合称为堆栈,它是一个根据程序的需要随着向堆栈中添加元素而增加的集合,因此它是一个动态集合栈中可以存储相同类型和不同类型的元素,向栈中添加元素的过程称为将元素压入栈,从栈中移除元素的过程称为将元素从栈中弹出这个堆栈属于 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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。这里我们分别讨论C#序列化对象的介绍、步骤、工作原理和示例。

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 随机数生成器指南。在这里,我们讨论随机数生成器的工作原理、伪随机数和安全数的概念。

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图指南。在这里,我们讨论如何从 SQL 数据库或 Excel 文件加载和导出数据网格视图的示例。

C# 中的阶乘 C# 中的阶乘 Sep 03, 2024 pm 03:34 PM

C# 阶乘指南。这里我们讨论 C# 中阶乘的介绍以及不同的示例和代码实现。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在这里,我们讨论 C# 中模式的介绍和前 3 种类型,以及其示例和代码实现。

c#多线程和异步的区别 c#多线程和异步的区别 Apr 03, 2025 pm 02:57 PM

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

C# 中的质数 C# 中的质数 Sep 03, 2024 pm 03:35 PM

C# 素数指南。这里我们讨论c#中素数的介绍和示例以及代码实现。

See all articles