定义:连续存储相同类型数据的线性结构,支持随机访问。
应用场景:列表渲染、数据缓存、算法处理
代码示例:
// 数组基本操作
const arr = [1, 2, 3, 4];
arr.push(5); // O(1) 平均时间复杂度
arr.pop(); // O(1)
arr.shift();// O(n) 不推荐高频使用
arr.unshift(0); // O(n)
// 数组遍历优化
// 推荐写法(减少属性查找)
for (let i = 0, len = arr.length; i < len; i++) {
console.log(arr[i]);
}
// 二维数组初始化
const matrix = Array.from({ length: 3 }, () => new Array(3).fill(0));
使用建议:
push/pop
替代 shift/unshift
for
循环而非 forEach
可提高性能Array.from
初始化注意事项:
sort
会改变原数组)定义:由节点组成的链式结构,节点包含值和指向下一个节点的指针
应用场景:内存管理、DOM 节点操作、LRU 缓存
代码示例:
class ListNode {
constructor(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
}
// 链表反转
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
使用建议:
注意事项:
定义:遵循后进先出(LIFO)原则的线性结构
应用场景:路由管理、撤销重做、函数调用栈
代码示例:
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
}
// 浏览器路由栈模拟
const routerStack = new Stack();
routerStack.push('/home');
routerStack.push('/about');
routerStack.pop(); // 返回 '/about',当前栈顶 '/home'
使用建议:
push/pop
Uint8Array
实现注意事项:
定义:遵循先进先出(FIFO)原则的线性结构
应用场景:任务调度、事件队列、消息缓冲
代码示例:
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
return this.items.shift();
}
front() {
return this.items[0];
}
}
// 任务队列处理
const taskQueue = new Queue();
taskQueue.enqueue(() => console.log('Task 1'));
taskQueue.enqueue(() => console.log('Task 2'));
taskQueue.dequeue()(); // 执行 Task 1
使用建议:
enqueue
+ pop
替代 shift
注意事项:
定义:通过哈希函数将键映射到存储位置的结构
应用场景:状态管理、缓存、快速查找
代码示例:
class HashTable {
constructor(size = 1024) {
this.table = new Array(size);
this.size = size;
}
hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % this.size;
}
set(key, value) {
const index = this.hash(key);
this.table[index] = value;
}
get(key) {
const index = this.hash(key);
return this.table[index];
}
}
// 简单状态管理
const state = new HashTable();
state.set('user', { name: 'John', age: 30 });
console.log(state.get('user'));
使用建议:
注意事项:
定义:n 个节点构成的有限集合,具有层次关系
应用场景:DOM 树、路由配置、算法优化
代码示例:
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// 二叉树前序遍历
function preorderTraversal(root) {
const result = [];
function traverse(node) {
if (!node) return;
result.push(node.val);
traverse(node.left);
traverse(node.right);
}
traverse(root);
return result;
}
// 构建DOM树
const div = document.createElement('div');
const span = document.createElement('span');
div.appendChild(span);
使用建议:
注意事项:
定义:由节点和边构成的非线性结构
应用场景:社交网络、路径规划、依赖分析
代码示例:
class Graph {
constructor() {
this.adjList = new Map();
}
addVertex(vertex) {
if (!this.adjList.has(vertex)) {
this.adjList.set(vertex, new Set());
}
}
addEdge(v1, v2) {
if (this.adjList.has(v1) && this.adjList.has(v2)) {
this.adjList.get(v1).add(v2);
this.adjList.get(v2).add(v1);
}
}
bfs(start) {
const visited = new Set();
const queue = [start];
visited.add(start);
while (queue.length) {
const node = queue.shift();
console.log(node);
this.adjList.get(node).forEach(neighbor => {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
});
}
}
}
// 页面依赖关系图
const graph = new Graph();
graph.addVertex('A');
graph.addVertex('B');
graph.addEdge('A', 'B');
graph.bfs('A'); // 输出 A -> B
使用建议:
注意事项:
建议在实际开发中建立数据结构选择决策表,根据具体场景评估时间复杂度、空间复杂度和操作频率,同时结合浏览器 / Node.js 环境特性进行优化。