单链表的插入、删除、查找、反转

258 阅读2分钟
/** * 功能:单链表的插入、删除、查找、反转 */ // 定义节点 class Node{     constructor(data){         this.data = data         this.next = null     } } class LinkList{     constructor(data){         // 初始化头结点         this.head = new Node(data)     }     // 数组生成链表     genListNode(ListArr){       this.head = new Node(ListArr[0])        let i = 1        let cur = this.head        while(i < ListArr.length){            let val = ListArr[i]            cur.next = new Node(val)            cur = cur.next            i++        }        return  this.head    }     // 根据value查找节点     findByValue = (value) => {         let currentNode = this.head         while(currentNode !== null && currentNode.data !== value){             // 如果没找到就继续下个节点            currentNode = currentNode.next         }         // 判断该节点是否找到         console.log(currentNode)         return currentNode === null ? -1 : currentNode     }     // 根据index查找节点     findByIndex = (index) => {         let pos = 0         let currentNode = this.head         while(currentNode !== null && pos !== index){             currentNode = currentNode.next             pos++         }         console.log(currentNode)         return currentNode === null ? -1 : currentNode     }     // 指定元素向后插入     insert = (value,element) => {         // 先查找到改元素         let currentNode = this.findByValue(element)         // 如果没有找到         if(currentNode === -1){             console.log('未找到该元素')             return          }         // 如果找到了,则创建该元素节点,进行插入操作         let newNode = new Node(value)           newNode.next = currentNode.next  // 将新创建节点的next指向要插入对象的next,脑补next指针指向图         currentNode.next = newNode  // 将当前节点的next指向新创建的节点     }     // 根据值删除节点     delete = (value) => {         let currentNode = this.head         let preNode = null         while(currentNode !== null && currentNode.data !== value){             preNode = currentNode // 保存前一个节点             currentNode = currentNode.next         }         if(currentNode === null) return -1         preNode.next = currentNode.next // 将前一个节点的next指向当前节点的next,完成删除currentNode操作     }     // 链表反转     reverse = () => {        let currrentNode = null        let headNode  = this.head        while(this.head && this.head.next){  // 若head.next为null 则遍历到尾节点            currrentNode = this.head.next  // 得到下一个节点            // 交换当前节点与下一个节点的指向  head.next = currrentNode.next  currrentNode.next = headNode            this.head.next = currrentNode.next             currrentNode.next = headNode            headNode = currrentNode        }        return headNode     }     //遍历所有节点     print = () => {         let currentNode = this.head         // 如果节点不为空         while(currentNode !== null){             console.log(currentNode.data)             currentNode = currentNode.next         }     } } // 测试 // 1、 传入数组数据生成链表 const list = new LinkList() // 1、传入数组生成链表 console.log('验证生成链表') console.log('-------------生成链表------------') list.genListNode([1,2,3,4]) console.log(list)list.print()console.log('-------------生成链表------------')console.log('')// 2、指定元素插入操作console.log('-------------插入操作------------')list.insert(9,3)list.print()console.log('-------------插入操作------------')console.log('')// 3、根据value查找节点console.log('-------------根据value查找节点------------')list.findByValue(2)console.log('-------------根据value查找节点------------')console.log('')// 4、根据index查找节点console.log('-------------根据index查找节点------------')list.findByIndex(2)console.log('-------------根据index查找节点------------')console.log('')// 5、删除操作console.log('-------------删除操作------------')list.delete(2)console.log(list)list.print()console.log('-------------删除操作------------')console.log('')// 6、链表反转操作console.log('-------------链表反转------------')const reversedList = list.reverse()console.log(reversedList)console.log('-------------链表反转------------')