3个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stackTest
{
class StackTest
{
private int[] Stack;
private int Top= -1;//栈的当前位置
public StackTest(int num)
{
Stack= New int[num];//初始化栈大小
}
public int Length
{
get
{
return Top;
}
}
public void Push(int num)//入栈
{
if(Top<Stack.Length)
Stack[++Top]=num;
else
MessageBox.Show("Stack is Full");
}
public void Pop()//出栈
{
if(Top>-1)
Top--;
else
MessageBox.Show("Stack is Empty");
}
public void ShowStack()//显示当前栈内值
{
for(int i=0;i<=Top;i++)
Console.WriteLine(Stack[i].ToString());
}
}
class Program
{
static void Main(string[] args)
{
StackTest stack = new StackTest(100);//初始化栈
stack.Push(100);//入栈值
stack.Push(200);
stack.Push(300);
stack.Push(400);
ShowStack();//显示栈内值
}
}
}
方法给你了,你修改下就可以用了!
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stackTest
{
class StackTest
{
private int[] Stack;
private int Top= -1;//栈的当前位置
public StackTest(int num)
{
Stack= New int[num];//初始化栈大小
}
public int Length
{
get
{
return Top;
}
}
public void Push(int num)//入栈
{
if(Top<Stack.Length)
Stack[++Top]=num;
else
MessageBox.Show("Stack is Full");
}
public void Pop()//出栈
{
if(Top>-1)
Top--;
else
MessageBox.Show("Stack is Empty");
}
public void ShowStack()//显示当前栈内值
{
for(int i=0;i<=Top;i++)
Console.WriteLine(Stack[i].ToString());
}
}
class Program
{
static void Main(string[] args)
{
StackTest stack = new StackTest(100);//初始化栈
stack.Push(100);//入栈值
stack.Push(200);
stack.Push(300);
stack.Push(400);
ShowStack();//显示栈内值
}
}
}
方法给你了,你修改下就可以用了!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询