求堆栈数据结构的C#实现

来源:百度知道 编辑:UC知道 时间:2024/06/23 18:33:03

public class Stack<ItemType>
{
private ItemType[] items;
private int top,size;
private bool IsFull;

public Stack(int size)
{
top = 0;
items = new ItemType[size];
this.size = size;
this.IsFull = false;
}

public void Push(ItemType data)
{
if (IsFull)
return;
items[0] = data;
top++;
if (top == size - 1)
this.IsFull = true;
}

public ItemType Pop()
{
if (top == 0)
throw (new Exception ("The stack is null"));
top--;
ItemType item = items[top];
this.IsFull = false;
return item;
}

}

使用方法:
try