diff --git a/code/practise/19 程序设计/01 栈的实现/01 栈模块/array_stack.c b/code/practise/19 程序设计/01 栈的实现/01 栈模块/array_stack.c new file mode 100644 index 0000000..a90ac5c --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/01 栈模块/array_stack.c @@ -0,0 +1,40 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include"stack.h" + +#define STACK_SIZE 100 + +static int contents[STACK_SIZE]; +static int top = 0; + +static void terminate(const char* message) { + printf("%s\n", message); + exit(EXIT_FAILURE); +} + +void make_empty() { + top = 0; +} + +bool is_empty() { + return top == 0; +} + +bool is_full() { + return top == STACK_SIZE; +} + +void push(int i) { + if (is_full()) + terminate("Error in push: stack is full\n"); + contents[top++] = i; +} + +int pop() { + if (is_empty()) + printf("Error in pop: stack is empty\n"); + return contents[--top]; +} + diff --git a/code/practise/19 程序设计/01 栈的实现/01 栈模块/linkedlist_stack.c b/code/practise/19 程序设计/01 栈的实现/01 栈模块/linkedlist_stack.c new file mode 100644 index 0000000..0b87d76 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/01 栈模块/linkedlist_stack.c @@ -0,0 +1,57 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include +#include"stack.h" + +typedef struct node { + int data; + struct node* next; +}node; + +static node* top = NULL; + +static void terminate(char* message) { + printf("%s\n", message); + exit(EXIT_FAILURE); +} + +void make_empty() { + while (!is_empty()) + pop(); +} + +bool is_empty() { + return top == NULL; +} + +bool is_full() { + return false; +} + +void push(int i) { + + node* new_node = (node*)malloc(sizeof(node)); + if (new_node == NULL) { + terminate("Error in push: stack is full.\n"); + exit(EXIT_FAILURE); + } + + new_node->data = i; + new_node->next = top; + top = new_node; +} + +int pop() { + + if (is_empty()) + terminate("Error in pop: stack is empty.\n"); + + int data = top->data; + + node* del = top; + top = top->next; + free(del); + + return data; +} diff --git a/code/practise/19 程序设计/01 栈的实现/01 栈模块/stack.h b/code/practise/19 程序设计/01 栈的实现/01 栈模块/stack.h new file mode 100644 index 0000000..deecc42 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/01 栈模块/stack.h @@ -0,0 +1,12 @@ +#ifndef STACK_H +#define STACK_H + +#include //C99 only + +void make_empty(); +bool is_empty(); +bool is_full(); +void push(int i); +int pop(); + +#endif diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.c b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.c new file mode 100644 index 0000000..16ab5fa --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.c @@ -0,0 +1,71 @@ +//#define _CRT_SECURE_NO_WARNINGS 1 +// +//#include +//#include +//#include"stackADT.h" +// +//#define STACK_SIZE 100 +// +//typedef struct stack_type { +// int contents[STACK_SIZE]; +// int top; +//}stack_type; +// +// +// +//static void terminate(char* message) { +// printf("%s\n", message); +// exit(EXIT_FAILURE); +//} +// +//Stack create() { +// +// Stack s = (Stack)malloc(sizeof(stack_type)); +// if (s == NULL) { +// terminate("Error in create: stack could not be created.\n"); +// exit(EXIT_FAILURE); +// } +// s->top = 0; +// +// return s; +//} +// +// +//void destory(Stack s) { +// +// free(s); +//} +// +// +//void make_empty(Stack s) { +// +// s->top = 0; +//} +// +//bool is_empty(Stack s) { +// return s->top == 0; +//} +// +//bool is_full(Stack s) { +// return s->top == STACK_SIZE; +//} +// +//void push(Stack s, int i) { +// +// if (is_full(s)) { +// terminate("Error in push: stack is full.\n"); +// exit(EXIT_FAILURE); +// } +// +// s->contents[s->top++] = i; +//} +// +//int pop(Stack s) { +// +// if (is_empty(s)) { +// terminate("Error in pop: stack is empty.\n"); +// exit(EXIT_FAILURE); +// } +// +// return s->contents[--s->top]; +//} \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.h b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.h new file mode 100644 index 0000000..1c1de10 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/01 定长数组实现/stackADT.h @@ -0,0 +1,16 @@ +#ifndef STACKADT_H +#define STACKADT_H + +#include + +typedef struct stack_type* Stack; + +Stack create(); +void destory(Stack s); +void make_empty(Stack s); +bool is_empty(const Stack s); +bool is_full(const Stack s); +void push(Stack s, int i); +int pop(Stack s); + +#endif \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.c b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.c new file mode 100644 index 0000000..e1275ab --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.c @@ -0,0 +1,73 @@ +//#define _CRT_SECURE_NO_WARNINGS 1 +// +//#include +//#include +//#include"stackADT3.h" +// +// +//typedef struct stack_type { +// int top; +// int size; +// Item contents[]; // +//}stack_type; +// +// +// +//static void terminate(char* message) { +// printf("%s\n", message); +// exit(EXIT_FAILURE); +//} +// +//Stack create(int size) { +// +// // sizeof(stack_type) ĴС +// Stack s = (Stack)malloc(sizeof(stack_type) + sizeof(Item) * size); +// if (s == NULL) { +// terminate("Error in create: stack could not be created.\n"); +// exit(EXIT_FAILURE); +// } +// s->top = 0; +// s->size = size; +// +// return s; +//} +// +// +//void destory(Stack s) { +// +// free(s); // ֻҪͷһ +//} +// +// +//void make_empty(Stack s) { +// +// s->top = 0; +//} +// +//bool is_empty(Stack s) { +// return s->top == 0; +//} +// +//bool is_full(Stack s) { +// return s->top == s->size; +//} +// +//void push(Stack s, Item i) { +// +// if (is_full(s)) { +// terminate("Error in push: stack is full.\n"); +// exit(EXIT_FAILURE); +// } +// +// s->contents[s->top++] = i; +//} +// +//Item pop(Stack s) { +// +// if (is_empty(s)) { +// terminate("Error in pop: stack is empty.\n"); +// exit(EXIT_FAILURE); +// } +// +// return s->contents[--s->top]; +//} \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.h b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.h new file mode 100644 index 0000000..d2768f6 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/02 动态数组实现/stackADT3.h @@ -0,0 +1,18 @@ +#ifndef STACKADT3_H +#define STACKADT3_H + +#include + +typedef int Item; + +typedef struct stack_type* Stack; + +Stack create(int size); +void destory(Stack s); +void make_empty(Stack s); +bool is_empty(const Stack s); +bool is_full(const Stack s); +void push(Stack s, Item i); +Item pop(Stack s); + +#endif \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.c b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.c new file mode 100644 index 0000000..0133168 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.c @@ -0,0 +1,79 @@ +#define _CRT_SECURE_NO_WARNINGS 1 + +#include +#include +#include"stackADT4.h" + +typedef struct node{ + Item data; + struct node* next; +}node; + +typedef struct stack_type { + node* top; +}stack_type; + + +static void terminate(char* message) { + printf("%s\n", message); + exit(EXIT_FAILURE); +} + +Stack create() { + Stack s = (Stack)malloc(sizeof(stack_type)); + if (s == NULL) { + terminate("Error in create: stack could not be created.\n"); + exit(EXIT_FAILURE); + } + s->top = NULL; + + return s; +} + +void destory(Stack s) { + + make_empty(s); + free(s); +} + + + +void make_empty(Stack s) { + while (!is_empty(s)) + pop(s); +} + +bool is_empty(Stack s) { + return s->top == NULL; +} + +bool is_full(Stack s) { + return false; +} + +void push(Stack s, Item i) { + + node* new_node = (node*)malloc(sizeof(node)); + if (new_node == NULL) { + terminate("Error in push: stack is full.\n"); + exit(EXIT_FAILURE); + } + + new_node->data = i; + new_node->next = s->top; + s->top = new_node; +} + +Item pop(Stack s) { + + if (is_empty(s)) + terminate("Error in pop: stack is empty.\n"); + + int data = s->top->data; + + node* del = s->top; + s->top = s->top->next; + free(del); + + return data; +} diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.h b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.h new file mode 100644 index 0000000..97b2ec9 --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.h @@ -0,0 +1,18 @@ +#ifndef STACKADT4_H +#define STACKADT4_H + +#include + +typedef int Item; + +typedef struct stack_type* Stack; + +Stack create(); +void destory(Stack s); +void make_empty(Stack s); +bool is_empty(const Stack s); +bool is_full(const Stack s); +void push(Stack s, Item i); +Item pop(Stack s); + +#endif \ No newline at end of file diff --git a/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/readme.md b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/readme.md new file mode 100644 index 0000000..0ba242e --- /dev/null +++ b/code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/readme.md @@ -0,0 +1 @@ +不同的实现方式中 接口(头文件)和客户(包含main函数的文件)改变的很少 \ No newline at end of file