关于new对象的问题
using System;using System.Collections.Generic;
using System.Text;
namespace DataStructure
{
///
/// Class1 的摘要说明。
///
public class Stack//栈类
{
//private int count = 0;
//private Node first = null;//定义首结点
public bool Empty
{
get
{
return (first == null);
}
}
public int Count
{
get
{
return count;
}
}
public object Pop()//出栈
{
if (first == null)
{
throw new InvalidOperationException("不能从一个空栈出栈!");
}
else
{
object temp = first.Value;
first = first.Next;
count--;
return temp;
}
}
//这里跟方法签名里的参数类型不同也可以吗?
public void push(object o)//入栈
{
first = new Node(o, first);
count++;
}
public Stack()
{
System.Console.WriteLine("建立了一个空栈");
}
private int count = 0;
private Node first = null;//定义首结点
public void Print()
{
object temp = Pop();
//first = Pop();
System.Console.Write("{0} ",temp.ToString());
}
static void Main(string[] args)
{
int n;
System.Console.Write("请输入入栈的元素个数:");
n = int.Parse(System.Console.ReadLine());
Stack stk = new Stack();
//这个for循环不明白,一直创建对象,又没有数组将值放在一起,如何可以输出这么多对象的值呢?
for ( int i = 0; i < n; i++)
{
stk.push(i);
}
while (stk.first != null)
{
stk.Print();
}
Console.ReadLine();
}
}
class Node //结点类
{
public Node Next;
public object Value;
public Node(object value) : this(value, null) { }
public Node(object value, Node next)
{
Next = next;
Value = value;
}
}
}