我用的vc6.0与gcc
//相关定义
//3-1define.h
#ifndef _MYDEFINEFILE_H
#define _MYDEFINEFILE_H
#define INITSIZE 100
#define ADDSIZE 10
typedef int elemtype;
typedef struct
{
int top;
elemtype *base;
int stacksize;
}sqstack;
#endif
//函数
//3-1lib.h
#include<stdio.h>
#include<malloc.h>
#include"3-1define.h"
void initstack(sqstack *s)
{
s->base=(elemtype *)malloc(INITSIZE*sizeof(elemtype));
s->top=0;
s->stacksize=INITSIZE;
printf("OK ,CREATDE!\n");
}
int getlen(sqstack *s)
{
return s->top;
}
int gettop(sqstack *s)
{
if(s->top<=0)
return 0;
else
return 1;
}
int push(sqstack *s,elemtype x)
{
if(s->top>=s->stacksize)
{s->base=(elemtype*)malloc(sizeof(elemtype)*(INITSIZE+ADDSIZE));
if(!s->base)
return 0;
s->stacksize=INITSIZE+ADDSIZE;
}
s->base[s->top++]=x;
return 1;
}
int pop(sqstack *s,elemtype *e)
{
if(s->top==0)
return 0;
*e=s->base[--s->top];
return 1;
}
int stackempty(sqstack *s)
{
if(s->top==0)
return 1;
else
return 0;
}
void list(sqstack *s)
{
int i;
for(i=s->top-1;i>=0;i--)
printf("%4d",s->base[i]);
printf("\n");
}
//主函数
#include<stdio.h>
#include"3-1define.h"
#include"3-1lib.h"
void main()
{
sqstack *s=(sqstack *)0;
int i=0,j;
int e;
initstack(s);
printf("how many do you want to creat:\n");
scanf("%d",&i);
for(j=0;j<i;j++)
{
printf("input %d e:\n",j+1);
scanf("%d",&e);
push(s,e);
}
list(s);
}
----------------解决方案--------------------------------------------------------
在调用initstack(s)之前,应该先给s申请空间,然后才能使用s->base
----------------解决方案--------------------------------------------------------
看看主函数,我用了的
----------------解决方案--------------------------------------------------------
........................
[此贴子已经被作者于2007-10-19 23:23:56编辑过]
----------------解决方案--------------------------------------------------------
看看主函数,我用了的
我是说你没有为s申请空间,告诉你吧:
要这样,而不是将s赋值为0!!!
s = (sqstack *)malloc(sizeof(sqstack));
----------------解决方案--------------------------------------------------------
谢谢哦
----------------解决方案--------------------------------------------------------