跳到正文

个人记录

JSCOI - 数据结构

oi_wiki约 18 分钟
原文
文章信息与版本

文章信息

收录
2026年7月17日
更新
2026年8月7日
篇幅
约 18 分钟

版本

  1. v2当前
  2. v1

一、链表

链表是一种非连续存储的数据结构,每个元素称为节点,包含数据和指向下一个节点的指针。

特性数组链表
存储方式连续存储非连续存储
查询O(1)O(1)O(n)O(n)
插入/删除O(n)O(n)O(1)O(1)

1. 单向链表

每个节点只存储指向下一个节点的指针。

数组模拟实现:

C++
int head, data[N], nxt[N], sz; // head 头指针, sz 链表大小

void Front(int val) { // 头插法
	data[++sz] = val;
	nxt[sz] = head;
	head = sz;
}

void dfs() { // 遍历
	for (int p = head; p; p = nxt[p])
		printf("%d ", data[p]);
}

void Insert(int pos, int val) { // 在 pos 后插入
	data[++sz] = val;
	nxt[sz] = nxt[pos];
	nxt[pos] = sz;
}

void Delete(int pos) { // 删除 pos 后的节点
	if (!pos || !nxt[pos]) return;
	nxt[pos] = nxt[nxt[pos]];
}

结构体实现:

C++
struct Node {
	int data;
	Node* next;
	Node(int val): data(val), next(nullptr) {}
};

Node* head = nullptr;

void Front(int val) {
	Node* newNode = new Node(val);
	newNode->next = head;
	head = newNode;
}

void Insert(Node* prev, int val) {
	if (!prev) return;
	Node* newNode = new Node(val);
	newNode->next = prev->next;
	prev->next = newNode;
}

void Delete(Node* prev) {
	if (!prev || !prev->next) return;
	Node* toDelete = prev->next;
	prev->next = toDelete->next;
	delete toDelete;
}

Node* find(int val) {
	for (Node* p = head; p; p = p->next)
		if (p->data == val) return p;
	return nullptr;
}

STL 实现(list):

C++
#include <bits/stdc++.h>
using namespace std;

list<int> lst;

void solve() {
	for (int val : lst) cout << val << " ";
	cout << endl;
}

int main() {
	lst.push_front(2);
	lst.push_front(7);
	lst.push_front(1);
	
	auto it = lst.begin();
	if (it != lst.end()) lst.insert(next(it), 9);
	
	auto pos = find(lst.begin(), lst.end(), 9);
	if (pos != lst.end() && next(pos) != lst.end())
		lst.erase(next(pos));
}

2. 双向链表

每个节点存储指向上一个和下一个节点的指针。

支持操作:

  • 头插法、尾插法
  • 在节点 xx 的左侧 / 右侧插入新节点
  • 删除节点 xx

STL 实现(list 本身就是双向链表):

C++
list<int> lst;
lst.push_back(10);   // 尾插
lst.push_front(20);  // 头插
auto it = lst.begin();
lst.insert(it, 30);  // 在 it 前插入
lst.erase(it);       // 删除 it 指向的元素

3. 循环链表

链表的尾节点指向头节点,形成环状结构。

支持操作:

  • 链表呈环状
  • 在节点 xx 前 / 后插入新节点
  • 删除节点 xx

二、栈

栈是一种后进先出 的数据结构。

核心操作

操作说明复杂度
push(x)将 xx 入栈O(1)O(1)
pop()弹出栈顶元素O(1)O(1)
top()获取栈顶元素O(1)O(1)
empty()判断栈是否为空O(1)O(1)
size()获取栈中元素个数O(1)O(1)

数组模拟实现

C++
int stk[N], top = 0; // top 指向栈顶元素位置

void push(int x) { stk[++top] = x; }
void pop() { top--; }
int topElement() { return stk[top]; }
bool empty() { return top == 0; }
int size() { return top; }

STL 实现(stack)

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	stack<int> stk;
	stk.push(10);
	stk.push(20);
	printf("%d\n", stk.top());  // 20
	stk.pop();
	printf("%d\n", stk.top());  // 10
	printf("%d\n", (int)stk.size()); // 1
	return 0;
}

