From c6bc541ab58363d783e60a007e80e9bf9e231fda Mon Sep 17 00:00:00 2001 From: garhve Date: Mon, 5 Dec 2022 19:43:39 +0800 Subject: initialize --- .../\346\240\210/\351\223\276\346\240\210/stack.c" | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 "c/dataStructure/\346\240\210\345\222\214\351\230\237\345\210\227/\346\240\210/\351\223\276\346\240\210/stack.c" (limited to 'c/dataStructure/栈和队列/栈/链栈/stack.c') diff --git "a/c/dataStructure/\346\240\210\345\222\214\351\230\237\345\210\227/\346\240\210/\351\223\276\346\240\210/stack.c" "b/c/dataStructure/\346\240\210\345\222\214\351\230\237\345\210\227/\346\240\210/\351\223\276\346\240\210/stack.c" new file mode 100755 index 0000000..e47dd81 --- /dev/null +++ "b/c/dataStructure/\346\240\210\345\222\214\351\230\237\345\210\227/\346\240\210/\351\223\276\346\240\210/stack.c" @@ -0,0 +1,56 @@ +#include "stack.h" + +bool InitStack(Stack * s) { + *s = NULL; + return true; +} + +bool DestroyStack(Stack * s) { + Stack p; + + while (*s) { + p = *s; + *s = (*s)->next; + free (p); + } + return true; +} + +bool Push(Stack * s, ElemType n) { + Stack p = (stack *) malloc (sizeof(stack)); + if (!p) { + fprintf(stderr,"No enough memory\n"); + return false; + } + p->data = n; + p->next = *s; + + *s = p; + + return true; +} + +bool Pop(Stack * s, ElemType * n) { + if (!*s) { + fprintf(stderr,"No element in stack!\n"); + return false; + } + *n = (*s)->data; + Stack p = *s; + *s = (*s)->next; + free(p); + + return true; +} + +ElemType GetTop(Stack * s) { + if (*s) + return (*s)->data; + return -1; +} + +bool StackIsEmpty (Stack * s) { + if (!*s) + return true; + return false; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2