常见的 JavaScript 算法包括但不限于以下几种:
- 排序算法:如冒泡排序、快速排序、插入排序、选择排序、归并排序等。
- 搜索算法:如二分查找、线性查找等。
- 递归算法:如斐波那契数列、阶乘等。
- 图算法:如深度优先搜索(DFS)、广度优先搜索(BFS)、最短路径算法(Dijkstra 算法)、最小生成树算法(Prim 算法、Kruskal 算法)等。
- 字符串算法:如字符串匹配(KMP 算法、Boyer-Moore 算法)、最长公共子序列(LCS)等。
- 动态规划算法:如背包问题、最长递增子序列(LIS)等。
JavaScript 中也有一些常见的数据结构,虽然 JavaScript 不像其他编程语言那样有原生支持的数据结构,但可以通过对象、数组和函数等基本类型来模拟实现各种数据结构。一些常见的 JavaScript 数据结构包括:
- 数组:JavaScript 的数组是一种线性数据结构,用于存储一系列有序的元素。
- 对象:JavaScript 中的对象是一种键值对的集合,用于存储和组织数据。
- 栈:通过数组或链表实现的一种后进先出(LIFO)的数据结构。
- 队列:通过数组或链表实现的一种先进先出(FIFO)的数据结构。
- 链表:由节点组成的一种线性数据结构,每个节点包含一个数据元素和指向下一个节点的指针。
- 集合:一种无序且唯一的数据集合。
- 字典(Map):一种键值对的集合,其中每个键都是唯一的。
- 堆:一种基于完全二叉树的特殊数据结构,通常用于实现优先队列。
- 树:一种非线性数据结构,如二叉树、二叉搜索树(BST)、平衡二叉树(AVL 树)、红黑树等。
- 图:一种由节点和边组成的非线性数据结构,用于表示各种网络结构。
JavaScript 中实现链表
js
// 定义链表节点
class Node {
constructor(data) {
this.data = data; // 节点数据
this.next = null; // 指向下一个节点的指针
}
}
// 定义链表
class LinkedList {
constructor() {
this.head = null; // 链表头节点
this.size = 0; // 链表大小
}
// 向链表末尾添加节点
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
this.size++;
}
// 在指定位置插入节点
insertAt(data, index) {
if (index < 0 || index > this.size) {
return false;
}
const newNode = new Node(data);
if (index === 0) {
newNode.next = this.head;
this.head = newNode;
} else {
let current = this.head;
let prev = null;
let i = 0;
while (i < index) {
prev = current;
current = current.next;
i++;
}
newNode.next = current;
prev.next = newNode;
}
this.size++;
return true;
}
// 删除指定位置的节点
removeAt(index) {
if (index < 0 || index >= this.size || !this.head) {
return null;
}
let current = this.head;
if (index === 0) {
this.head = current.next;
} else {
let prev = null;
let i = 0;
while (i < index) {
prev = current;
current = current.next;
i++;
}
prev.next = current.next;
}
this.size--;
return current.data;
}
// 获取指定位置的节点数据
getAt(index) {
if (index < 0 || index >= this.size || !this.head) {
return null;
}
let current = this.head;
let i = 0;
while (i < index) {
current = current.next;
i++;
}
return current.data;
}
// 返回链表大小
getSize() {
return this.size;
}
// 打印链表元素
print() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' ';
current = current.next;
}
console.log(result.trim());
}
}
// 示例用法
const linkedList = new LinkedList();
linkedList.append(1);
linkedList.append(2);
linkedList.append(3);
linkedList.print(); // 输出: 1 2 3
linkedList.insertAt(4, 1);
linkedList.print(); // 输出: 1 4 2 3
linkedList.removeAt(2);
linkedList.print(); // 输出: 1 4 3
console.log(linkedList.getAt(1)); // 输出: 4
console.log(linkedList.getSize()); // 输出: 3// 定义链表节点
class Node {
constructor(data) {
this.data = data; // 节点数据
this.next = null; // 指向下一个节点的指针
}
}
// 定义链表
class LinkedList {
constructor() {
this.head = null; // 链表头节点
this.size = 0; // 链表大小
}
// 向链表末尾添加节点
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
this.size++;
}
// 在指定位置插入节点
insertAt(data, index) {
if (index < 0 || index > this.size) {
return false;
}
const newNode = new Node(data);
if (index === 0) {
newNode.next = this.head;
this.head = newNode;
} else {
let current = this.head;
let prev = null;
let i = 0;
while (i < index) {
prev = current;
current = current.next;
i++;
}
newNode.next = current;
prev.next = newNode;
}
this.size++;
return true;
}
// 删除指定位置的节点
removeAt(index) {
if (index < 0 || index >= this.size || !this.head) {
return null;
}
let current = this.head;
if (index === 0) {
this.head = current.next;
} else {
let prev = null;
let i = 0;
while (i < index) {
prev = current;
current = current.next;
i++;
}
prev.next = current.next;
}
this.size--;
return current.data;
}
// 获取指定位置的节点数据
getAt(index) {
if (index < 0 || index >= this.size || !this.head) {
return null;
}
let current = this.head;
let i = 0;
while (i < index) {
current = current.next;
i++;
}
return current.data;
}
// 返回链表大小
getSize() {
return this.size;
}
// 打印链表元素
print() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' ';
current = current.next;
}
console.log(result.trim());
}
}
// 示例用法
const linkedList = new LinkedList();
linkedList.append(1);
linkedList.append(2);
linkedList.append(3);
linkedList.print(); // 输出: 1 2 3
linkedList.insertAt(4, 1);
linkedList.print(); // 输出: 1 4 2 3
linkedList.removeAt(2);
linkedList.print(); // 输出: 1 4 3
console.log(linkedList.getAt(1)); // 输出: 4
console.log(linkedList.getSize()); // 输出: 3这段代码定义了一个 Node 类表示链表节点,以及一个 LinkedList 类表示链表。可以通过 append 方法向链表末尾添加节点,通过 insertAt 方法在指定位置插入节点,通过 removeAt 方法删除指定位置的节点,通过 getAt 方法获取指定位置的节点数据,通过 getSize 方法获取链表大小,通过 print 方法打印链表元素。
liang14658fox