C# 顺序栈

    public interface IStack<T>
{
int GetLength();
bool IsEmpty();
bool IsFull();
void Clear();
void Push(T item);
T Pop();
T GetTop();
}


public class Stack<T> : IStack<T>
{
private int size;
private T[] data;
private int top;

public T this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}

public int GetSize
{
get
{
return size;
}
set
{
size = value;
}
}

public int Top
{
get
{
return top;
}
}

public Stack(int size)
{
data = new T[size];
top = -1;
this.size = size;
}

public int GetLength()
{
return top + 1;
}

public bool IsEmpty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}

public bool IsFull()
{
if (top == size -1)
{
return true;
}
else
{
return false;
}
}

public void Clear()
{
top = -1;
}

public void Push(T item)
{
if (IsFull())
{
throw new Exception("Stack is full, can't push any item");
}
else
{
data[++top] = item;
}
}

public T Pop()
{
T temp = data[top];
if (IsEmpty())
{
throw new Exception("Stack is empty, can't pop out anything.");
}
top--;
return temp;
}

public T GetTop()
{
if (IsEmpty())
{
throw new Exception("Stack is empty, can't get top item.");
}
return data[top];
}
}
原文地址:https://www.cnblogs.com/Ligeance/p/2391659.html