#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Intstack
{
int *elems;
int loglength;
int alloclength;
} Intstack;
void IntstackNew(Intstack *s)
{
s->loglength=0;
s->alloclength=4;
s->elems=malloc(4*sizeof(int));
assert(s->elems!=NULL);
}
void IntstackFree(Intstack *s)
{
free(s->elems);
}
void IntstackPush(Intstack *s, int value)
{
if(s->alloclength==s->loglength)
{
s->alloclength *= 2;
s->elems=realloc(s->elems, s->alloclength*sizeof(int));
assert(s->elems!=NULL);
}
s->elems[s->loglength] = value;
s->loglength++;
}
int IntstackPop(Intstack *s)
{
assert(s->loglength>0);
s->loglength--;
return s->elems[s->loglength];
}
int main()
{
Intstack s;
IntstackNew(&s);
IntstackPush(&s, 4);
IntstackPush(&s, 5);
printf("%d\n", IntstackPop(&s));
printf("%d\n", IntstackPop(&s));
IntstackFree(&s);
printf("Hello world!\n");
return 0;
}
C语言小函数
最新推荐文章于 2021-05-16 12:14:47 发布