#include<stdio.h>
#include<iostream>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACKINCREMENT 10
#define STACK_INIT_SIZE 100
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
//初始化为空栈
int InitStack(SqStack &s) {
if(s.base){
cout<<"该栈已存在!"<<endl;
}else{
s.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s.base){
cout<<"创建失败!"<<endl;
cout<<endl<<"请输入你的选择:";
return ERROR;
}
s.top=s.base;
s.stacksize = STACK_INIT_SIZE;
*(s.base) = 0;
cout<<"初始化成功!"<<endl;
}
return OK;
}
//销毁栈
int DestroyStack(SqStack &s) {
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
return FALSE;
} else {
free(s.base);
s.stacksize = 0;
s.base=NULL;
cout<<"销毁成功!"<<endl;
return OK;
}
}
//将栈置空
int ClearStack(SqStack &s) {
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
return FALSE;
} else {
while(s.top != s.base) {
*(s.top) = 0;
s.top--;
}
*(s.base) = 0;
cout<<"清空成功!"<<endl;
return OK;
}
}
//判断栈是否为空栈
int StackEmpty(SqStack s) {
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
return FALSE;
} else {
if(s.base == s.top) {
cout<<"该栈是空栈!"<<endl;
return ERROR;
} else {
cout<<"该栈不是空栈!"<<endl;
return OK;
}
}
}
//返回栈的长度
int StackLength(SqStack s) {
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
return FALSE;
} else {
int len = 0;
while(s.top != s.base) {
len++;
s.top--;
}
cout<<"栈的长度为:"<<len<<endl;
return OK;
}
}
//求栈顶元素
int GetTop(SqStack s) {
if(*(s.base) == 0){
cout<<"该栈内没有元素!"<<endl;
}
else{
cout<<"该栈顶元素为:"<<*(s.top-1)<<endl;
}
return OK;
}
//插入元素,并使其成为栈顶元素
int Push(SqStack &s,SElemType e) {
if(s.top - s.base >= s.stacksize) {
s.base = (SElemType *)realloc(s.base,(s.stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!s.base) return ERROR;
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*(s.top++) = e;
return OK;
}
//删除栈顶元素,并返回其值
int Pop(SqStack &s, SElemType &e) {
if(s.top == s.base){
cout<<"该栈内没有元素,删除失败!"<<endl;
return ERROR;
}else{
e = *(--s.top);
cout<<"删除成功!"<<endl;
cout<<"已删除的栈顶元素为:"<<e<<endl;
return OK;
}
}
//输出栈内元素
int StackTraverse(SqStack s) {
while(s.top != s.base) {
cout<<*(s.base)<<" ";
s.base++;
}
cout<<endl;
return OK;
}
//创建并输入栈元素
int PutIn(SqStack &s) {
s.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!s.base) {
cout<<"创建失败!"<<endl;
cout<<endl<<"请输入你的选择:";
return ERROR;
}
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
int nums;
while((cin>>nums)!=0) {
if(nums<0) break;
Push(s,nums);
}
return OK;
}
int main() {
cout<<"可执行操作有:" <<endl;
cout<<"************************************************************"<<endl;
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;
cout<<"************* 10.创建并输入栈元素 ***********"<<endl;
cout<<"************* 11.退出 ***********"<<endl;
cout<<"************************************************************"<<endl;
cout<<endl<<"请输入你的选择:";
int num,n;
SElemType e;
SqStack s;
s.base = NULL;
while(scanf("%d",&num)!= EOF) {
switch(num) {
case 1:
InitStack(s);
cout<<endl<<"请输入你的选择:";
break;
case 2:
DestroyStack(s);
cout<<endl<<"请输入你的选择:";
break;
case 3:
ClearStack(s);
cout<<endl<<"请输入你的选择:";
break;
case 4:
StackEmpty(s,e);
cout<<endl<<"请输入你的选择:";
break;
case 5:
StackLength(s);
cout<<endl<<"请输入你的选择:";
break;
case 6:
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
} else {
GetTop(s);
cout<<endl<<"请输入你的选择:";
break;
}
case 7:
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
} else {
cout<<"请输入插入元素:";
cin>>n;
Push(s,n);
cout<<"插入成功!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
}
case 8:
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
} else {
Pop(s,e);
cout<<endl<<"请输入你的选择:";
break;
}
case 9:
if(s.base == NULL) {
cout<<"顺序栈不存在!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
}
else if(*(s.base) == 0){
cout<<"该栈内没有元素!"<<endl;
cout<<endl<<"请输入你的选择:";
break;
}
else {
cout<<"该栈内元素为:";
StackTraverse(s);
cout<<endl<<"请输入你的选择:";
break;
}
case 10:
cout<<"请输入栈元素并以一个负数结尾!"<<endl;
PutIn(s);
cout<<endl<<"请输入你的选择:";
break;
case 11:
return 0;
default:
cout<<"该数据无效,请重新输入:";
break;
}
}
return 0;
}
顺序栈的几种基本操作
最新推荐文章于 2022-04-05 20:00:00 发布