3-3 顺序栈的操作实现(3)

顺序栈的表示

#define MAXSIZE 100;
typedef struct SqStack {
	SElemType *top; //栈顶指针
	SElemType *base; //栈底指针
	int stacksize; //栈可用最大容量
}SqStack;

【算法】栈的初始化:

Status InitStack(SqStack &S) {
	S.base = (*SElemType)malloc(MAXSIZE*sizeof(ElemType)); //在内存中开辟空间
	//S.base = new SElemType[MAXSIZE]; 
	if(!S.base)
		return ERROR;
	S.top = S.base;
	S.stacksize = MAXSIZE;
	return OK;
}

3-3 顺序栈的操作(4)

【算法】顺序栈判断是否为空

Status StackEmpty(SqStack S) { //若为空返回true
	if(S.top == S.base)
		return TRUE;
	else
		return FALSE;
}

【算法】求顺序栈长度

int StackLnegth(SqStack S) {
	return S.top-S.base;
}

【算法】清空顺序栈

Status ClearStack(SqStack &S) {
	if(S.base)
		S.top = S.base;
	return OK;
}

【算法】删除顺序栈

Status DestroyStack(SqStack &S) {
	if(S.base) {
		free(S.base);
		S.stacksize = 0;
		S.top = S.base = NULL;
	}
	return OK;
}

【算法】入栈

Status Push(SqStack &S, SElemType e) {
	if(S.top - S.base == S.stacksize)
		return ERROR;
	*S.top++ = e;
	return OK;
}