We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function DoublyLinkedList(){ this.length = 0; this.head = null; this.tail = null } function Node(element){ this.element = element; this.next = null; this.prev = null; } DoublyLinkedList.prototype.append = function(element){ var newNode = new Node(element); if(!this.head){ this.head = newNode; this.tail = newNode; }else{ this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } this.length++; }; DoublyLinkedList.prototype.forwardArray = function(element){ var arr = []; var currentNode = this.head while(currentNode){ arr.push(currentNode.element) currentNode = currentNode.next; } return arr } DoublyLinkedList.prototype.reverseArray = function(element){ var arr = []; var currentNode = this.tail while(currentNode){ arr.push(currentNode.element) currentNode = currentNode.prev; } return arr } DoublyLinkedList.prototype.insert = function(position, element){ //检测越界问题 if(position<0 || position>this.length){ throw Error("请输入正确position") } var newNode = new Node(element); var currentNode = this.head; var previousNode = null; var index = 0; if(position==0){ if(this.head){ this.head = newNode; newNode.next = currentNode; currentNode.prev = newNode; }else{ this.head = newNode; this.tail = newNode; } }else if(position == this.length){ this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; }else{ while(index++<position){ previousNode = currentNode; currentNode = currentNode.next } previousNode.next = newNode; currentNode.prev = newNode; newNode.prev = previousNode; newNode.next = currentNode; } return this.length++; } DoublyLinkedList.prototype.removeAt = function(position){ //检测越界问题 if(position<0 || position>this.length-1){ return null } var currentNode = this.head; var prevNode = null; var index = 0; if(position==0){ //不用再判断是否存在头结点了,越界的时候检测过了 if(this.length==1){ this.head = null; this.tail = null; }else{ this.head = this.head.next; this.head.prev = null; } }else if(position=this.length-1){ currentNode = this.tail this.tail = this.tail.prev this.tail.next = null; }else{ while(index++<position){ prevNode = currentNode; currentNode = currentNode.next; } prevNode.next = currentNode.next currentNode.next.prev = prevNode; } this.length--; return currentNode.element } DoublyLinkedList.prototype.indexOf = function(element){ //检测越界问题 var currentNode = this.head; var index = 0; while(currentNode){ if(currentNode.element==element){ return index } currentNode = currentNode.next index++ } return -1 } // 判断是否为空 DoublyLinkedList.prototype.isEmpty = function () { return this.length === 0 } // 获取链表长度 DoublyLinkedList.prototype.size = function () { return this.length } // 获取第一个元素 DoublyLinkedList.prototype.getHead = function () { return this.head.element } // 获取最后一个元素 DoublyLinkedList.prototype.getTail = function () { return this.tail.element } var DoublyLinkedList = new DoublyLinkedList(); DoublyLinkedList.append('a') DoublyLinkedList.append('b') DoublyLinkedList.append('c') DoublyLinkedList.append('d') console.log(DoublyLinkedList.removeAt(3)) console.log(DoublyLinkedList.head, DoublyLinkedList.tail) console.log(DoublyLinkedList.forwardArray()) console.log(DoublyLinkedList.reverseArray())
The text was updated successfully, but these errors were encountered:
No branches or pull requests
双向链表
代码实现
参考链接
The text was updated successfully, but these errors were encountered: