#include
#include
#include
#include
typedef int SElemType;
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack S)
{
(*S).base=(SElemType)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(*S).base) exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
void DestroyStack(SqStack *S)
{
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
}
Status StackEmpty(SqStack S)
{
if(S.top==0) return OK;
else
return ERROR;
}
int ClearStack(SqStack *S)
{
(*S).top=(*S).base;
return 0;
}
int StackLength(SqStack S)
{
int i=0;
i=(S).top-(S).base;
return i;
}
Status GetTop(SqStack S,SElemType e)
{//返回栈顶元素
if (S.top==S.base) return ERROR;
*e=(S.top-1);
return OK;
}
Status 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) exit(OVERFLOW);
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;
}
*(*S).top++=e;
return OK;
}
Status Pop(SqStack S,SElemType *e)
{
if((*S).top=(*S).base) return ERROR;
--(*S).top;
*e=(*S).top;
printf("输出的元素为%d:",&e);
return OK;
}
void shuzhi()
{
SqStack S;
int e;
InitStack(&S);
printf("输入要转换的数:");
scanf("%d",e);
while(e>0)
{
Push(&S,e%8);
e=e/8;
}
if(StackEmpty(S)) printf("栈为空:");
else
{
Pop(&S,&e);
printf("输出的数为%d\n",e);
}
}
void OutputStack(SqStack *S)
{
int *q,i;
if((*S).stacksize==0)printf("The list is empty:");
q=(*S).top-1;
for(i=0;i<(*S).top-(*S).base;i++)
{
printf("输出的栈为%d\n",q);
q--;
}
}
void menu()
{
printf(" ********************************************* \n");
printf(" * 0.创建栈 * 1.销毁栈 * \n");
printf(" ******************************************** \n");
printf(" * 2.判断栈空 * * 3.清空栈* \n");
printf(" ********************************************* \n");
printf(" * 4.测量栈长 * * 5.获得栈顶元素* \n");
printf(" ********************************************* \n");
printf(" * 6.入栈 * * 7.出栈 * \n");
printf(" ********************************************* \n");
printf(" * 8.数制转换 * * 9.输出元素* \n");
printf(" ********************** ********************** \n");
printf(" ********* 按任意键进入系统 ********************** \n");
printf(" ---------------------- ---------------------- \n");
printf("按任意键继续\n");
getch();
system("cls");
}
void main()
{
int a,b,num,i=0;
SElemType e;
SqStack S;
while(1)
{ menu();
printf("请选择菜单编号:");
scanf("%d",&num);
switch(num)
{
case 0: InitStack(&S);
printf("按任意键继续\n");
break;
case 1:DestroyStack(&S);
printf("按任意键继续\n");
break;
case 2:{
e=StackLength(S);
if(e==0)
printf("该栈为空!");
else
printf("该栈不为空!");
break;
}
case 3:ClearStack(&S);
printf("按任意键继续\n");
break;
case 4:
printf("栈的长度为:%d\n",StackLength(S));
printf("按任意键继续\n");
break;
case 5:GetTop(S,&e);
printf("删除的栈顶元素为:%d\n",e);
printf("按任意键继续\n");
break;
case 6:Push(&S,e);
printf("请输入要进栈的元素个数:\n");
scanf("%d",&a);
printf("请输入要进栈的%d个元素:\n",a);
for(b=0;b<a;b++)
{
scanf("%d",&e);
}
printf("按任意键继续\n");
break;
case 7:Pop(&S,&e);
printf("按任意键继续\n");
break;
case 8:shuzhi();
printf("按任意键继续\n"); break;
case 9:OutputStack(&S);
printf("按任意键继续\n");break;
default:printf("请在0-9之间选择\n");break;
}
}
}
可用“插入代码”重新编辑,不会导致代码显示缺失,方便找错