数据结构--链表

103 阅读2分钟

1.单向链表

链表节点(Node)类实现

function Node (el) {
    this.el = el
    this.next = null
}

链表(Link)类实现

// 构造函数
function Link() {
    this.head = new Node('head')
}

// 链表结尾追加一个节点
Link.prototype.append = function(el) {
    var currNode = this.head
    while(currNode.next !== null) {
        currNode = currNode.next
    }
    currNode.next = new Node(el)
}

// 按节点的值查找节点
Link.prototype.find = function(el) {
    var currNode = this.head
    while(currNode && currNode.el !== el) {
        currNode = currNode.next
    }
    return currNode
}

// 插入一个节点
Link.prototype.insert = function(newEl, oldEl) {
    var newNode = new Node(newEl)
    var findNode = this.find(oldEl)
    if(findNode) {
        newNode.next = findNode.next
        findNode.next = newNode
    } else {
        throw new Error('找不到给定插入的节点')
    }
}

// 展示链表中的元素
Link.prototype.display = function() {
    var currNode = this.head.next
    while(currNode) {
        console.log(currNode.el)
        currNode = currNode.next
    }
}

// 寻找给定节点的前一个节点
Link.prototype.findPrev = function(el) {
    var currNode = this.head
    while(currNode.next && currNode.next.el !== el) {
        currNode = currNode.next
    }
    return currNode
}

// 删除给定的节点
Link.prototype.remove = function(el) {
    var prevNode = this.findPrev(el)
    if(prevNode.next !== null) {
        prevNode.next = prevNode.next.next
    } else {
        throw new Error('找不到要删除的节点')
    }
}

2.双向链表

双向链表节点(DNode)类实现

function DNode(el) {
    this.el = el
    this.prev = null
    this.next = null
}

双向链表(DLink)类的实现

// 构造函数
function DLink() {
    this.head = new DNode('head')
}

// append:向链表结尾添加一个节点
DLink.prototype.find = function (el) {
    var currNode = this.head
    while(currNode.next !== null) {
        currNode = currNode.next
    }
    var newNode = new Node(el)
    newNode.next = currNode.next
    currNode.next = newNode
}

// find: 查找给定的节点
DLink.prototype.find = function(el) {
    var currNode = this.head
    while(currNode &&currNode.el !== el) {
        currNode = currNode.next
    }
    return currNode
}

// insert: 插入一个节点
DLink.prototype.insert = function(newEl, oldEl) {
    var newNode = new DNode(newEl)
    var currNode = this.find(oldEl)
    if(currNode) {
        newNode.next = currNode.next
        newNode.prev = currNode
        currNode.next = newNode
    } else {
        throw new  Error('未找到指定要插入节点位置对应的值!')
    }
}

// display: 顺序展示链表节点
DLink.prototype.display = function() {
    var currNode = this.head.next
    while(currNode) {
        console.log(currNode.el)
        currNode = currNode.next
    }
}

// findLast: 查找最后一个节点
DLink.prototype.findLast = function() {
    var currNode = this.head
    while(currNode.next !== null) {
        currNode = currNode.next
    }
    return currNode
}

// dispReverse: 逆序展示链表元素
DLink.prototype.dispReverse = function() {
    var currNode = this.head
    currNode = this.findLast()
    while(currNode.prev !== null) {
        console.log(currNode.el)
        currNode = currNode.prev
    }
}

// remove: 删除节点
DLink.prototype.remove = function(el) {
    var currNode = this.find(el)
    if(currNode && currNode.next !== null) {
        currNode.prev.next = currNode.next
        currNode.next.prev = currNode.prev
        currNode.next = null
        currNode.prev = null
    } else {
        throw new Error('找不到要删除对应的节点')
    }
}

3.循环链表

循环链表(CLink)类实现

// 构造函数
function CLink() {
    this.head = new Node('head')
    this.head.next = this.head
}

// append: 向链表节点增加一个元素
CLink.prototype.append = function(el) {
    var currNode = this.head
    while(currNode.next !== null && currNode.next !== this.head) {
        currNode = currNode.next
    }
}

// find: 根据节点的值查找链表节点
CLink.prototype.find = function(el) {
    var currNode = this.head
    while(currNode && currNode.el !== el) {
        currNode = currNode.next
    }
    return currNode
}

// insert: 插入一个节点
CLink.prototype.insert = function(newEl, oldEl) {
    var newNode = new Node(newEl)
    var currNode = this.find(oldEl)
    if(currNode) {
        newNode.next = currNode.next
        currNode.next = newNode
    } else {
        throw new Error('未找到指定要插入节点位置对应的值!')
    }
}

// display: 展示链表元素节点
CLink.prototype.display = function() {
    var currNode = this.head.next
    while(currNode && currNode.next !== this.head) {
        console.log(currNode.el)
        currNode = currNode.next
    }
}

// 根据给定值寻找前一个节点
CLink.prototype.findPrev = function(el) {
    var currNode = this.head
    while(currNode.next && currNode.next.el !== el) {
        currNode = currNode.next
    }
    return currNode
}

// 删除给定值对应的节点
CLink.prototype.remove = function(el) {
    var prevNode = this.findPrev(el)
    if(prevNode.next !== null) {
        prevNode.next = prevNode.next.next
        prevNode.next.next = null
    } else {
        throw new Error('找不到要删除的节点')
    }
}