标签:user getc getch pause nbsp ems 随笔 sys console
#include <stdio.h>
#include <stdlib.h>
#include<assert.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct{
int *elems;
int loglength;
int alloclength;
}stack;
void StackNew(stack*s);
void StackDispose(stack*s);
void StackPush(stack*s,int volue);
int StackPop(stack*s);
void StackPush(stack*s,int value)
{
if(s->loglength==s->alloclength)
{
s->alloclength*=2;
s->elems=realloc(s->elems,s->alloclength*sizeof(int));
assert(s->elems!=NULL);
}
s->elems[s->loglength]=value;
s->loglength++;
}
void StackNew(stack*s)
{
s->loglength=0;
s->alloclength=4;
s->elems=malloc(4*sizeof(int));
assert(s->elems!=NULL);
}
int StackPop(stack*s)
{
assert(s->loglength>0);
s->loglength--;
return s->elems[s->loglength];
}
void StackDispose(stack*s)
{
free(s->elems);
}
int main()
{
stack s;
int i;
StackNew(&s);
for(i=0;i<4;i++)
StackPush(&s,i);
for(i=0;i<4;i++)
printf("%d",StackPop(&s));
StackDispose(&s);
}
C++随笔
标签:user getc getch pause nbsp ems 随笔 sys console
原文地址:https://www.cnblogs.com/shiheyuanfang/p/11921614.html