"Data structures are the backbone of efficient algorithms." 这是一个记录我深入学习 C 语言数据结构 的代码仓库。从最基础的线性表开始,逐步构建起对底层内存管理和指针操作的深刻理解。 This repository documents my journey of mastering Data Structures in C. Starting from fundamental linear lists, I aim to build a deep understanding of low-level memory management and pointer manipulation.
目前已实现以下核心数据结构 (Current Implementations):
| 目录 (Directory) | 名称 (Name) | 特性 (Features) | 核心文件 (Core Files) |
|---|---|---|---|
SeqList |
动态顺序表 |
(Dynamic Sequential List) | - 支持动态扩容 (Auto-expansion)
- 连续物理内存
- 随机访问 | SeqList.c, SeqList.h |
| SList | 单链表
(Singly Linked List) | - 无头结点 (No Sentinel)
- 灵活内存分配
- 适合做其他结构的子结构 | SListNode.c, SListNode.h |
| List | 带头双向循环链表
(Doubly Circular Linked List) | - 哨兵位 (Sentinel Head)
- 双向循环 (Bidirectional & Cyclic)
- 任意位置插入/删除 | List.c, List.h |
所有涉及内存分配的操作(malloc, realloc)均包含空指针检查,且所有结构均实现了对应的销毁函数(Destroy),确保无内存泄漏。
All memory allocations include
NULLchecks, and every structure comes with a correspondingDestroyfunction to prevent memory leaks.
使用 typedef 定义数据类型(如 typedef int SLDataType),方便未来替换为其他数据类型,增强代码的可维护性。
Uses
typedefto abstract data types, making the code maintainable and adaptable for different data types.
在关键接口中使用 assert 断言,确保指针有效性及索引合法性,快速定位开发过程中的逻辑错误。
Extensive use of
assertto validate pointers and indices, ensuring robustness during development.
The elegance of initialization in a circular linked list:
// List.c
LTNode* LTInit() {
LTNode* phead = LTBuyNode(-1);
// 哨兵位指向自己,形成逻辑闭环
// Sentinel points to itself, forming a logical loop
phead->next = phead;
phead->prev = phead;
return phead;
}Auto-expansion strategy in Sequential List:
// SeqList.c
void SLCheckCapacity(SL* ps) {
if (ps->size == ps->capacity) {
// 2倍扩容策略 / 2x growth strategy
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
// ... (Error handling)
ps->arr = tmp;
ps->capacity = newCapacity;
}
}- 顺序表 (Sequential List)
- 单链表 (Singly Linked List)
- 带头双向循环链表 (Doubly Circular Linked List)
- 栈与队列 (Stack & Queue)
- 二叉树 (Binary Tree)
- 排序算法 (Sorting Algorithms)
本项目基于标准的 C11 编写,可以使用 gcc 或 Visual Studio 编译。
Using GCC:
# 编译双向链表测试 / Compile List test
gcc List/List.c List/test.c -o list_test
./list_test
Author: A passionate CS student exploring the world of AI & Systems. Gitee Mirror: https://gitee.com/lu-ma-flies-fast/data-structures-in-c