Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 890 Bytes

2.1栈的顺序存储——顺序栈.md

File metadata and controls

50 lines (43 loc) · 890 Bytes
//数组中下标为0的元素就是栈底
//top指向栈顶元素的下一个位置

#define MAXSIZE 100
typedef int datatype;
typedef struct{
	datatype a[MAXSIZE];
	int top;
}sequence_stack;

//顺序栈的初始化——置空栈
void init(sequence_stack *st){
	st->top=0;
}

//判断顺序栈是否为空
int isEmpty(sequence_stack st){
	return (st.top==0 ? 1:0);
}

//取得栈顶结点值
datatype getTopValue(sequence_stack st){
	if(isEmpty(st)==1){
		printf("顺序栈是空的!");
		exit(1);
	}
	return st.a[st.top-1];
}

//在栈顶插入一个元素——进栈
void push(sequence_stack *st,datatype x){
	if(st->top==MAXSIZE){
		printf("顺序栈是满的!");
		exit(1);
	}
	st->a[st->top]=x;
	st->top++;
}

//在栈顶删除一个元素——出栈————
void pop(sequence_stack *st){
	if(st.top==0){
		printf("顺序栈是空的!");
		exit(1);
	}
	st->top--;
}