经典应用

  • 括号匹配
  • 表达式求值
  • DFS(深度优先搜索)

三、队列(Queue)

队列是一种先进先出 的数据结构。

核心操作

操作说明复杂度
push(x)将 xx 入队O(1)O(1)
pop()弹出队首元素O(1)O(1)
front()获取队首元素O(1)O(1)
back()获取队尾元素O(1)O(1)
empty()判断队列是否为空O(1)O(1)
size()获取队列中元素个数O(1)O(1)

数组模拟实现

C++
int que[N], head = 1, tail = 0; // head 指向队首, tail 指向队尾

void push(int x) { que[++tail] = x; }
void pop() { head++; }
int frontElement() { return que[head]; }
bool empty() { return head > tail; }
int size() { return tail - head + 1; }

循环队列

使用数组模拟队列会导致一个问题:随着时间的推移,整个队列会向数组的尾部移动,一旦到达数组的最末端,即使数组的前端还有空闲位置,再进行入队操作也会导致溢出。

解决办法是采用循环的方式来组织存放队列元素的数组,即将数组下标为 00 的位置看做是最后一个位置的后继。

C++
int que[N], head = 1, tail = 0;

void push(int x) {
	tail = (tail + 1) % N;
	que[tail] = x;
}

int pop() {
	int x = que[head];
	head = (head + 1) % N;
	return x;
}

int frontElement() { return que[head]; }
int size() { return (tail - head + 1 + N) % N; }

STL 实现(queue)

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	queue<int> q;
	q.push(10);
	q.push(20);
	printf("%d\n", q.front()); // 10
	q.pop();
	printf("%d\n", q.front()); // 20
	printf("%d\n", (int)q.size()); // 1
	return 0;
}

经典应用

  • BFS(广度优先搜索)
  • 拓扑排序

注意:务必在访问 front() / top() 前判空!


四、单调栈与单调队列

1. 单调栈

栈内元素保持单调递增或单调递减。

算法模板(单调不递增):

C++
// 伪代码
Function Push(x):
while 栈非空 and x > 栈顶元素 do
	弹出栈顶元素
	将 x 压入栈中

应用场景:

  • 寻找下一个更大元素
  • 寻找上一个更大元素

2. 单调队列

队列内元素保持单调递增或单调递减。

算法模板(单调不递增):

C++
// 伪代码
	Function Enqueue(x):
	while 队列非空 and x > 队尾元素 do
		删除队尾元素
		将 x 加入队尾
		
		Function Dequeue():
		return 弹出的队首元素

应用场景:

  • 滑动窗口最大值 / 最小值
  • 求区间最值

五、优先队列(Priority Queue)与堆(Heap)

优先队列是一种特殊的队列,每次出队的元素是优先级最高(最大或最小)的元素。其底层实现通常是二叉堆。

核心操作

操作说明复杂度
push(x)插入元素 xxO(log⁡n)O(\log n)
pop()弹出堆顶元素O(log⁡n)O(\log n)
top()获取堆顶元素O(1)O(1)
empty()判断堆是否为空O(1)O(1)
size()获取堆中元素个数O(1)O(1)

STL 实现(priority_queue)

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
	// 大根堆(默认)
	priority_queue<int> pq;
	pq.push(30);
	pq.push(10);
	pq.push(50);
	printf("%d\n", pq.top()); // 50
	pq.pop();
	printf("%d\n", pq.top()); // 30
	printf("%d\n", (int)pq.size()); // 2
	
	// 小根堆
	priority_queue<int, vector<int>, greater<int>> hp;
	hp.push(30);
	hp.push(10);
	hp.push(50);
	printf("%d\n", hp.top()); // 10
	
	return 0;
}

六、常用 STL 容器与算法

1. 向量(vector)

本质:可动态扩容的数组,支持 O(1)O(1) 随机访问。

常用操作:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	vector<int> v;
	v.push_back(10);   // 尾部插入
	v.pop_back();      // 尾部删除
	int x = v[0];      // 下标访问
	int sz = v.size(); // 获取大小
	bool empty = v.empty(); // 判空
	v.clear();         // 清空
	
	// 遍历
	for (int i = 0; i < (int)v.size(); i++)
		cout << v[i] << " ";
	for (int x : v)
		cout << x << " ";
	for (auto it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
}

