數據結構-棧-順序存儲-基本運算

//棧
//順序存儲
//定義
const int maxsize=6;
typedef struct seqstack
{
	DataType data[maxsize];
	int top;
}SeqStk;

//1.初始化
int InitStack(SeqStk *stk)
{
	stk->top=0;
	return 1;
}

//2.判棧空
int EmptyStack(SeqStk *stk)
{
	if (stk->top==0)
		return 1;
	else 
		return 0;
}
//3.進棧
int Push(SeqStk *stk,DataType x)
{
	if (stk->top==maxsize-1)
	{
		error("stack full!");
		return 0;
	}
	else
	{
		stk->top++;
		stk->data[stk->top]=x;
		return 1;
	}
}

//4.出棧
int Pop(SeqStk *stk)
{
	if (EmptyStack(stk))
	{
		error("underflow!");
		return 0;
	}
	else
	{
		stk->top--;
		return 1;
	}
}

//5.取棧頂元素
DataType GetTop(SeqStk *stk)
{
	if (EmptyStack(stk))
		return NULLData;
	else
		return stk->data[stk->top];
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章