链表

链表(Linked List)是一种线性数据结构,它由一系列的节点组成,其中每个节点包含两部分:

  1. 数据部分:存储节点的实际数据。
  2. 指针部分:存储指向下一个节点的引用(或指针)。

与数组不同,链表中的元素不需要在内存中是连续的。每个节点通过指针连接到下一个节点,形成一个链式结构。

链表有多个变种,包括:

  • 单向链表:每个节点只包含指向下一个节点的引用。
  • 双向链表:每个节点包含两个指针,分别指向前一个节点和后一个节点。
  • 循环链表:尾节点指向头节点,形成一个环。

单向链表的实现代码:
 
// 节点类
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 === null) {
            this.head = newNode;  // 如果链表为空,将新节点作为头节点
        } else {
            let current = this.head;
            while (current.next) {
                current = current.next;  // 遍历到链表的最后一个节点
            }
            current.next = newNode;  // 将新节点连接到最后一个节点的 next 指针上
        }
        this.size++;  // 增加链表的大小
    }

    // 在链表的头部插入节点
    prepend(data) {
        const newNode = new Node(data);  // 创建新节点
        newNode.next = this.head;  // 将新节点的 next 指向当前的头节点
        this.head = newNode;  // 将新节点设置为头节点
        this.size++;  // 增加链表的大小
    }

    // 删除链表的第一个节点
    removeFirst() {
        if (this.head === null) return;  // 如果链表为空,直接返回
        this.head = this.head.next;  // 将头节点指向下一个节点
        this.size--;  // 减少链表的大小
    }

    // 删除链表的最后一个节点
    removeLast() {
        if (this.head === null) return;  // 如果链表为空,直接返回
        if (this.size === 1) {  // 如果链表中只有一个节点
            this.head = null;  // 将头节点设置为 null
        } else {
            let current = this.head;
            let previous;
            while (current.next) {
                previous = current;
                current = current.next;  // 遍历到最后一个节点
            }
            previous.next = null;  // 删除最后一个节点
        }
        this.size--;  // 减少链表的大小
    }

    // 查找链表中某个特定的节点
    find(data) {
        let current = this.head;
        while (current) {
            if (current.data === data) {
                return current;  // 找到节点,返回节点
            }
            current = current.next;
        }
        return null;  // 如果没有找到,返回 null
    }

    // 打印链表
    print() {
        if (this.head === null) {
            console.log('List is empty');
            return;
        }
        let current = this.head;
        let listValues = '';
        while (current) {
            listValues += current.data + ' -> ';
            current = current.next;
        }
        console.log(listValues.slice(0, -4));  // 去掉最后的箭头
    }
}

// 测试链表
const list = new LinkedList();
list.append(10);  // 添加 10 到链表
list.append(20);  // 添加 20 到链表
list.append(30);  // 添加 30 到链表
list.prepend(5);  // 在链表头部添加 5
list.print();     // 输出: 5 -> 10 -> 20 -> 30

console.log(list.find(20));  // 输出: Node { data: 20, next: Node { data: 30, next: null } }

list.removeFirst();  // 删除头节点
list.removeLast();   // 删除尾节点
list.print();        // 输出: 10 -> 20

 

posted @ 2025-03-09 23:04  我是格鲁特  阅读(19)  评论(0)    收藏  举报