#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;
#define StackSize 100
#define StackIncrement 10
typedef struct SqStack
{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(int *)malloc(StackSize *sizeof(int));
if(!S->base)
{
return 0;
}//存储分配失败
S->top=S->base;
S->stacksize=StackSize;
return 1;
}
void GetTop(SqStack S,int &e)
{
if(S.top==S.base)
{
cout<<"栈为空";
}
else
{
e=*(S.top-1);
cout<<"栈顶元素为"<<e;
}
}
int StackLength(SqStack S)
{
cout<<"栈的长度为"<<S.top-S.base;
return 0;
}
void ClearStack(SqStack *S)
{
S->top=S->base;
}
void DestroyStack(SqStack *S)
{
if(S->base)
{
free(S->base);
}
S->base=S->top=NULL;
cout<<"销毁成功";
}
void Show(SqStack S)
{
int *p=S.top;
while(p!=S.base)
{
cout<<*(--p)<<" ";
}
cout<<*p<<" ";
}
void Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(int *)realloc(S->base,(S->stacksize+StackIncrement)*sizeof(int));
if(!S->base)
{
exit(1);
}
S->top=S->base+S->stacksize;
S->stacksize=StackIncrement;
}
*S->top++=e;
}
void Pop(SqStack *S,int &e)
{
if(S->top==S->base)
{
cout<<"栈空";
}
else
{
e=*--S->top;
}
}
void StackEmpty(SqStack S)
{
if(S.base==S.top)
{
cout<<"栈为空";
}
else
{
cout<<"栈不空";
}
}
int main()
{
SqStack *S;
int i,e,n,j;
while(1)
{
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<<"请输入要执行的步骤:";
cin>>i;
if(i==1)
{
InitStack(S);
}
if(i==2)
{
cout<<"请输入要插入的元素个数:";
cin>>n;
for(j=0;j<n;j++)
{
cout<<"请输入要插入的元素:";
cin>>e;
Push(S,e);
}
}
if(i==3)
{
StackEmpty(*S);
}
if(i==4)
{
ClearStack(S);
}
if(i==5)
{
StackLength(*S);
}
if(i==6)
{
GetTop(*S,e);
}
if(i==7)
{
Pop(S,e);
}
if(i==8)
{
DestroyStack(S);
}
if(i==9)
{
Show(*S);
}
if(i==10)
{
break;
}
}
return 0;
}
编译可以通过。但是输入1初始化栈的时候就会停止运行。不知道为什么。。求解答
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;
#define StackSize 100
#define StackIncrement 10
typedef struct SqStack
{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(int *)malloc(StackSize *sizeof(int));
if(!S->base)
{
return 0;
}//存储分配失败
S->top=S->base;
S->stacksize=StackSize;
return 1;
}
void GetTop(SqStack S,int &e)
{
if(S.top==S.base)
{
cout<<"栈为空";
}
else
{
e=*(S.top-1);
cout<<"栈顶元素为"<<e;
}
}
int StackLength(SqStack S)
{
cout<<"栈的长度为"<<S.top-S.base;
return 0;
}
void ClearStack(SqStack *S)
{
S->top=S->base;
}
void DestroyStack(SqStack *S)
{
if(S->base)
{
free(S->base);
}
S->base=S->top=NULL;
cout<<"销毁成功";
}
void Show(SqStack S)
{
int *p=S.top;
while(p!=S.base)
{
cout<<*(--p)<<" ";
}
cout<<*p<<" ";
}
void Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(int *)realloc(S->base,(S->stacksize+StackIncrement)*sizeof(int));
if(!S->base)
{
exit(1);
}
S->top=S->base+S->stacksize;
S->stacksize=StackIncrement;
}
*S->top++=e;
}
void Pop(SqStack *S,int &e)
{
if(S->top==S->base)
{
cout<<"栈空";
}
else
{
e=*--S->top;
}
}
void StackEmpty(SqStack S)
{
if(S.base==S.top)
{
cout<<"栈为空";
}
else
{
cout<<"栈不空";
}
}
int main()
{
SqStack *S;
int i,e,n,j;
while(1)
{
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<<"请输入要执行的步骤:";
cin>>i;
if(i==1)
{
InitStack(S);
}
if(i==2)
{
cout<<"请输入要插入的元素个数:";
cin>>n;
for(j=0;j<n;j++)
{
cout<<"请输入要插入的元素:";
cin>>e;
Push(S,e);
}
}
if(i==3)
{
StackEmpty(*S);
}
if(i==4)
{
ClearStack(S);
}
if(i==5)
{
StackLength(*S);
}
if(i==6)
{
GetTop(*S,e);
}
if(i==7)
{
Pop(S,e);
}
if(i==8)
{
DestroyStack(S);
}
if(i==9)
{
Show(*S);
}
if(i==10)
{
break;
}
}
return 0;
}
编译可以通过。但是输入1初始化栈的时候就会停止运行。不知道为什么。。求解答