1.data.h
# ifndef _DATA_H
# define _DATA_H
typedef int ElemType;
# endif
2.stack.h
# ifndef _STACK_H
# define _STACK_H
# include "data.h"
# define STACK_INIT_SIZE 10
# define STACK_INCREME 10
typedef struct
{
ElemType * base;
ElemType * top;
int size;
}STACK;
int Pop(STACK * s, ElemType * e);
int Push(STACK * s, ElemType * e);
void DestroyStack(STACK * s);
STACK * InitStack();
int IsEmpty(STACK * s);
# endif
3.stack.c
# include "stack.h"
# include <stdio.h>
# include <stdlib.h>
STACK * InitStack()
{
STACK * s = (STACK *)malloc(sizeof(STACK));
if(NULL == s)
exit(0);
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(NULL == s->base) exit(0);
s->top = s->base;
s->size = STACK_INIT_SIZE;
return s;
}
void DestroyStack(STACK * s)
{
free(s->base);
free(s);
}
int Push(STACK * s, ElemType * e)
{
if(s == NULL || e == NULL)
return 0;
if(s->top - s->base >= s->size)
{
s->base = (ElemType *)realloc(s->base, (s->size +
STACK_INCREME) * sizeof(ElemType));
if(s->base == NULL) return 0;
s->top = s->base + s->size;
s->size = s->size + STACK_INCREME;
}
* s->top ++ = * e;
return 1;
}
int Pop(STACK * s, ElemType * e)
{
if(s == NULL || e == NULL)
return 0;
if(s->base == s->top)
return 0;
* e = * --s->top;
return 1;
}
int IsEmpty(STACK * s)
{
return s->top == s->base ? 1 : 0;
}
4.main.c
# include "stack.h"
# include <stdio.h>
void main()
{
int num = 1348;
int temp;
STACK * s = InitStack();
while(num)
{
temp = num % 8;
Push(s, &temp);
num /= 8;
}
while(!IsEmpty(s))
{
Pop(s, &temp);
printf("%d", temp);
}
printf("\n");
DestroyStack(s);
}