探索,使用栈的定义,定义一个通用类型的栈来存储一系列的字符串,并把它们以相反的顺序打印出来。
typedef struct{ void *elems; int elemSize; int loglength; int alloclength; void(*freefn)(void*); }
int main(--,--){ const char* friends = {"AI","Bob","Carl"}; stack stringStack; StackNew(&stringStack,sizeof(char*));//希望栈能够动态保存动态申请的C字符串的地址 for(int i=0;i<3;i++){ char* copy = strdup(friends[i]); StackPush(&stringStack,©); } char *name; for(int i=0;i<3;i++){ stackPop(&stringStack,&name); printf("%s\n,name)} free(name); } stack dispose(&stringStack);
void StackNew(stack *s,int elemSize,void(*freefn)(void*));
void stackDispose(stack *s){ if(s->freefn != NULL){ for(int i=0,i<s->loglengtj;i++){ s->freefn((char*)s->elems + i*s->elemsSize); } } free(s->elems); }
void stringFree(void *elem){ free(*(char**)elem), }
标签:freefn,范式,07,int,void,elems,char,CS107,stack From: https://www.cnblogs.com/zhimingyiji/p/16625867.html