一、数组 (Array) 实现
1. 基础数组
#include <iostream>
using namespace std;
int main() {
// 静态数组
int staticArr[5] = {1,2,3,4,5};
// 动态数组
int size = 5;
int* dynamicArr = new int[size];
// 访问元素
cout << "Third element: " << dynamicArr[2] << endl;
delete[] dynamicArr; // 释放内存
return 0;
}
2. 动态扩容数组(类似vector)
class DynamicArray {
private:
int* arr;
int capacity;
int length;
public:
DynamicArray() : capacity(2), length(0) {
arr = new int[capacity];
}
void push_back(int val) {
if(length == capacity) {
// 扩容策略
capacity *= 2;
int* newArr = new int[capacity];
for(int i=0; i<length; i++) {
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
}
arr[length++] = val;
}
int at(int index) {
if(index >= length) throw out_of_range("Index out of range");
return arr[index];
}
~DynamicArray() {
delete[] arr;
}
};
二、链表 (Linked List)
1. 单链表实现
class ListNode {
public:
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class LinkedList {
private:
ListNode* head;
public:
LinkedList() : head(nullptr) {}
// 添加节点到头部
void addFront(int val) {
ListNode* newNode = new ListNode(val);
newNode->next = head;
head = newNode;
}
// 删除节点
void deleteNode(int val) {
ListNode* prev = nullptr;
ListNode* curr = head;
while(curr) {
if(curr->val == val) {
if(prev) {
prev->next = curr->next;
} else {
head = curr->next;
}
delete curr;
return;
}
prev = curr;
curr = curr->next;
}
}
// 遍历打印
void print() {
ListNode* curr = head;
while(curr) {
cout << curr->val << " -> ";
curr = curr->next;
}
cout << "null" << endl;
}
};
2. 双链表实现
class DListNode {
public:
int val;
DListNode *prev, *next;
DListNode(int x) : val(x), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
private:
DListNode* head;
DListNode* tail;
public:
// 添加节点到尾部
void addBack(int val) {
DListNode* newNode = new DListNode(val);
if(!head) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
// 其他操作类似单链表,需要处理prev指针
};
三、树 (Tree)
1. 二叉树实现
class TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class BinaryTree {
private:
TreeNode* root;
// 递归插入
TreeNode* insert(TreeNode* node, int val) {
if(!node) return new TreeNode(val);
if(val < node->val) {
node->left = insert(node->left, val);
} else {
node->right = insert(node->right, val);
}
return node;
}
public:
BinaryTree() : root(nullptr) {}
void insert(int val) {
root = insert(root, val);
}
// 前序遍历
void preOrder(TreeNode* node) {
if(node) {
cout << node->val << " ";
preOrder(node->left);
preOrder(node->right);
}
}
// 其他遍历方式类似
};
2. AVL树(平衡二叉搜索树)
class AVLNode {
public:
int val;
AVLNode *left, *right;
int height;
AVLNode(int x) : val(x), left(nullptr), right(nullptr), height(1) {}
};
class AVLTree {
private:
AVLNode* root;
int getHeight(AVLNode* node) {
return node ? node->height : 0;
}
// 右旋转
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// 其他旋转操作和平衡因子计算...
};
四、图 (Graph)
1. 邻接矩阵表示法
class GraphMatrix {
private:
int V; // 顶点数
vector<vector<int>> adj;
public:
GraphMatrix(int vertices) : V(vertices) {
adj = vector<vector<int>>(V, vector<int>(V, 0));
}
void addEdge(int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // 无向图
}
};
2. 邻接表表示法(更高效)
class GraphList {
private:
int V;
vector<list<int>> adj;
public:
GraphList(int vertices) : V(vertices), adj(vertices) {}
void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u); // 无向图
}
// BFS遍历
void BFS(int start) {
vector<bool> visited(V, false);
queue<int> q;
visited[start] = true;
q.push(start);
while(!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";
for(int v : adj[u]) {
if(!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
}
};
关键点解析:
-
内存管理:
- 使用
new/delete
管理堆内存 - 链表节点要及时释放内存
- 推荐使用智能指针(如
unique_ptr
)
- 使用
-
时间复杂度:
- 数组随机访问:O(1)
- 链表插入/删除:O(1)(已知位置)
- 二叉树平衡操作:O(log n)
-
设计模式:
- 使用类封装数据结构
- 分离节点类和容器类
- 实现迭代器模式遍历元素
-
扩展功能:
- 实现STL风格的接口(begin/end)
- 添加异常处理
- 支持模板泛型编程
建议按照以下顺序练习:
- 先实现数组和链表
- 再实现二叉树及其遍历
- 最后实现图结构及算法(DFS/BFS)
- 尝试实现更复杂的结构(红黑树、哈希表)
注意:实际开发中应优先使用STL容器(vector、list、map等),这些实现主要用于学习原理。