using System;
public class Stack
{
private Node first = null;
public bool Empty {
get {
return (first == null);
}
}
public object Pop() {
if (first == null)
throw new Exception("Can't Pop from an empty Stack.");
else {
object temp = first.Value;
first = first.Next;
return temp;
}
}
public void Push(object o) {
first = new Node(o, first);
}
private 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;
}
}
}
public class Stack
{
private Node first = null;
public bool Empty {
get {
return (first == null);
}
}
public object Pop() {
if (first == null)
throw new Exception("Can't Pop from an empty Stack.");
else {
object temp = first.Value;
first = first.Next;
return temp;
}
}
public void Push(object o) {
first = new Node(o, first);
}
private 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;
}
}
}
public Node(object value): this(value, null) {}
public Node(object value, Node next) {
Next = next;
Value = value;
}三种重载,实际上真正的实体是第三种,前面两种实现了参数数目的可变。
1.不指定任何参数:
Node n = new Node();
相当于:
Node n = new Node(null, null);2.指定 value 参数:
Node n = new Node("MyNode");
相当于:
Node n = new Node("MyNode", null);3.两者都指定:
Node n = new Node("MyNode", nextNode);