我用的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);
}