C# 从对象(堆栈)中放置和删除元素
初始化2个堆栈对象:C# 从对象(堆栈)中放置和删除元素,c#,C#,初始化2个堆栈对象: Stack s1 = new Stack(), s2 = new Stack(); s1=0由10个元素组成的数组,从顶部开始为空:0 const int Rows = 10; int[] Table = new int[Rows]; public void TableStack(int[] Table) { for (int i=0; i < Table.Length; i++) {
Stack s1 = new Stack(), s2 = new Stack();
s1=0由10个元素组成的数组,从顶部开始为空:0
const int Rows = 10;
int[] Table = new int[Rows];
public void TableStack(int[] Table)
{
for (int i=0; i < Table.Length; i++)
{
}
}
流行音乐:
我必须使用get&set吗?如果是,我如何用数组实现它?
我真的不知道该用什么来做这个。
任何提示都将不胜感激
程序使用以下驱动程序测试无法更改或修改的堆栈类:
public void ExecuteProgram()
{
Console.Title = "StackDemo";
Stack s1 = new Stack(), s2 = new Stack();
ShowStack(s1, "s1");
ShowStack(s2, "s2");
Console.WriteLine();
int getal = TryPop(s1);
ShowStack(s1, "s1");
TryPush(s2, 17);
ShowStack(s2, "s2");
TryPush(s2, -8);
ShowStack(s2, "s2");
TryPush(s2, 59);
ShowStack(s2, "s2");
Console.WriteLine();
for (int i = 1; i <= 3; i++)
{
TryPush(s1, 2 * i);
ShowStack(s1, "s1");
}
Console.WriteLine();
for (int i = 1; i <= 3; i++)
{
TryPush(s2, i * i);
ShowStack(s2, "s2");
}
Console.WriteLine();
for (int i = 1; i <= 6; i++)
{
getal = TryPop(s2);
//use number
ShowStack(s2, "s2");
}
}/*ExecuteProgram*/
您可以使用内置的堆栈,它将为您提供push、pop、peek等功能。堆栈对象上有push和pop方法。只需按所示调用s1.Push和s1.Pop即可。提示:您需要在堆栈类中维护内部数组中最顶层元素的索引
我不知道你所说的get/set是什么意思,但如果你实际上指的是属性,那么这里就不需要它了。为什么不在列表周围使用一个小包装器呢?我不想为此使用内置堆栈类。谢谢。@Chris:如果你的家庭作业说你不能使用内置类,那么你需要在你的问题中说明。是的。前几天我做了一些类似的东西,这让我很困惑,我是否应该使用它们。
int number = s1.Pop(); // s1 = 5 0 0 0 0 0 0 0 0 0 0 (top:1) 9 got removed
public void ExecuteProgram()
{
Console.Title = "StackDemo";
Stack s1 = new Stack(), s2 = new Stack();
ShowStack(s1, "s1");
ShowStack(s2, "s2");
Console.WriteLine();
int getal = TryPop(s1);
ShowStack(s1, "s1");
TryPush(s2, 17);
ShowStack(s2, "s2");
TryPush(s2, -8);
ShowStack(s2, "s2");
TryPush(s2, 59);
ShowStack(s2, "s2");
Console.WriteLine();
for (int i = 1; i <= 3; i++)
{
TryPush(s1, 2 * i);
ShowStack(s1, "s1");
}
Console.WriteLine();
for (int i = 1; i <= 3; i++)
{
TryPush(s2, i * i);
ShowStack(s2, "s2");
}
Console.WriteLine();
for (int i = 1; i <= 6; i++)
{
getal = TryPop(s2);
//use number
ShowStack(s2, "s2");
}
}/*ExecuteProgram*/