• 数据结构-链表


    什么是链表?链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中指针链接次序实现的,这里就不展示图片了简单说一下

    index.html

    script 引入link.js

    <script >

       let link = new LinkNode()
        link.push(1)
        link.push(2)
        link.push(3)
        function decToBinary(num) {
            const s = new Stack()
            while(num > 0) {
                s.push(num % 2)
                num = Math.floor(num / 2)
            }
            let str = ''
            while(s.len() > 0) {
                str += s.pop()
            }
            return str
        }

    </script >

    link.js

    class Node {
        constructor(data) {
            this.data = data
            this.next = null
        }
    }
    class LinkNode{
        constructor() {
            this.head = null
            this.count = 0
        }
        push(data) {
            let node = new Node(data)
            if(!this.head) {
                this.head = node
            } else {
                let current = this.head
                while(current.next) {
                    current = current.next
                }
                current.next = node
            }
            this.count++
        }
        get(index) {
            if(index >= this.count) {
                return undefined
            } else {
                let current = this.head
                for(let i =0; i< index; i++) {
                    current = current.next
                }
                return current.data
            }
        }
        set(index, data) {
            if(index >= this.count) {
                return false
            } else {
                let current = this.head
                for(let i =0; i< index; i++) {
                    current = current.next
                }
                current.data = data
                return true
            }
        }
        remove(index) {
            if(index >= this.count) {
                return false
            } else {
                let current = this.head
                let previous = null
                for(let i =0; i< index; i++) {
                    previous = current
                    current = current.next
                }
                previous.next = current.next
                this.count--
            }
        }
        insert(index, data) {
            if(index>=this.count){
                return false
            }else{
                let node = new Node(data)
                let current = this.head
                let previous = null
                for(let i =0;i<index;i++){
                    previous = current
                    current = current.next
                }
                previous.next = node
                node.next = current
                this.count++
                return true
            }
        }
    }
  • 相关阅读:
    hdu 5352 匈牙利(多重匹配)
    hdu 2112 最短路+stl
    hdu 1811 拓扑排序+并查集+细心
    hdu5407 CRB and Candies
    hdu1018 Big Number
    hdu5410 CRB and His Birthday
    hdu1023 Train Problem II
    hdu4812 D Tree
    hdu1085 Holding Bin-Laden Captive!
    hdu4810 Wall Painting
  • 原文地址:https://www.cnblogs.com/yzy521/p/14149815.html
Copyright © 2020-2023  润新知