💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ### 定义 ![](https://box.kancloud.cn/2016-09-07_57cf6b3f49c93.png) ### ADT ### 代码实现 ``` #include <stdio.h> #include <stdlib.h> #include <assert.h> struct elt { struct elt *next; int value; }; typedef struct elt *Stack; #define STACK_EMPTY (0) void stackPush(Stack *s,int value) { struct elt *e; e = malloc(sizeof(* e)); assert(e); e->value = value; e->next = *s; *s = e; } int stackEmpty(const Stack *s) { return (*s == 0); } int stackPop(Stack *s) { int ret; struct elt *e; assert(!stackEmpty(s)); ret = (*s)->value; e = *s; *s = e->next; free(e); return ret; } int stackPrint(const Stack *s) { struct elt *e; for(e = *s; e!=0;e=e->next) { printf("%d ",e->value); } putchar('\n'); } //looping over backwords a stack /* void stackPrintReversed(const Stack *s) { Stack s2; struct elt *e; for(e = *s; e!=0;e=e->next) { stackPush(&s2,e->value); } stackPrint(s2); } */ int main(int argc,char **argv) { int i; Stack s; s = STACK_EMPTY; for(i=0;i<5;i++) { printf("push %d\n",i); stackPush(&s,i); stackPrint(&s); } while(!stackEmpty(&s)) { printf("pop gets %d\n",stackPop(&s)); stackPrint(&s); } stackPrintReversed(&s); return 0; } ```