【前端】JavaScript 数据结构 -- 链表

JavaScript 数据结构 – 链表

1. 链表定义

1.1 定义

链表是一种使用任意单元存储线性表元素的数据结构,是一种链式存储的线性表,该种数据结构的特点:

  1. 方便插入删除
  2. 不支持随机存取
  3. 存储单元(元素)在内存中不一定连续
  4. 链表中元素存储元素信息外,还含有指向下一个元素的指针域信息

1.2 示意图

【前端】JavaScript 数据结构 -- 链表_第1张图片

  1. head:链表头
  2. Node:元素(结点)
  3. info:元素信息
  4. next:指向下一元素的指针域

1.3 链表支持方法

  1. add(node):增加一个结点
  2. remove():删除一个结点
  3. size():返回链表长度
  4. isEmpty():检测链表是否为空
  5. get(index):获取 index 下的元素

2. JavaScript 链表的实现

2.1 字面量声明

let list = {
    head:null,
    
    add: function(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    },
    
    remove: function() {
        if (this.head === null || this.head.next === null) {
            this.head = null;
            return;
        }

        let pre = this.head;
        let next = this.head.next;
        while (next.next !== null) {
            pre = next;
            next = pre.next;
        }
        console.log(pre.data);
        pre.next = null;
    },

    isEmpty: function() {
        return this.head === null;
    },

    size: function() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    },

    get: function(index) {
        if (this.head === null) {
            throw "list is empty";
        } 

        if (index === 0) {
            return this.head.data;
        }

        let size = 0;
        let next = this.head;
        while (next !== null) {
            next = next.next;
            size++;
            if (size == index) {
                return next.data;
            }
        }

        throw "list size is less than index";
    }
}

2.2 函数式声明

function UserList() {
    this.head = null;
    this.add = function(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    }
    
    this.remove = function() {
        if (this.head === null || this.head.next === null) {
            this.head = null;
            return;
        }

        let pre = this.head;
        let next = this.head.next;
        while (next.next !== null) {
            pre = next;
            next = pre.next;
        }
        console.log(pre.data);
        pre.next = null;
    }

    this.isEmpty = function() {
        return this.head === null;
    }

    this.size = function() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    }

    this.get = function() {
        if (this.head === null) {
            throw "list is empty";
        } 

        if (index === 0) {
            return this.head.data;
        }

        let size = 0;
        let next = this.head;
        while (next !== null) {
            next = next.next;
            size++;
            if (size == index) {
                return next.data;
            }
        }

        throw "list size is less than index";
    }
}

2.3 ES2015(ES6) 声明(建议)

class UserList {
    head = null;
    constructor() {

    }
    add(data) {
        let node = {
            data: data,
            next: null
        }
        if (this.head === null) {
            this.head = node
        } else {
            let pre = this.head;
            let next = this.head.next;
            while (next !== null) {
                pre = next;
                next = next.next;
            }
            pre.next = node;
        }
    }
    
    remove() {
        if (this.head === null || this.head.next === null) {
            this.head = null;
            return;
        }

        let pre = this.head;
        let next = this.head.next;
        while (next.next !== null) {
            pre = next;
            next = pre.next;
        }
        console.log(pre.data);
        pre.next = null;
    }

    isEmpty() {
        return this.head === null;
    }

    size() {
        if (this.head === null) {
            return 0;
        } 
        let size = 0;
        let next = this.head.next;
        size++;
        while (next !== null) {
            next = next.next;
            size++;
        }
        return size;
    }

    get(index) {
        if (this.head === null) {
            throw "list is empty";
        } 

        if (index === 0) {
            return this.head.data;
        }

        let size = 0;
        let next = this.head;
        while (next !== null) {
            next = next.next;
            size++;
            if (size == index) {
                return next.data;
            }
        }

        throw "list size is less than index";
    }
}

你可能感兴趣的:(前端,链表,数据结构,前端)