经典应用:

  • 数据个数动态变化时,首选 vector
  • 邻接表形式存储图:vector<int> G[N]

2. 集合(set)与映射(map)

set:存储不重复的关键字,自动有序

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	set<int> S;
	S.insert(5);
	S.insert(3);
	S.insert(7);
	S.insert(3); // 重复插入无效
	
	if (S.count(3)) cout << "3 exists" << endl; // count 返回 0 或 1
	
	auto it = S.find(5);
	if (it != S.end()) cout << *it << endl;
	
	S.erase(3); // 删除元素
}

map:存储键值对 (key, value),键不重复且自动有序

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	map<string, int> M;
	M["apple"] = 5;
	M["banana"] = 3;
	
	// 访问存在的键
	cout << M["apple"] << endl; // 5
	
	// 使用 find 避免意外插入
	auto it = M.find("banana");
	if (it != M.end())
		cout << it->first << ": " << it->second << endl;
	
	// 统计单词出现次数
	string word;
	while (cin >> word) M[word]++;
}

注意事项:

  • map 通过下标 [] 访问不存在的元素,会默认插入该键
  • 建议先用 find() 或 count() 判断存在性

3. 排序(sort)

C++
#include <bits/stdc++.h>
using namespace std;

struct Student {
	string name;
	int score;
} stu[100];

bool cmp(Student a, Student b) {
	return a.score > b.score; // 按分数降序
}

int main() {
	vector<int> v = {3, 1, 4, 1, 5};
	
	sort(v.begin(), v.end()); // 升序:1 1 3 4 5
	sort(v.begin(), v.end(), greater<int>()); // 降序:5 4 3 1 1
	
	// 自定义排序
	sort(stu + 1, stu + 100 + 1, cmp);
}

前提条件:序列必须已按升序排序!

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	vector<int> v = {1, 2, 3, 3, 5, 7};
	
	// lower_bound: 返回第一个 >= x 的迭代器
	auto it1 = lower_bound(v.begin(), v.end(), 3); // 指向第一个 3
	
	// upper_bound: 返回第一个 > x 的迭代器
	auto it2 = upper_bound(v.begin(), v.end(), 3); // 指向 5
	
	// binary_search: 返回 bool,判断 x 是否存在
	bool flag = binary_search(v.begin(), v.end(), 3); // true
	
	// 降序序列中查找(使用 greater<int>())
	vector<int> v2 = {7, 5, 3, 3, 2, 1};
	auto it3 = lower_bound(v2.begin(), v2.end(), 3, greater<int>());
	// 返回第一个 <= x 的位置
	
	// 另一种写法
	auto it4 = upper_bound(v2.rbegin(), v2.rend(), 3).base();
}
函数说明返回值
lower_bound(begin, end, x)第一个 大于等于 xx 的位置迭代器
upper_bound(begin, end, x)第一个 大于 xx 的位置迭代器
binary_search(begin, end, x)判断 xx 是否存在bool

5. 字符串(string)

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	string s = "hello";
	int len = s.length(); // 或 s.size()
	string sub = s.substr(1, 3); // "ell"
	s += " world"; // 拼接
	char c = s[0]; // 'h'
	
	// 查找子串
	int pos = s.find("world"); // 返回起始位置
	
	// 读入
	string s2;
	cin >> s2; // 读入一个单词
	getline(cin, s2); // 读入整行
	
	// 与 STL 算法配合
	sort(s.begin(), s.end()); // 对字符排序
}

七、综合应用:LIS(最长上升子序列)

借助 lower_bound 求严格上升的最长上升子序列:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	vector<int> V = {1, 5, 3, 3, 2, 3};
	vector<int> lis; // lis[i] 表示长度为 i+1 的 LIS 的最小末尾值
	
	for (int x : V) {
		auto it = lower_bound(lis.begin(), lis.end(), x);
		if (it == lis.end())
			lis.push_back(x);
		else
			*it = x;
	}
	
	for (int x : lis) cout << x << " "; // 输出: 1 2 3
	cout << endl;
	cout << "LIS 长度: " << lis.size() << endl;
	return 0;
}

原文