class Stack
{
int maxsize; //順序棧的容量
object[] data; //數組,用於存儲棧中的數據
int top; //指示棧頂
public object this[int index]
{
get { return data[index]; }
set { data[index] = value; }
}
//使用構造器初始化棧
public Stack(int size)
{
data = new object[size];
maxsize = size;
top = -1;
}
//求棧的長度(棧中的元素個數)
public double StackLength()
{
return top + 1;
}
//判斷順序棧是否爲空
public bool IsEmpty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}
//判斷順序棧是否爲滿
public bool IsFull()
{
if (top == maxsize - 1)
{
return true;
}
else
{
return false;
}
}
//入棧操作
public void Push(object e)
{
if (IsFull())
{
return;
}
data[++top] = e;
}
//出棧操作,並返回出棧的元素
public object Pop()
{
object temp = null;
if (IsEmpty())
{
return temp;
}
temp = data[top];
top--;
return temp;
}
}
c#中棧的定義、操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.