9.链栈类
namespace DSList
{
public class LinkStack<T> : IStack<T>
{
//Fields
private Node<T> top; //Top reference indicator
private int num; //Node quantity
//Properties
public Node<T> Top
{
get
{
return top;
}
set
{
top = value;
}
}
public int Num
{
get
{
return num;
}
set
{
num = value;
}
}
//Constructor
public LinkStack()
{
top = null;
num = 0;
}
//Base Methods
public int GetLength()
{
return Num;
}
public bool IsEmpty()
{
if (num == 0 && top==null)
{
return true;
}
else
{
return false;
}
}
public void Clear()
{
top = null;
num = 0;
}
public void Push(T item)
{
Node<T> newNode = new Node<T>(item);
if (top == null)
{
top = newNode;
}
else
{
newNode.Next = top;
top = newNode;
}
++num;
}
public T Pop()
{
if (IsEmpty())
{
Console.WriteLine("LinkStack is empty!");
return default(T);
}
Node<T> tmpNode = top;
top = top.Next;
--num;
return tmpNode.Data;
}
public T GetTop()
{
if (IsEmpty())
{
Console.WriteLine("LinkStack is empty!");
return default(T);
}
return top.Data;
}
public void Output()
{
Node<T> p = top;
while (p != null)
{
Console.Write(p.Data+" ");
p = p.Next;
}
Console.WriteLine();
}
}
}