#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW 2
#define OK 1
typedef int SElemType;
typedef int Status;
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;
}
Status ChuShihua(SqStack &S)
{
int n;
printf("請輸入資料以-1結束\n");
while(scanf("%d",&n),n!=-1)
*S.top++=n;
Status Push(SqStack &S)
int e;
printf("請輸入要壓入的元素:");
scanf("%d",&e);
if(S.top-S.base>=S.stacksize){
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
*S.top++=e;
Status Pop(SqStack &S)
printf("出棧元素為:");
if(S.top!=S.base)
printf("%d\n",*--S.top);
return 0;
void tishi()
printf("所有操作如下:\n");
printf("(1)採用順序儲存實現棧的初始化操作。\n");
printf("(2)採用順序儲存實現棧的入棧操作。\n");
printf("(3)採用順序儲存實現棧的出棧操作。\n");
printf("(-1)退出\n");
printf("請選擇:");
int main()
{ int m;
SqStack s;
InitStack(s);
do{
tishi();
scanf("%d",&m);
switch(m)
case 1:
ChuShihua(s);
break;
case 2:
Push(s);
case 3:
Pop(s);
}while(m!=-1);
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW 2
#define OK 1
typedef int SElemType;
typedef int Status;
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;
}
Status ChuShihua(SqStack &S)
{
int n;
printf("請輸入資料以-1結束\n");
while(scanf("%d",&n),n!=-1)
*S.top++=n;
return OK;
}
Status Push(SqStack &S)
{
int e;
printf("請輸入要壓入的元素:");
scanf("%d",&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)
{
printf("出棧元素為:");
if(S.top!=S.base)
printf("%d\n",*--S.top);
return 0;
}
void tishi()
{
printf("所有操作如下:\n");
printf("(1)採用順序儲存實現棧的初始化操作。\n");
printf("(2)採用順序儲存實現棧的入棧操作。\n");
printf("(3)採用順序儲存實現棧的出棧操作。\n");
printf("(-1)退出\n");
printf("請選擇:");
}
int main()
{ int m;
SqStack s;
InitStack(s);
do{
tishi();
scanf("%d",&m);
switch(m)
{
case 1:
ChuShihua(s);
break;
case 2:
Push(s);
break;
case 3:
Pop(s);
break;
}
}while(m!=-1);
return OK;
}