interface IList<T>
{
bool IsEmpty();
int GetLength();
void Push(T elem);
void Pop();
T peek();
void Clear();
}
/// <summary>
/// 链栈
/// </summary>
/// <typeparam name="T"></typeparam>
class LinkStack<T> : IList<T>
{
private Node<T> head; //指向栈顶的头指针
private int count; //元素个数
public LinkStack()
{
//初始化为空栈,栈顶指针为空
head = null;
count = 0;
}
public void Clear()
{
head = null;
count = 0;
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return head == null;
}
public T peek()
{
if (head == null)
{
Console.WriteLine("空链栈无法取数据");
return default(T);
}
return head.Data;
}
public void Pop()
{
Node<T> temp = head;
if (head == null)
{
Console.WriteLine("空链栈无法删除");
return;
}
head = temp.Next;
count--;
}
public void Push(T elem)
{
Node<T> newNode = new Node<T>(elem);
head = newNode;
count++;
}
}