顺序栈的实现


#include <iostream>
using namespace std;

#define MaxSize 20
#define Error 0
#define Ok 1
typedef int Elemtype;
typedef int Status;

struct SqStack{
	Elemtype data[MaxSize];
	int top;
}; 
Status InitStack(SqStack* s){
	s->top=-1;
	return Ok; 
}
Status Clear(SqStack* s){
	s->top=-1;
	return Ok;
}
bool IsEmpty(SqStack s){
	if(s.top==-1) return true;
	else return false;
}
int Length(SqStack s){
	return s.top+1;
}
Status GetTop(SqStack s,Elemtype* e){
	if(s.top==-1) return Error;
	*e=s.data[s.top]; 
}
Status Push(SqStack* s,Elemtype e){
	if(s->top==MaxSize-1) return Error;
	++s->top;
	s->data[s->top]=e;
	return Ok;
}
Status Pop(SqStack* s,Elemtype* e){
	if(s->top<0) return Error;
	*e=s->data[s->top];
	--s->top;
	return Ok;
}
Status Traverse(SqStack s){
	for(int i=0;i<=s.top;++i)
		cout<<s.data[i]<<' ';
	cout<<endl;
	return Ok;
}

int main(){
	SqStack s;
	InitStack(&s);
	cout<<Length(s)<<IsEmpty(s)<<endl;
	Elemtype ein=77;
	Elemtype eout;
	Push(&s,ein);Push(&s,ein);
	Traverse(s);
	cout<<Length(s)<<IsEmpty(s)<<endl;
	GetTop(s,&eout);
	cout<<eout<<Length(s);
	Pop(&s,&eout);
	cout<<eout<<Length(s);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值