mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-03 10:43:15 +08:00
4-21
This commit is contained in:
40
code/practise/19 程序设计/01 栈的实现/01 栈模块/array_stack.c
Normal file
40
code/practise/19 程序设计/01 栈的实现/01 栈模块/array_stack.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#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];
|
||||
}
|
||||
|
||||
57
code/practise/19 程序设计/01 栈的实现/01 栈模块/linkedlist_stack.c
Normal file
57
code/practise/19 程序设计/01 栈的实现/01 栈模块/linkedlist_stack.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#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;
|
||||
}
|
||||
12
code/practise/19 程序设计/01 栈的实现/01 栈模块/stack.h
Normal file
12
code/practise/19 程序设计/01 栈的实现/01 栈模块/stack.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
#include<stdbool.h> //C99 only
|
||||
|
||||
void make_empty();
|
||||
bool is_empty();
|
||||
bool is_full();
|
||||
void push(int i);
|
||||
int pop();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
//#define _CRT_SECURE_NO_WARNINGS 1
|
||||
//
|
||||
//#include<stdio.h>
|
||||
//#include<stdlib.h>
|
||||
//#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];
|
||||
//}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef STACKADT_H
|
||||
#define STACKADT_H
|
||||
|
||||
#include<stdbool.h>
|
||||
|
||||
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
|
||||
@@ -0,0 +1,73 @@
|
||||
//#define _CRT_SECURE_NO_WARNINGS 1
|
||||
//
|
||||
//#include<stdio.h>
|
||||
//#include<stdlib.h>
|
||||
//#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];
|
||||
//}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef STACKADT3_H
|
||||
#define STACKADT3_H
|
||||
|
||||
#include<stdbool.h>
|
||||
|
||||
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
|
||||
79
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.c
Normal file
79
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#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;
|
||||
}
|
||||
18
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.h
Normal file
18
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/03 链表实现/stackADT4.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef STACKADT4_H
|
||||
#define STACKADT4_H
|
||||
|
||||
#include<stdbool.h>
|
||||
|
||||
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
|
||||
1
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/readme.md
Normal file
1
code/practise/19 程序设计/01 栈的实现/02 栈抽象数据类型/readme.md
Normal file
@@ -0,0 +1 @@
|
||||
不同的实现方式中 接口(头文件)和客户(包含main函数的文件)改变的很少
|
||||
Reference in New Issue
Block a user