主题:解决了。基础知识都忘了
#include<stdio.h>
#include<malloc.h>
#define stack_size 100//初始量
#define stackincrement 10//增量
#define ok 1
#define error 0
#define overflow -1
//================================
typedef struct
{
int *base;
int *top;
int stacksize;
}*sqstack;
//=================================
int initstack(sqstack s)
{
s->base=(int*)malloc(stack_size*sizeof(int));
if(!s->base) return (overflow);
s->top=s->base;
s->stacksize+=stack_size;
return ok;
}
int 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) return (overflow);
s->top=s->base+stackincrement;
s->stacksize+=stackincrement;
}
*s->top++=e;
return ok;
}
int gettop(sqstack s,int e)//返回栈顶元素
{
if(s->top==s->base)
return error;
e=*(s->top-1);
return e;
}
int pop(sqstack s,int e)//出栈
{
if(s->top==s->base)
return error;
e=*--s->top;
return e;
}
int main()
{
int a[5],i,j,k;
sqstack sq=NULL;//建一个结构体变量
initstack(sq);//建空栈
printf("input the number:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]); //输入一个数据后就无法往下运行了
push(sq,a[i]);//入栈
}
printf("\n");
printf("%d",gettop(sq,j));
printf("\n");
printf("%d\n",pop(sq,k));
return ok;
}
#include<malloc.h>
#define stack_size 100//初始量
#define stackincrement 10//增量
#define ok 1
#define error 0
#define overflow -1
//================================
typedef struct
{
int *base;
int *top;
int stacksize;
}*sqstack;
//=================================
int initstack(sqstack s)
{
s->base=(int*)malloc(stack_size*sizeof(int));
if(!s->base) return (overflow);
s->top=s->base;
s->stacksize+=stack_size;
return ok;
}
int 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) return (overflow);
s->top=s->base+stackincrement;
s->stacksize+=stackincrement;
}
*s->top++=e;
return ok;
}
int gettop(sqstack s,int e)//返回栈顶元素
{
if(s->top==s->base)
return error;
e=*(s->top-1);
return e;
}
int pop(sqstack s,int e)//出栈
{
if(s->top==s->base)
return error;
e=*--s->top;
return e;
}
int main()
{
int a[5],i,j,k;
sqstack sq=NULL;//建一个结构体变量
initstack(sq);//建空栈
printf("input the number:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]); //输入一个数据后就无法往下运行了
push(sq,a[i]);//入栈
}
printf("\n");
printf("%d",gettop(sq,j));
printf("\n");
printf("%d\n",pop(sq,k));
return ok;
}