diff --git a/.gitignore b/.gitignore
index a89a933..5051599 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,6 +51,7 @@ Module.symvers
Mkfile.old
dkms.conf
/.vs/Project2
+.vscode/
*.tlog
*.tlog
*.recipe
@@ -59,4 +60,4 @@ dkms.conf
*.tlog
*.tlog
Project2/Debug/Project2.tlog/link.read.1.tlog
-Project2/Debug/Project2.tlog/link.secondary.1.tlog
+Project2/Debug/Project2.tlog/link.secondary.1.tlog
\ No newline at end of file
diff --git a/Project2.vcxproj b/Project2.vcxproj
index 79156a2..451a113 100644
--- a/Project2.vcxproj
+++ b/Project2.vcxproj
@@ -19,6 +19,7 @@
+
@@ -26,6 +27,7 @@
+
diff --git a/Project2.vcxproj.filters b/Project2.vcxproj.filters
index c70b33b..45faa74 100644
--- a/Project2.vcxproj.filters
+++ b/Project2.vcxproj.filters
@@ -24,10 +24,13 @@
源文件
-
+
源文件
-
+
+ 源文件
+
+
源文件
@@ -44,5 +47,8 @@
头文件
+
+ 头文件
+
\ No newline at end of file
diff --git a/Project2/Debug/Project2.Build.CppClean.log b/Project2/Debug/Project2.Build.CppClean.log
deleted file mode 100644
index 2f60bb0..0000000
--- a/Project2/Debug/Project2.Build.CppClean.log
+++ /dev/null
@@ -1,18 +0,0 @@
-c:\code\lencode\project2\project2\debug\vc143.pdb
-c:\code\lencode\project2\project2\debug\vc143.idb
-c:\code\lencode\project2\project2\debug\sq_list.obj
-c:\code\lencode\project2\project2\debug\main.obj
-c:\code\lencode\project2\project2\debug\linked_list_stack.obj
-c:\code\lencode\project2\project2\debug\linked_list.obj
-c:\code\lencode\project2\project2\debug\array_stack.obj
-c:\code\lencode\project2\debug\project2.exe
-c:\code\lencode\project2\debug\project2.pdb
-c:\code\lencode\project2\project2\debug\project2.ilk
-c:\code\lencode\project2\project2\debug\project2.tlog\cl.command.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\cl.items.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\cl.read.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\cl.write.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\link.command.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\link.read.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\link.secondary.1.tlog
-c:\code\lencode\project2\project2\debug\project2.tlog\link.write.1.tlog
diff --git a/Project2/Debug/Project2.exe.recipe b/Project2/Debug/Project2.exe.recipe
deleted file mode 100644
index 5058dca..0000000
--- a/Project2/Debug/Project2.exe.recipe
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
- C:\code\lencode\Project2\Debug\Project2.exe
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Project2/Debug/Project2.log b/Project2/Debug/Project2.log
deleted file mode 100644
index eafe481..0000000
--- a/Project2/Debug/Project2.log
+++ /dev/null
@@ -1,3 +0,0 @@
- array_stack.c
-C:\code\lencode\Project2\array_stack.c(13,5): warning C4013: “memset”未定义;假设外部返回 int
- Project2.vcxproj -> C:\code\lencode\Project2\Debug\Project2.exe
diff --git a/Project2/Debug/Project2.tlog/CL.command.1.tlog b/Project2/Debug/Project2.tlog/CL.command.1.tlog
deleted file mode 100644
index f35c952..0000000
Binary files a/Project2/Debug/Project2.tlog/CL.command.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.tlog/CL.read.1.tlog b/Project2/Debug/Project2.tlog/CL.read.1.tlog
deleted file mode 100644
index 2d1da44..0000000
Binary files a/Project2/Debug/Project2.tlog/CL.read.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.tlog/CL.write.1.tlog b/Project2/Debug/Project2.tlog/CL.write.1.tlog
deleted file mode 100644
index 9a03e9e..0000000
Binary files a/Project2/Debug/Project2.tlog/CL.write.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.tlog/Cl.items.tlog b/Project2/Debug/Project2.tlog/Cl.items.tlog
deleted file mode 100644
index 86bcd1f..0000000
--- a/Project2/Debug/Project2.tlog/Cl.items.tlog
+++ /dev/null
@@ -1,5 +0,0 @@
-C:\code\lencode\Project2\array_stack.c;C:\code\lencode\Project2\Project2\Debug\array_stack.obj
-C:\code\lencode\Project2\linked_list.c;C:\code\lencode\Project2\Project2\Debug\linked_list.obj
-C:\code\lencode\Project2\linked_list_stack.c;C:\code\lencode\Project2\Project2\Debug\linked_list_stack.obj
-C:\code\lencode\Project2\main.c;C:\code\lencode\Project2\Project2\Debug\main.obj
-C:\code\lencode\Project2\sq_list.c;C:\code\lencode\Project2\Project2\Debug\sq_list.obj
diff --git a/Project2/Debug/Project2.tlog/Project2.lastbuildstate b/Project2/Debug/Project2.tlog/Project2.lastbuildstate
deleted file mode 100644
index 9743022..0000000
--- a/Project2/Debug/Project2.tlog/Project2.lastbuildstate
+++ /dev/null
@@ -1,2 +0,0 @@
-PlatformToolSet=v143:VCToolArchitecture=Native32Bit:VCToolsVersion=14.43.34808:TargetPlatformVersion=10.0.26100.0:
-Debug|Win32|C:\code\lencode\Project2\|
diff --git a/Project2/Debug/Project2.tlog/link.command.1.tlog b/Project2/Debug/Project2.tlog/link.command.1.tlog
deleted file mode 100644
index 6848040..0000000
Binary files a/Project2/Debug/Project2.tlog/link.command.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.tlog/link.read.1.tlog b/Project2/Debug/Project2.tlog/link.read.1.tlog
deleted file mode 100644
index eeb33b4..0000000
Binary files a/Project2/Debug/Project2.tlog/link.read.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.tlog/link.secondary.1.tlog b/Project2/Debug/Project2.tlog/link.secondary.1.tlog
deleted file mode 100644
index 1eb78f1..0000000
--- a/Project2/Debug/Project2.tlog/link.secondary.1.tlog
+++ /dev/null
@@ -1,2 +0,0 @@
-^C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\ARRAY_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\MAIN.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\SQ_LIST.OBJ
-C:\code\lencode\Project2\Project2\Debug\Project2.ilk
diff --git a/Project2/Debug/Project2.tlog/link.write.1.tlog b/Project2/Debug/Project2.tlog/link.write.1.tlog
deleted file mode 100644
index 76fb829..0000000
Binary files a/Project2/Debug/Project2.tlog/link.write.1.tlog and /dev/null differ
diff --git a/Project2/Debug/Project2.vcxproj.FileListAbsolute.txt b/Project2/Debug/Project2.vcxproj.FileListAbsolute.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/README.md b/README.md
index e3e21b5..5f1f8b8 100644
--- a/README.md
+++ b/README.md
@@ -1,159 +1,212 @@
# 数据结构与算法练习代码
+
## 项目目标
- 通过用C语言构建本学期所有数据结构和算法,来练编码.
+
+本项目旨在通过C语言实现本学期所学的所有数据结构和算法,以提升编码能力、加深对数据结构和算法的理解,并培养良好的编程习惯。通过实际编写代码,我们将逐步构建一个完整的数据结构与算法库,为后续的软件开发和学习打下坚实的基础。
+
## 项目结构说明
- `main.c`由于测试和调用其它`.c`文件,其它算法源文件用其英文名命名,编写被调用文件时首先在`name.h`声名结构体和函数,然后在`name.c`实现功能.
-## 示例
-`linked_list.h`:
- ```c
-#ifndef LINKED_LIST_H
-#define LINKED_LIST_H
-#include
+项目采用模块化设计,每个数据结构或算法模块都独立封装,便于管理和复用。具体结构如下:
-// 链表节点结构体
-typedef struct Node {
- int data;
- struct Node* next;
-} Node;
+- **`main.c`**:主程序文件,用于测试和调用其他模块的功能。它是整个项目的入口,通过它来运行和验证各个数据结构和算法模块的实现。
+- **算法源文件**:每个数据结构或算法模块都有一个独立的源文件,文件名以其实现的功能的英文名命名,例如`linked_list.c`用于实现链表,`stack.c`用于实现栈等。这种命名方式直观且易于理解,方便开发者快速定位和使用相关模块。
+- **头文件**:每个模块的头文件(如`linked_list.h`、`stack.h`等)用于声明该模块中定义的结构体和函数。头文件是模块对外的接口,它定义了模块可以被外部调用的函数和数据结构,隐藏了具体的实现细节,符合模块化设计的原则,同时也便于其他模块的引用和调用。
-// 链表操作函数声明
-Node* create_node(int data);
-void append_node(Node** head, int data);
-bool delete_node(Node** head, int data);
-void print_list(const Node* head);
-void free_list(Node** head);
+在编写被调用模块的代码时,首先需要在对应的头文件中声明结构体和函数,明确模块的接口;然后在对应的源文件中实现这些函数的功能。这种分离接口和实现的方式,不仅有助于代码的组织和管理,还便于后续的维护和扩展。例如,如果需要修改某个模块的内部实现,只需修改其源文件,而无需修改头文件和主程序文件,只要保持接口不变,不会影响其他模块的正常调用。
-#endif // LINKED_LIST_H
+## 示例代码
-```
+以下是一个简单的示例,展示如何组织代码和实现功能。
-`linked_list.c`:
- ```c
-#include
-#include
-#include "linked_list.h"
+### 简化示例代码
-// 创建新节点
-Node* create_node(int data) {
- Node* new_node = (Node*)malloc(sizeof(Node));
- if (!new_node) {
- fprintf(stderr, "内存分配失败\n");
- exit(EXIT_FAILURE);
- }
- new_node->data = data;
- new_node->next = NULL;
- return new_node;
-}
+以下是一个简单的“Hello, World!”示例,展示如何在项目中添加一个新的模块。
-// 在链表末尾添加节点
-void append_node(Node** head, int data) {
- Node* new_node = create_node(data);
- if (*head == NULL) {
- *head = new_node;
- return;
- }
- Node* current = *head;
- while (current->next != NULL) {
- current = current->next;
- }
- current->next = new_node;
-}
+#### `hello_world.h`
-// 删除包含指定数据的节点
-bool delete_node(Node** head, int data) {
- if (*head == NULL) {
- return false;
- }
- Node* current = *head;
- Node* previous = NULL;
-
- // 查找要删除的节点
- while (current != NULL && current->data != data) {
- previous = current;
- current = current->next;
- }
-
- // 未找到节点
- if (current == NULL) {
- return false;
- }
-
- // 删除节点
- if (previous == NULL) {
- // 要删除的是头节点
- *head = current->next;
- } else {
- previous->next = current->next;
- }
- free(current);
- return true;
-}
-
-// 打印链表
-void print_list(const Node* head) {
- const Node* current = head;
- while (current != NULL) {
- printf("%d -> ", current->data);
- current = current->next;
- }
- printf("NULL\n");
-}
-
-// 释放链表内存
-void free_list(Node** head) {
- Node* current = *head;
- while (current != NULL) {
- Node* next = current->next;
- free(current);
- current = next;
- }
- *head = NULL;
-}
-
-```
-`main.c`:
```c
+#ifndef HELLO_WORLD_H
+#define HELLO_WORLD_H
+
+void print_hello_world();
+
+#endif // HELLO_WORLD_H
+```
+
+#### `hello_world.c`
+
+```c
+#include "hello_world.h"
#include
-#include "linked_list.h"
+
+void print_hello_world() {
+ printf("Hello, World!\n");
+}
+```
+
+#### 在`main.c`中调用
+
+```c
+#include "hello_world.h"
int main() {
- Node* head = NULL;
- insertNode(&head, 10);
- insertNode(&head, 20);
- insertNode(&head, 30);
- printList(head);
- deleteNode(&head, 20);
- printList(head);
+ print_hello_world();
return 0;
}
```
-## 补充
-### 知识补充
-```c
-#ifndef LINKED_LIST_H
-#define LINKED_LIST_H
+## 知识补充
+### 头文件保护符
-#endif
-```
-在 C 语言中,头文件(`.h` 文件)通常使用预处理指令 `#ifndef`、`#define` 和 `#endif` 来防止头文件被重复包含,避免编译错误。这种技术被称为 **头文件保护符** 或 **包含卫士**。
+在C语言中,头文件(`.h`文件)通常使用预处理指令`#ifndef`、`#define`和`#endif`来防止头文件被重复包含,避免编译错误。这种技术被称为**头文件保护符**或**包含卫士**。
**工作原理:**
-1. **`#ifndef LINKED_LIST_H`**:检查宏 `LINKED_LIST_H` 是否未被定义。
-2. **`#define LINKED_LIST_H`**:如果未定义,则定义宏 `LINKED_LIST_H`。
+1. **`#ifndef LINKED_LIST_H`**:检查宏`LINKED_LIST_H`是否未被定义。
+2. **`#define LINKED_LIST_H`**:如果未定义,则定义宏`LINKED_LIST_H`。
3. **头文件内容**:包含实际的类型定义和函数声明。
4. **`#endif`**:结束条件编译。
-当头文件第一次被包含时,`LINKED_LIST_H` 未被定义,编译器会处理头文件内容并定义 `LINKED_LIST_H`。如果头文件再次被包含,由于 `LINKED_LIST_H` 已被定义,`#ifndef` 条件不成立,编译器会跳过头文件内容,从而避免重复定义。
+当头文件第一次被包含时,`LINKED_LIST_H`未被定义,编译器会处理头文件内容并定义`LINKED_LIST_H`。如果头文件再次被包含,由于`LINKED_LIST_H`已被定义,`#ifndef`条件不成立,编译器会跳过头文件内容,从而避免重复定义。
**注意事项:**
- **宏名的唯一性**:确保每个头文件的宏名唯一,通常使用头文件名的大写形式,并用下划线替代非字母数字字符。
-- **`#pragma once` 指令**:一些编译器支持 `#pragma once`,它可以防止头文件被多次包含,使用起来更简洁,但并非所有编译器都支持,使用 `#ifndef` 等预处理指令具有更好的可移植性。
+- **`#pragma once`指令**:一些编译器支持`#pragma once`,它可以防止头文件被多次包含,使用起来更简洁,但并非所有编译器都支持,使用`#ifndef`等预处理指令具有更好的可移植性。
-通过使用头文件保护符,可以有效防止头文件的重复包含,确保代码的正确编译。
-### 规则补充
-统一使用`小写下划线命名法`
+通过使用头文件保护符,可以有效防止头文件的重复包含,确保代码的正确编译。
+
+### 命名规范
+
+为了保持代码的一致性和可读性,本项目统一使用**小写下划线命名法**。例如,变量名和函数名使用`variable_name`和`function_name`的形式,宏定义使用`MACRO_NAME`的形式。
+
+## 如何参与项目
+
+### 1. 克隆项目
+
+#### 方式1:直接克隆
+
+```bash
+git clone https://github.com/jdhnsu/C_DS_Algo.git
+cd C_DS_Algo
+```
+
+#### 方式2(推荐):
+
+使用`GitHub Desktop`客户端克隆项目,并在本地创建分支。
+
+- [GitHub Desktop 下载](https://desktop.github.com/download/)
+- [GitHub Desktop 汉化工具](https://github.com/robotze/GithubDesktopZhTool) (记得给项目点个赞!)
+
+### 2. 添加新的数据结构或算法模块
+
+- 工具说明:默认使用 *Visual Studio 2022 IDE*,克隆项目后,点击项目文件`Project2.sln`就可以开始编辑了。如果使用其它方式推荐使用`CMake`工具编译项目。
+- 创建一个新的头文件(如`new_module.h`)和源文件(如`new_module.c`)。
+- 在头文件中声明结构体和函数接口。
+- 在源文件中实现这些函数的功能。
+- 在`main.c`中调用新模块的功能,验证其正确性。
+
+### 3. 提交代码
+
+- 提交你的代码到你的分支。
+- 创建一个Pull Request,详细描述你的更改和新增功能。
+
+### 4. 提出建议或问题
+
+如果你有任何建议或遇到问题,欢迎在[Issues](https://github.com/jdhnsu/C_DS_Algo/issues)中提出,[wiki](https://github.com/jdhnsu/C_DS_Algo/wiki) 中也有许多代码讲解可以查看.
+
+## 示例:添加一个新的模块
+
+假设我们要添加一个栈模块,以下是步骤:
+
+1. **创建头文件`stack.h`**
+
+ ```c
+ #ifndef STACK_H
+ #define STACK_H
+
+ typedef struct Stack {
+ int* elements;
+ int capacity;
+ int top;
+ } Stack;
+
+ Stack* create_stack(int capacity);
+ void push(Stack* stack, int element);
+ int pop(Stack* stack);
+ bool is_empty(Stack* stack);
+ void free_stack(Stack* stack);
+
+ #endif // STACK_H
+ ```
+2. **创建源文件`stack.c`**
+
+ ```c
+ #include "stack.h"
+ #include
+ #include
+
+ Stack* create_stack(int capacity) {
+ Stack* stack = (Stack*)malloc(sizeof(Stack));
+ stack->elements = (int*)malloc(capacity * sizeof(int));
+ stack->capacity = capacity;
+ stack->top = -1;
+ return stack;
+ }
+
+ void push(Stack* stack, int element) {
+ if (stack->top == stack->capacity - 1) {
+ fprintf(stderr, "栈满,无法压入元素\n");
+ return;
+ }
+ stack->elements[++stack->top] = element;
+ }
+
+ int pop(Stack* stack) {
+ if (stack->top == -1) {
+ fprintf(stderr, "栈空,无法弹出元素\n");
+ return -1;
+ }
+ return stack->elements[stack->top--];
+ }
+
+ bool is_empty(Stack* stack) {
+ return stack->top == -1;
+ }
+
+ void free_stack(Stack* stack) {
+ free(stack->elements);
+ free(stack);
+ }
+ ```
+3. **在`main.c`中调用栈模块**
+
+ ```c
+ #include "stack.h"
+
+ int main() {
+ Stack* stack = create_stack(10);
+ push(stack, 10);
+ push(stack, 20);
+ printf("栈顶元素:%d\n", pop(stack));
+ free_stack(stack);
+ return 0;
+ }
+ ```
+4. **提交代码**
+
+ - 提交你的`stack.h`和`stack.c`文件。
+ - 在`main.c`中验证栈模块的功能。(无需提交`main.c`文件)
+ - 提交时记得详细说明你的更改。
+
+## 文件结构
+
+```
+C_DS_Algo/
+├── (一些日志文件和git配置(忽略))
+├── README.md
+├── linked_list.h
+├── linked_list.c
+└── main.c
+```
diff --git a/array_queue.c b/array_queue.c
new file mode 100644
index 0000000..fcc66e6
--- /dev/null
+++ b/array_queue.c
@@ -0,0 +1,89 @@
+#include
+#include
+#include "array_queue.h"
+
+/*@-------------
+ # 设计思路更新(原版漏洞太多):
+ 设计圆环式队列,当rear == size时,循环到数组的[0]位,当然这样的队列仍然遵循`先进先出原则`
+ ## 实现思路:
+ 通过取模运算,使得rear和front都在数组的[0,size-1]范围内,这样就能实现循环队列的功能.
+ # 有点解析:
+ 在传统的队列中,当队尾到达队列的末尾时,即使队列前面还有空闲空间,也无法继续入队,导致空间浪费。而环形队列通过取模运算,\n
+ 使得队尾指针可以在到达队列末尾后,从头开始继续使用队列的空闲空间,从而提高了空间的利用率。
+@-------------*/
+
+
+// 初始化队列
+array_queue *init_array_queue()
+{
+ array_queue *q = (array_queue *)malloc(sizeof(array_queue));
+ if (q == NULL)
+ {printf("error: malloc failed[From init_queue]"); return NULL;}
+ memset(q,0,sizeof(array_queue));
+ q->front = 0;
+ q->rear = 0;
+ q->size = 0;
+ return q;
+}
+
+// 判空
+int empty_array_queue(array_queue *q)
+{
+ if (q->size == 0)
+ return 1;
+ else
+ return 0;
+}
+
+// 判断满
+int full_array_queue(array_queue *q)
+{
+ if (q->size == MAX_QUEUE)
+ return 1;
+ else
+ return 0;
+}
+
+// 入队
+void push_array_queue(array_queue *q, elem_type value)
+{
+ if (full_array_queue(q))
+ {printf("error: queue is full[From push_queue]"); return;}
+ q->data[q->rear] = value;
+ q->rear = (q->front + q->size + 1) % MAX_QUEUE;
+ q->size++;
+}
+
+// 出队
+elem_type pop_array_queue(array_queue *q)
+{
+ if (empty_array_queue(q))
+ {printf("error: queue is empty[From pop_queue]"); return -1;}
+ elem_type value = q->data[q->front];
+ q->front = (q->front + 1) % MAX_QUEUE;
+ q->size--;
+ return value;
+}
+
+// 打印队列
+void print_array_queue(array_queue *q)
+{
+ if (empty_array_queue(q))
+ {
+ printf("error :\n");
+ return ;
+ }
+ int j = q->front;
+ for (int i=0;isize;i++)
+ {
+ printf("[");
+ printf("%d ",q->data[j%MAX_QUEUE]);
+ printf("]\n");
+ j++;
+ }
+}
+
+
+// void return def => fuck shit
+// if for while malloc def sleep pause NULL
+// #include $time_noon
\ No newline at end of file
diff --git a/array_queue.h b/array_queue.h
new file mode 100644
index 0000000..4f05aa0
--- /dev/null
+++ b/array_queue.h
@@ -0,0 +1,34 @@
+#ifndef ARRAY_QUEUE_H
+#define ARRAY_QUEUE_H
+#define MAX_QUEUE 3
+#define elem_type int
+// queue 构建
+typedef struct array_queue
+{
+ elem_type data[MAX_QUEUE];
+ int front; // 队列头指针
+ int rear; // 队列尾指针
+ int size;
+}array_queue;
+
+// queue 初始化
+array_queue *inti_array_queue();
+
+// 判空
+int empty_array_queue(array_queue *q);
+
+//判满
+int full_array_queue(array_queue *q);
+
+// 入队
+void push_array_queue(array_queue *q, elem_type value);
+
+// 出队
+elem_type pop_array_queue(array_queue *q);
+
+// 打印队列
+void print_array_queue(array_queue *q);
+
+
+
+#endif
\ No newline at end of file
diff --git a/array_stack.c b/array_stack.c
index 72e00ce..058a9a9 100644
--- a/array_stack.c
+++ b/array_stack.c
@@ -2,12 +2,11 @@
#include
#include "array_stack.h"
-// ʼջ
+// 初始化数组栈
array_stack* init_array_stack(void)
{
array_stack* new_stack = (array_stack*)malloc(sizeof(array_stack));
- if (new_stack == NULL) {
- printf("ڴʧ[init_array_stack()]\n");
+ if (new_stack == NULL) { printf("error: malloc() failed [From: init_array_stack()]\n");
return NULL;
}
memset(new_stack, 0, sizeof(array_stack));
@@ -15,24 +14,24 @@ array_stack* init_array_stack(void)
return new_stack;
}
-// ջ
+// 入栈
void push_array_stack(array_stack* stack, elem_type value)
{
if (stack->top + 1 > MAX_S)
{
- printf("ջ[push_array_stack()]\n");
+ printf("Stack overflow [From: push_array_stack()]\n");
return;
}
stack->top++;
stack->data[stack->top] = value;
}
-// ջ
+// 出栈
int pop_array_stack(array_stack* stack)
{
if (stack->top == -1)
{
- printf("ջΪ[pop_array_stack()]\n");
+ printf("Stack underflow [From: pop_array_stack()]\n");
return -1;
}
int value = stack->data[stack->top];
@@ -40,15 +39,15 @@ int pop_array_stack(array_stack* stack)
return value;
}
-// ӡջ
+// 打印
void print_array_stack(array_stack* stack)
{
if (stack->top == -1)
{
- printf("ջΪ[print_array_stack()]\n");
+ printf("viod stack [From: print_array_stack()]\n");
return;
}
- printf("ջԪ: %d\n", stack->data[stack->top]);
+ printf("top: %d\n", stack->data[stack->top]);
printf("-----\n");
for (int i = stack->top; i >= 0; i--)
{
diff --git a/linked_list.c b/linked_list.c
index 2a1b631..ef7be3f 100644
--- a/linked_list.c
+++ b/linked_list.c
@@ -6,6 +6,11 @@
node* init_node(elem_type value)
{
node* new_node = (node*)malloc(sizeof(node));
+ if (new_node == NULL)
+ {
+ printf("error: malloc failed\n");
+ return NULL;
+ }
new_node->value = value;
new_node->next = NULL;
return new_node;
@@ -28,10 +33,9 @@ void replace_node(node* n, elem_type value)
}
// ڵ
-void insert_node(node* head, elem_type value)
+void insert_node(node* head,node *new_node)
{
- node* new_node = init_node(value);
- new_node->next = head->next->next;
+ new_node->next = head->next;
head->next = new_node;
}
@@ -49,14 +53,16 @@ address_node *find_node(node* head, elem_type value)
printf("ڴʧ\n");
return NULL;
}
- n->n = 0;
- n->p = head;
- while (head != NULL)
+ n->n = 1;
+ n->p = head->next;
+ node *temp = head;
+ while (temp->next != NULL)
{
- if (head->value == value)
+ if (temp->value == value)
return n;
n->n += 1;
- head = head->next;
+ temp = temp->next;
+ n->p = temp;
}
printf("ҵڵ\n");
free(n);
@@ -66,9 +72,42 @@ address_node *find_node(node* head, elem_type value)
// ӡ
void print_node_list(node* head)
{
- while (head->next != NULL)
+ node* head_1 = head;
+ printf("[%d ",head_1->value);
+ while (head_1->next != NULL)
{
- head = head->next;
- printf("%d ", head->value);
+ head_1 = head_1->next;
+ printf("%d ", head_1->value);
}
+ printf("]\n");
+}
+
+//
+int get_node_list(node* head)
+{
+ node *head_1 = head;
+ int i = 1;
+ while (head_1->next != NULL)
+ {
+ head_1 = head_1->next;
+ i++;
+ }
+ printf("Ϊ%d\n", i);
+ return i;
+}
+
+
+// Ԫ
+elem_type get_node_value(node* head,int pos)
+{
+ node* head_1 = head;
+ int i = 1;
+ while (i < pos && head_1)
+ {
+ head_1 = head_1->next;
+ i++;
+ }
+ if (head_1 == NULL || i != pos)
+ return;
+ return head_1->value;
}
\ No newline at end of file
diff --git a/linked_list.h b/linked_list.h
index d095e8a..15eb8c7 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -11,7 +11,7 @@ typedef struct node
typedef struct address_node
{
int n;
- node* p;
+ elem_type * p;
}address_node;
@@ -25,7 +25,7 @@ void delete_node(node* n);
void replace_node (node* n, elem_type value);
// ڵ
-void insert_node(node* head, elem_type value);
+void insert_node(node* head,node *new_node);
//ʽڵ
int get_node(node* n);
@@ -36,5 +36,11 @@ address_node *find_node(node* head, elem_type value);
// ӡ
void print_node_list(node* head);
+// get length of linked list
+int get_node_list(node* head);
+
+//
+elem_type get_node_value(node* head,int pos);
+
#endif
diff --git a/main.c b/main.c
index c3fc1a8..e7359a5 100644
--- a/main.c
+++ b/main.c
@@ -1,13 +1,10 @@
#include
#include
#include
-#include "linked_list_stack.h"
int main(void)
{
-
-
//sq_list* L = init_sq_list();
//sq_list* N = init_sq_list();
//for (int i = 1; i <= 3; i++)
@@ -25,13 +22,82 @@ int main(void)
//mer_ge_sq_list(L,N);
//print_sq_list(L);
//printf("L->length:[%d]\n", L->length);
- stack_linked* head = init_stack_linked();
+ /*sq_list* L = init_sq_list();
+ sq_list* N = init_sq_list();
+ for (int i = 1; i <= 3; i++)
+ {
+ N->data[i - 1] = i+10;
+ }
+ for (int i = 1; i <= 10; i++)
+ {
+ L->data[i - 1] = i;
+ }
+ N->length = 3;
+ L->length = 10;
+ print_sq_list(N);
+ print_sq_list(L);
+ mer_ge_sq_list(L,N);
+ print_sq_list(L);
+ printf("L->length:[%d]\n", L->length);*/
+
+
+ /* array_stack* stack = init_array_stack();
+ push_array_stack(stack, 1);
+ push_array_stack(stack, 2);
+ push_array_stack(stack, 3);*/
+
+
+
+ /* array_queue *q = init_array_queue();
+ push_array_queue(q,0);
+ push_array_queue(q,1);
+ push_array_queue(q,2);
+ print_array_queue(q);
+ printf("---------------------\n");
+ pop_array_queue(q);
+ print_array_queue(q);
+ printf("----------\n");
+ push_array_queue(q, 666);
+ print_array_queue(q);*/
+
+// node* q1 = init_node(1);
+// node* q2 = init_node(2);
+// node* q3 = init_node(3);
+// node* q4 = init_node(4);
+// q1->next = q2;
+// q2->next = q3;
+// q3->next = q4;
+// //address_node *n = find_node(q1, 3);
+// // printf("%d\n %p\n", n->n,n->p
+// //int e = get_node_list(q1);
+// node* q2_3 = init_node(0);
+// insert_node(q2, q2_3);
+// print_node_list(q1);
+// delete_node(q2);
+// print_node_list(q1);
+
+/*
+stack_linked* head = init_stack_linked();
push_stack_linked(head, 1);
push_stack_linked(head, 2);
push_stack_linked(head, 3);
print_linked(head);
pop_stack_linked(head);
print_linked(head);
+*/
+
+ // queue_test();
+ array_queue *q = init_array_queue();
+ push_array_queue(q, 1);
+ push_array_queue(q, 2);
+ push_array_queue(q, 3);
+ print_array_queue(q);
+ printf("----------\n");
+ pop_array_queue(q);
+ print_array_queue(q);
+ printf("----------\n");
+ push_array_queue(q, 666);
+ print_array_queue(q);
printf("Hello World!\n");
system("pause");
return 0;
diff --git a/sq_list.c b/sq_list.c
index f698f29..3afdefd 100644
--- a/sq_list.c
+++ b/sq_list.c
@@ -6,6 +6,11 @@
sq_list* init_sq_list(void)
{
sq_list* name = (sq_list*)malloc(sizeof(sq_list));
+ if (name == NULL)
+ {
+ printf("error: malloc failed![From init_sq_list]\n");
+ return NULL;
+ }
memset(name, 0, sizeof(sq_list));
return name;
}