请求大神,帮忙解决一下这个习题!!

a6656411 2011-10-01 10:29:41
定一个Stack类,用来建立可存放整数的堆栈对象。该类的成员如下:
(1)aryData()数组:该数组用来存放堆栈内的元素.
(2)Stack(int Num)构造函数:用来建立可存放Num个元素的aryData()数组。
(3)public int Pop()方法:用来取出aryData数组中的一个元素,如果无元素,则返回0.
(4)public void Push(int n):可将n值放入到啊人员Data数组堆栈中,如果堆栈已满则显示"堆栈已满"的消息.
(5)public void PrintSatck():打印出堆栈内所有元素.

有点头绪,但就是写不出代码,新手一个,帮帮忙,谢谢,感激不尽!!
...全文
70 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
number1170196649 2011-10-01
  • 打赏
  • 举报
回复
namespace StackClass
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack(20);
for (int i = 0; i < 10;i++ )
{
stack.Push(i);
}
//stack.Pop();
stack.PrintSatck();
Console.ReadLine();
}
}

class Stack
{
private int item=0;
private int[] ArrayData;

public Stack(int num)
{
ArrayData=new int[num];
}

public int Pop()
{
if (item==0)
{
return 0;
}
return ArrayData[--item];
}

public void Push(int n)
{
if (item<ArrayData.Length)
{
ArrayData[item++] = n;
}
else
{
throw new StackOverflowException("堆栈已满");
}
}

public void StackClear()
{
item = 0;
}

public void PrintSatck()
{
for (int i = 0; i < item;i++ )
{
Console.WriteLine(ArrayData[i]);
}
}
}
}
number1170196649 2011-10-01
  • 打赏
  • 举报
回复
namespace StackClass
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack(20);
for (int i = 0; i < 10;i++ )
{
stack.Push(i);
}
//stack.Pop();
stack.PrintSatck();
Console.ReadLine();
}
}

class Stack
{
private int item=0;
private int[] ArrayData;

public Stack(int num)
{
ArrayData=new int[num];
}

public int Pop()
{
if (item==0)
{
return 0;
}
return ArrayData[--item];
}

public void Push(int n)
{
if (item<ArrayData.Length)
{
ArrayData[item++] = n;
}
else
{
throw new StackOverflowException("堆栈已满");
}
}

public void StackClear()
{
item = 0;
}

public void PrintSatck()
{
for (int i = 0; i < item;i++ )
{
Console.WriteLine(ArrayData[i]);
}
}
}
}
cnwin 2011-10-01
  • 打赏
  • 举报
回复
先自己写.哪儿遇到困难了再就具体提问
Alexander 2011-10-01
  • 打赏
  • 举报
回复
不是有System.Collections.Stack和System.Collections.Generic.Stack类么?干嘛还要自己写……
要保证数据都是整数的话用泛型System.Collections.Generic.Stack<int>不就行了。
言多必失 2011-10-01
  • 打赏
  • 举报
回复
这个真不难,你自己试试写,感觉不对把代码贴出来。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