init
This commit is contained in:
commit
190a8eb653
63
.gitattributes
vendored
Normal file
63
.gitattributes
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
###############################################################################
|
||||
# Set default behavior to automatically normalize line endings.
|
||||
###############################################################################
|
||||
* text=auto
|
||||
|
||||
###############################################################################
|
||||
# Set default behavior for command prompt diff.
|
||||
#
|
||||
# This is need for earlier builds of msysgit that does not have it on by
|
||||
# default for csharp files.
|
||||
# Note: This is only used by command line
|
||||
###############################################################################
|
||||
#*.cs diff=csharp
|
||||
|
||||
###############################################################################
|
||||
# Set the merge driver for project and solution files
|
||||
#
|
||||
# Merging from the command prompt will add diff markers to the files if there
|
||||
# are conflicts (Merging from VS is not affected by the settings below, in VS
|
||||
# the diff markers are never inserted). Diff markers may cause the following
|
||||
# file extensions to fail to load in VS. An alternative would be to treat
|
||||
# these files as binary and thus will always conflict and require user
|
||||
# intervention with every merge. To do so, just uncomment the entries below
|
||||
###############################################################################
|
||||
#*.sln merge=binary
|
||||
#*.csproj merge=binary
|
||||
#*.vbproj merge=binary
|
||||
#*.vcxproj merge=binary
|
||||
#*.vcproj merge=binary
|
||||
#*.dbproj merge=binary
|
||||
#*.fsproj merge=binary
|
||||
#*.lsproj merge=binary
|
||||
#*.wixproj merge=binary
|
||||
#*.modelproj merge=binary
|
||||
#*.sqlproj merge=binary
|
||||
#*.wwaproj merge=binary
|
||||
|
||||
###############################################################################
|
||||
# behavior for image files
|
||||
#
|
||||
# image files are treated as binary by default.
|
||||
###############################################################################
|
||||
#*.jpg binary
|
||||
#*.png binary
|
||||
#*.gif binary
|
||||
|
||||
###############################################################################
|
||||
# diff behavior for common document formats
|
||||
#
|
||||
# Convert binary document formats to text before diffing them. This feature
|
||||
# is only available from the command line. Turn it on by uncommenting the
|
||||
# entries below.
|
||||
###############################################################################
|
||||
#*.doc diff=astextplain
|
||||
#*.DOC diff=astextplain
|
||||
#*.docx diff=astextplain
|
||||
#*.DOCX diff=astextplain
|
||||
#*.dot diff=astextplain
|
||||
#*.DOT diff=astextplain
|
||||
#*.pdf diff=astextplain
|
||||
#*.PDF diff=astextplain
|
||||
#*.rtf diff=astextplain
|
||||
#*.RTF diff=astextplain
|
54
.gitignore
vendored
Normal file
54
.gitignore
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
/.vs/Project2
|
||||
/.vs
|
31
Project2.sln
Normal file
31
Project2.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.13.35828.75 d17.13
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Project2", "Project2.vcxproj", "{137CCEB6-E223-4830-A736-5BED572F5DC8}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Debug|x64.Build.0 = Debug|x64
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Debug|x86.Build.0 = Debug|Win32
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Release|x64.ActiveCfg = Release|x64
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Release|x64.Build.0 = Release|x64
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Release|x86.ActiveCfg = Release|Win32
|
||||
{137CCEB6-E223-4830-A736-5BED572F5DC8}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {AB6C27C7-C88E-477B-B238-D2A28049AF3C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
212
README.md
Normal file
212
README.md
Normal file
@ -0,0 +1,212 @@
|
||||
# 数据结构与算法练习代码
|
||||
|
||||
## 项目目标
|
||||
|
||||
本项目旨在通过C语言实现本学期所学的所有数据结构和算法,以提升编码能力、加深对数据结构和算法的理解,并培养良好的编程习惯。通过实际编写代码,我们将逐步构建一个完整的数据结构与算法库,为后续的软件开发和学习打下坚实的基础。
|
||||
|
||||
## 项目结构说明
|
||||
|
||||
项目采用模块化设计,每个数据结构或算法模块都独立封装,便于管理和复用。具体结构如下:
|
||||
|
||||
- **`main.c`**:主程序文件,用于测试和调用其他模块的功能。它是整个项目的入口,通过它来运行和验证各个数据结构和算法模块的实现。
|
||||
- **算法源文件**:每个数据结构或算法模块都有一个独立的源文件,文件名以其实现的功能的英文名命名,例如`linked_list.c`用于实现链表,`stack.c`用于实现栈等。这种命名方式直观且易于理解,方便开发者快速定位和使用相关模块。
|
||||
- **头文件**:每个模块的头文件(如`linked_list.h`、`stack.h`等)用于声明该模块中定义的结构体和函数。头文件是模块对外的接口,它定义了模块可以被外部调用的函数和数据结构,隐藏了具体的实现细节,符合模块化设计的原则,同时也便于其他模块的引用和调用。
|
||||
|
||||
在编写被调用模块的代码时,首先需要在对应的头文件中声明结构体和函数,明确模块的接口;然后在对应的源文件中实现这些函数的功能。这种分离接口和实现的方式,不仅有助于代码的组织和管理,还便于后续的维护和扩展。例如,如果需要修改某个模块的内部实现,只需修改其源文件,而无需修改头文件和主程序文件,只要保持接口不变,不会影响其他模块的正常调用。
|
||||
|
||||
## 示例代码
|
||||
|
||||
以下是一个简单的示例,展示如何组织代码和实现功能。
|
||||
|
||||
### 简化示例代码
|
||||
|
||||
以下是一个简单的“Hello, World!”示例,展示如何在项目中添加一个新的模块。
|
||||
|
||||
#### `hello_world.h`
|
||||
|
||||
```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 <stdio.h>
|
||||
|
||||
void print_hello_world() {
|
||||
printf("Hello, World!\n");
|
||||
}
|
||||
```
|
||||
|
||||
#### 在`main.c`中调用
|
||||
|
||||
```c
|
||||
#include "hello_world.h"
|
||||
|
||||
int main() {
|
||||
print_hello_world();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 知识补充
|
||||
|
||||
### 头文件保护符
|
||||
|
||||
在C语言中,头文件(`.h`文件)通常使用预处理指令`#ifndef`、`#define`和`#endif`来防止头文件被重复包含,避免编译错误。这种技术被称为**头文件保护符**或**包含卫士**。
|
||||
|
||||
**工作原理:**
|
||||
|
||||
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`条件不成立,编译器会跳过头文件内容,从而避免重复定义。
|
||||
|
||||
**注意事项:**
|
||||
|
||||
- **宏名的唯一性**:确保每个头文件的宏名唯一,通常使用头文件名的大写形式,并用下划线替代非字母数字字符。
|
||||
- **`#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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
```
|
87
array_queue.c
Normal file
87
array_queue.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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 ;
|
||||
}
|
||||
for (int i=0;i<q->size;i++)
|
||||
{
|
||||
printf("[");
|
||||
printf("%d ",q->data[i%MAX_QUEUE]);
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// void return def => fuck shit
|
||||
// if for while malloc def sleep pause NULL
|
||||
// #include $time_noon
|
34
array_queue.h
Normal file
34
array_queue.h
Normal file
@ -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
|
57
array_stack.c
Normal file
57
array_stack.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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("error: malloc() failed [From: init_array_stack()]\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(new_stack, 0, sizeof(array_stack));
|
||||
new_stack->top = -1;
|
||||
return new_stack;
|
||||
}
|
||||
|
||||
// 入栈
|
||||
void push_array_stack(array_stack* stack, elem_type value)
|
||||
{
|
||||
if (stack->top + 1 > MAX_S)
|
||||
{
|
||||
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("Stack underflow [From: pop_array_stack()]\n");
|
||||
return -1;
|
||||
}
|
||||
int value = stack->data[stack->top];
|
||||
stack->top--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// 打印
|
||||
void print_array_stack(array_stack* stack)
|
||||
{
|
||||
if (stack->top == -1)
|
||||
{
|
||||
printf("viod stack [From: print_array_stack()]\n");
|
||||
return;
|
||||
}
|
||||
printf("top: %d\n", stack->data[stack->top]);
|
||||
printf("-----\n");
|
||||
for (int i = stack->top; i >= 0; i--)
|
||||
{
|
||||
printf("| %d |\n", stack->data[i]);
|
||||
}
|
||||
printf("-----\n");
|
||||
}
|
22
array_stack.h
Normal file
22
array_stack.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ARRAY_SATCK
|
||||
#define ARRAY_SATCK
|
||||
#define MAX_S 100
|
||||
#define elem_type int
|
||||
typedef struct array_stack
|
||||
{
|
||||
elem_type data[MAX_S];
|
||||
int top;
|
||||
}array_stack;
|
||||
// 初始栈
|
||||
array_stack* init_array_stack(void);
|
||||
|
||||
// 入栈
|
||||
void push_array_stack(array_stack* s, elem_type value);
|
||||
|
||||
// 出栈
|
||||
int pop_array_stack(array_stack* s);
|
||||
|
||||
// 打印数组栈
|
||||
void print_array_stack(array_stack* s);
|
||||
|
||||
#endif
|
79
linked_list.c
Normal file
79
linked_list.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "linked_list.h"
|
||||
|
||||
// 初始赋值
|
||||
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;
|
||||
}
|
||||
|
||||
// 删除下一个节点
|
||||
void delete_node(node* n)
|
||||
{
|
||||
if (n->next != NULL) {
|
||||
node* temp = n->next;
|
||||
n->next = n->next->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
// 替换节点值
|
||||
void replace_node(node* n, elem_type value)
|
||||
{
|
||||
n->value = value;
|
||||
}
|
||||
|
||||
// 插入节点
|
||||
void insert_node(node* head, elem_type value)
|
||||
{
|
||||
node* new_node = init_node(value);
|
||||
new_node->next = head->next->next;
|
||||
head->next = new_node;
|
||||
}
|
||||
|
||||
// 访问节点
|
||||
int get_node(node* n)
|
||||
{
|
||||
return n->value;
|
||||
}
|
||||
|
||||
// 查找
|
||||
address_node *find_node(node* head, elem_type value)
|
||||
{
|
||||
address_node* n = (address_node*)malloc(sizeof(address_node));
|
||||
if (n == NULL) {
|
||||
printf("内存分配失败\n");
|
||||
return NULL;
|
||||
}
|
||||
n->n = 0;
|
||||
n->p = head;
|
||||
while (head != NULL)
|
||||
{
|
||||
if (head->value == value)
|
||||
return n;
|
||||
n->n += 1;
|
||||
head = head->next;
|
||||
}
|
||||
printf("无法找到节点\n");
|
||||
free(n);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 打印链表
|
||||
void print_node_list(node* head)
|
||||
{
|
||||
while (head->next != NULL)
|
||||
{
|
||||
head = head->next;
|
||||
printf("%d ", head->value);
|
||||
}
|
||||
}
|
40
linked_list.h
Normal file
40
linked_list.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef LINKED_LIST_H
|
||||
#define LINKED_LIST_H
|
||||
#define elem_type int
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
elem_type value;
|
||||
struct node* next;
|
||||
} node;
|
||||
|
||||
typedef struct address_node
|
||||
{
|
||||
int n;
|
||||
node* p;
|
||||
}address_node;
|
||||
|
||||
|
||||
// 初始化节点
|
||||
node* init_node(elem_type value);
|
||||
|
||||
// 删除下一个节点
|
||||
void delete_node(node* n);
|
||||
|
||||
// 替换节点
|
||||
void replace_node (node* n, elem_type value);
|
||||
|
||||
// 插入节点
|
||||
void insert_node(node* head, elem_type value);
|
||||
|
||||
//访问节点
|
||||
int get_node(node* n);
|
||||
|
||||
// 查找
|
||||
address_node *find_node(node* head, elem_type value);
|
||||
|
||||
// 打印链表
|
||||
void print_node_list(node* head);
|
||||
|
||||
#endif
|
||||
|
44
linked_list_stack.c
Normal file
44
linked_list_stack.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "linked_list_stack.h"
|
||||
|
||||
// 初始栈
|
||||
stack_linked* init_stack_linked(void)
|
||||
{
|
||||
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
|
||||
if (s == NULL)
|
||||
{
|
||||
printf("内存分配失败!\n");
|
||||
return NULL;
|
||||
}
|
||||
s->top = NULL;
|
||||
s->size = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
// 入栈
|
||||
void push_stack_linked(stack_linked* s, elem_type value)
|
||||
{
|
||||
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
|
||||
if (node == NULL)
|
||||
{
|
||||
printf("内存分配失败!\n");
|
||||
return NULL;
|
||||
}
|
||||
node->value = value;
|
||||
node->next = s->top;
|
||||
s->top = node;
|
||||
s->size++;
|
||||
}
|
||||
|
||||
// 出栈
|
||||
int pop_stack_linked(stack_linked* s)
|
||||
{
|
||||
int flog = 0;
|
||||
flog = s->top->value;
|
||||
stack_node* tmp = s->top;
|
||||
s->top = s->top->next;
|
||||
free(tmp);
|
||||
tmp = NULL;
|
||||
return flog;
|
||||
}
|
44
linked_list_stack.h
Normal file
44
linked_list_stack.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef LINKED_LIST_STACK_H
|
||||
#define LINKED_LIST_STACK_H
|
||||
#define elem_type int
|
||||
|
||||
typedef struct stack_node
|
||||
{
|
||||
elem_type value;
|
||||
struct stack_node *next;
|
||||
|
||||
}stack_node;
|
||||
|
||||
typedef struct stack_linked
|
||||
{
|
||||
stack_node *top;
|
||||
int size;
|
||||
}stack_linked;
|
||||
|
||||
// 初始栈
|
||||
stack_linked* init_stack_linked(void);
|
||||
|
||||
// 入栈
|
||||
void push_stack_linked(stack_linked* s, elem_type value);
|
||||
|
||||
// 出栈
|
||||
int pop_stack_linked(stack_linked* s);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
53
main.c
Normal file
53
main.c
Normal file
@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "array_stack.h"
|
||||
#include "sq_list.h"
|
||||
#include "array_queue.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
|
||||
|
||||
/*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);
|
||||
printf("Hello World!\n");
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
176
sq_list.c
Normal file
176
sq_list.c
Normal file
@ -0,0 +1,176 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "sq_list.h"
|
||||
|
||||
sq_list* init_sq_list(void)
|
||||
{
|
||||
sq_list* name = (sq_list*)malloc(sizeof(sq_list));
|
||||
memset(name, 0, sizeof(sq_list));
|
||||
return name;
|
||||
}
|
||||
|
||||
// 删除
|
||||
void delete_sq_list(sq_list* list,int pos)
|
||||
{
|
||||
if (pos < 0 || pos > list->length || list->length == MAX)
|
||||
{
|
||||
printf("位置错误\n");
|
||||
return;
|
||||
}
|
||||
if (pos == list->length - 1)
|
||||
list->data[pos] = 0;
|
||||
else
|
||||
{
|
||||
for (int i = pos; i < list->length - 1; i++)
|
||||
list->data[i] = list->data[i + 1];
|
||||
list->data[list->length - 1] = 0;
|
||||
}
|
||||
list->length--;
|
||||
|
||||
}
|
||||
|
||||
// 替换
|
||||
void replace_sq_list(sq_list* list, int pos, elem_type value)
|
||||
{
|
||||
if (pos < 0 || pos > list->length || list->length >= MAX)
|
||||
{
|
||||
printf("位置错误\n");
|
||||
return;
|
||||
}
|
||||
if (pos > list->length && pos <= MAX)
|
||||
printf("不建议在此处插入");
|
||||
list->data[pos] = value;
|
||||
}
|
||||
|
||||
|
||||
// 插入
|
||||
void insert_sq_list(sq_list* list, int pos, elem_type value)
|
||||
{
|
||||
if (pos < 0 || pos > list->length || list->length >= MAX)
|
||||
{
|
||||
printf("位置错误\n");
|
||||
return;
|
||||
}
|
||||
if (pos == MAX)
|
||||
printf("有损插入\n");
|
||||
if (pos > list->length && pos <= MAX)
|
||||
printf("不建议在此处插入");
|
||||
int i = list->length -1;
|
||||
int flog = i;
|
||||
for (i; i >= pos-1; i--)
|
||||
{
|
||||
list->data[i + 1] = list->data[i];
|
||||
}
|
||||
list->data[pos] = value;
|
||||
/* if (flog + 1 > list->length)*/
|
||||
list->length++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 打印
|
||||
void print_sq_list(sq_list* list)
|
||||
{
|
||||
printf("[");
|
||||
for (int i = 0; i < list->length; i++)
|
||||
printf("%d ", list->data[i]);
|
||||
printf("]");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
//获得元素
|
||||
void get_sq_list(sq_list* list1, int pos, elem_type*e)
|
||||
{
|
||||
if (list1->length == 0 || pos<0 || pos>list1->length)
|
||||
{
|
||||
printf("获取元素失败");
|
||||
return;
|
||||
}
|
||||
*e = list1->data[pos];
|
||||
return;
|
||||
}
|
||||
|
||||
//查找
|
||||
int locate_list(sq_list* list, elem_type e)
|
||||
{
|
||||
int i = 0;
|
||||
if (list->length == 0)
|
||||
{
|
||||
printf("表长为空");
|
||||
return;
|
||||
}
|
||||
for (i = 0;i < list->length;i++)
|
||||
{
|
||||
if (list->data[i] == e)
|
||||
return 1;
|
||||
}
|
||||
if (i > list->length)
|
||||
{
|
||||
printf("越界错误");
|
||||
return;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 2 到 1 合并
|
||||
void merge_sq_list(sq_list* list_1, sq_list* list_2)
|
||||
{
|
||||
if (list_1->length == 0 || list_2->length == 0)
|
||||
{
|
||||
printf("至少有一个空表\n");
|
||||
return;
|
||||
}
|
||||
if ((list_1->length + list_2->length) > MAX)
|
||||
printf("总和超范围\n但是会继续合并只不过可能为有损合并\n");
|
||||
int flog = 0;
|
||||
for (int i = 0; i < list_2->length; i++)
|
||||
{
|
||||
|
||||
for (int j = 0; j < list_1->length; j++)
|
||||
{
|
||||
if (list_1->data[j] != list_2->data[i])
|
||||
{
|
||||
if (j == list_1->length -1)
|
||||
{
|
||||
list_1->data[list_1->length] = list_2->data[i];
|
||||
list_1->length++;
|
||||
flog++;
|
||||
}
|
||||
else
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
printf("----------------------------------\n");
|
||||
printf("有%d不相同 有%d相同\n", flog, list_2->length - flog);
|
||||
printf("----------------------------------\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
//合并2.0
|
||||
void mer_ge_sq_list(sq_list* list_1, sq_list* list_2)
|
||||
{
|
||||
int e;
|
||||
int k = 0;
|
||||
int flog = 0;
|
||||
for (int k=0;k < list_2->length;k++)
|
||||
{
|
||||
get_sq_list(list_2, k, &e);
|
||||
if (!locate_list(list_1, list_2))
|
||||
{
|
||||
insert_sq_list(list_1, list_1->length, e);
|
||||
flog++;
|
||||
}
|
||||
}
|
||||
printf("----------------------------------\n");
|
||||
printf("有%d不相同 有%d相同\n", flog, list_2->length - flog);
|
||||
printf("----------------------------------\n");
|
||||
}
|
||||
|
40
sq_list.h
Normal file
40
sq_list.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef SQ_LIST_H
|
||||
#define SQ_LIST_H
|
||||
#define MAX 100
|
||||
#define elem_type int
|
||||
|
||||
|
||||
typedef struct sq_list
|
||||
{
|
||||
elem_type data[MAX];
|
||||
int length;
|
||||
} sq_list;
|
||||
|
||||
// ³õʼ»¯
|
||||
sq_list *init_sq_list(void);
|
||||
|
||||
// ɾ³ý
|
||||
void delete_sq_list(sq_list* list, int pos);
|
||||
|
||||
// Ìæ»»
|
||||
void replace_sq_list(sq_list* list, int pos, elem_type value);
|
||||
|
||||
// ²åÈë
|
||||
void insert_sq_list(sq_list* list, int pos, elem_type value);
|
||||
|
||||
// ´òÓ¡
|
||||
void print_sq_list(sq_list* list);
|
||||
|
||||
//»ñµÃÔªËØ
|
||||
void get_sq_list(sq_list* list1,int pos, elem_type *e);
|
||||
|
||||
//²éÕÒ
|
||||
int locate_list(sq_list* list, elem_type e);
|
||||
|
||||
// ºÏ²¢Ë³Ðò±í1.0
|
||||
void merge_sq_list(sq_list* list_1, sq_list* list_2);
|
||||
|
||||
//ºÏ²¢Ë³Ðò±í2.0
|
||||
void mer_ge_sq_list(sq_list* list_1, sq_list* list_2);
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user