栈的基本操作(顺序表)

菜鸟制作,不要笑话......

这是栈的基本操作,直接上代码:

head.h

#define OK 1
#define ERROR 0
#define STACKINITSIZE 100
#define STACKADD 10
typedef int Status;
typedef int SElemType;
typedef struct
{
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;
void show();
Status InitStack(SqStack &S);
Status DestroyStack(SqStack &S);
Status ClearStack(SqStack &S);
Status StackEmpty(SqStack &S);
int StackLength(SqStack S);
Status GetTop(SqStack &S,SElemType &e);
Status Push(SqStack &S,SElemType e);
Status Pop(SqStack &S,SElemType &e);
Status StackTraverse(SqStack &S);

function.cpp

#include"head.h"
#include<iostream>
using namespace std;
void show()
{
	cout<<"1.初始化栈!"<<endl;
	cout<<"2.销毁栈!"<<endl;
	cout<<"3.清空栈!"<<endl;
	cout<<"4.栈是否为空!"<<endl;
	cout<<"5.栈长!"<<endl;
	cout<<"6.取栈顶!"<<endl;
	cout<<"7.压栈!"<<endl;
	cout<<"8.出栈!"<<endl;
	cout<<"9.遍历栈!"<<endl;
}
Status InitStack(SqStack &S)
{
	S.base=(SElemType*)malloc(STACKINITSIZE*sizeof(SElemType));
	S.top=S.base;
	S.stacksize=STACKINITSIZE;
	return OK;
}
Status DestroyStack(SqStack &S)
{
	if(S.base==NULL)
		return ERROR;
	free(S.base);
	S.base=S.top=NULL;
	S.stacksize=0;
	return OK;
}
Status ClearStack(SqStack &S)
{
	if(S.base==NULL)
		return ERROR;
	S.top=S.base;
	S.stacksize=0;
	return OK;
}
Status StackEmpty(SqStack &S)
{
	if(S.base==S.top)
		return OK;
	else
		return ERROR;
}
int StackLength(SqStack S)
{
	if(S.base==NULL)
		return ERROR;
	return S.top-S.base;
}
Status GetTop(SqStack &S,SElemType &e)
{
	if(S.base==NULL)
		return ERROR;
	if(S.top==S.base)
		return ERROR;
	else
		e=*(S.top-1);
	return OK;
}
Status Push(SqStack &S,SElemType e)
{
	if(S.top-S.base>= STACKINITSIZE)
	{
		S.base=(SElemType*)realloc(S.base,(STACKINITSIZE+ STACKADD)*sizeof(SElemType));
		S.top=S.base+STACKINITSIZE;
		S.stacksize+=STACKADD;
	}
	*S.top++=e;
	return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
	if(S.top==S.base)
		return ERROR;
	e=*--S.top;
	return OK;
}
Status StackTraverse(SqStack &S)
{
	if(S.base==NULL)
		return ERROR;
	if(S.base==S.top)
		return ERROR;
	SElemType *p=S.base;
	for(;p<S.top;p++)
		cout<<*p<<" ";
	cout<<endl;
	return OK;
}

main.cpp

#include"head.h"
#include<iostream>
using namespace std;
void main()
{
	SqStack S;
	int n;
	SElemType e;
	while(1)
	{
		show();
		cin>>n;
		switch(n)
		{
		case 1:{
				if(!InitStack(S))
					cout<<"初始化失败!"<<endl;
				else
					cout<<"初始化成功!"<<endl;
				break;
			   }
		case 2:{
				if(!DestroyStack(S))
					cout<<"销毁失败!"<<endl;
				else
					cout<<"销毁成功!"<<endl;
				break;
			   }
		case 3:{
			   if(!ClearStack(S))
					cout<<"清空失败!"<<endl;
				else
					cout<<"清空成功!"<<endl;
				break;
			   }
		case 4:
			{
				if(S.base==NULL)
					cout<<"栈不存在!"<<endl;
				if(StackEmpty(S))
					cout<<"栈为空!"<<endl;
				else
					cout<<"栈不为空!"<<endl;
				break;
			}
		case 5:{
			   if(!StackLength(S))
				   cout<<"栈不存在!"<<endl;
			   else
				   cout<<"栈长为:"<< StackLength(S)<<endl;
			   break;
			   }
		case 6:
			{
			if(!GetTop(S,e))
				cout<<"获取失败!"<<endl;
			else
				cout<<"栈顶为:"<<e<<endl;
			break;
			}
		case 7:{
				cout<<"请输入要压入的数据:"<<endl;
				cin>>e;
				if(!Push(S,e))
					cout<<"压入失败!"<<endl;
				else
					cout<<"压入成功!"<<endl;
				break;
			   }
		case 8:{
			   if(!Pop(S,e))
				   cout<<"出栈失败!"<<endl;
			   else
				   cout<<"出栈成功!出栈的数据为:"<<e<<endl;
			   break;
			   }
		case 9:
			{
			if(!StackTraverse(S))
				cout<<"遍历失败!"<<endl;
			else
				cout<<"遍历成功!"<<endl;
			break;
			}
		default:{cout<<"输入有误!请重新输入:"<<endl;break;}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值