From 4646b03c9f8743c1024536de59a0f9c330d8db64 Mon Sep 17 00:00:00 2001 From: hairrrrr <781728963@qq.com> Date: Tue, 21 Apr 2020 12:57:11 +0800 Subject: [PATCH] 4-21 --- .../02 栈抽象数据类型/04 最终形态/final_stackADT.c | 79 +++++++++++++++++++ .../02 栈抽象数据类型/04 最终形态/final_stackADT.h | 18 +++++ .../02 栈抽象数据类型/04 最终形态/stackclient.c | 26 ++++++ 3 files changed, 123 insertions(+) create mode 100644 code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.c create mode 100644 code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.h create mode 100644 code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/stackclient.c diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.c b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.c new file mode 100644 index 0000000..ae63f34 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.c @@ -0,0 +1,79 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include +#include +#include +#include"final_stackADT.h" + +#define PUBLIC +#define PRIVATE static + +typedef struct node{ + Item data; + struct node* next; +}node; + +typedef struct stack_type { + node* top; + Item pop_val; +}stack_type; + + +PUBLIC Stack stack_create() { + + Stack s = (Stack)malloc(sizeof(stack_type)); + + assert(s != NULL); + + s->top = NULL; + + return s; +} + +PUBLIC void stack_destory(Stack s) { + + stack_make_empty(s); + free(s); +} + + + +PUBLIC void stack_make_empty(Stack s) { + while (!stack_is_empty(s)) + stack_pop(s); +} + +PUBLIC bool stack_is_empty(Stack s) { + return s->top == NULL; +} + +PUBLIC bool stack_is_full(Stack s) { + return false; +} + +PUBLIC bool stack_push(Stack s, Item i) { + + node* new_node = (node*)malloc(sizeof(node)); + if (new_node == NULL) + return false; + + new_node->data = i; + new_node->next = s->top; + s->top = new_node; + + return true; +} + +PUBLIC Item* stack_pop(Stack s) { + + if (stack_is_empty(s)) + return NULL; + + node* del = s->top; + s->pop_val = del->data; + s->top = s->top->next; + free(del); + + return &s->pop_val; +} diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.h b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.h new file mode 100644 index 0000000..b89ffe6 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/final_stackADT.h @@ -0,0 +1,18 @@ +#ifndef FINAL_STACKADT_H +#define FINAL_STACKADT_H + +#include + +typedef int Item; + +typedef struct stack_type* Stack; + +Stack stack_create(); +void stack_destory(Stack s); +void stack_make_empty(Stack s); +bool stack_is_empty(const Stack s); +bool stack_is_full(const Stack s); +bool stack_push(Stack s, Item i); +Item* stack_pop(Stack s); + +#endif \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/stackclient.c b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/stackclient.c new file mode 100644 index 0000000..3468490 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/04 最终形态/stackclient.c @@ -0,0 +1,26 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include"final_stackADT.h" + +int main(void) { + + Stack s1, s2; + + s1 = stack_create(); + s2 = stack_create(); + + stack_push(s1, 1); + stack_push(s1, 2); + + printf("%d\n", *stack_pop(s1)); + printf("%d\n", *stack_pop(s1)); + + stack_destory(s1); + stack_destory(s2); + + return 0; +} + + +