首页 > 代码库 > javascript普通链表及双向链表
javascript普通链表及双向链表
写代码的真是心细啊,每一步操作的先后顺序都在卡准。
我其实只是理解了思想和大概的操作。
真正要用时,可能还是要复制,粘贴。。。:)
function LinkedList(){ var Node = function(element){ this.element = element; this.next = null; }; var length = 0; var head = null; this.append = function(element){ var node = new Node(element), current; if (head == null){ head = node; } else { current = head; while (current.next){ current = current.next; } current.next = node; } length++; }; this.insert = function(position, element){ if (position >=0 && position <=length){ var node = new Node(element), current = head, previous, index =0; if (position === 0){ node.next = current; head = node; } else { while (index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; } length++; return true; } else { return false; } }; this.removeAt = function(position){ if (position > -1 && position < length){ var current = head, previous, index = 0; if (position === 0){ head = current.next; } else { while (index++ < position){ previous = current; current = current.next; } previous.next = current.next; } length--; return current.element; } else { return null; } }; this.remove = function(element){ var index = this.indexOf(element); return this.removeAt(index); }; this.indexOf = function(element){ var current = head, index = -1; while (current){ if (element == current.element){ return index; } index++; current = current.next; } return -1; }; this.isEmpty = function(){ return length === 0; }; this.size = function () { return length; }; this.toString = function(){ var current = head, string = ‘‘; while (current){ string += current.element; current = current.next; } return string; }; this.getHead = function(){ return head; } this.print = function(){ };}var list = new LinkedList();list.append(15);list.append(10);console.log(list.toString());function DoublyLinkedList() { var Node = function(element){ this.element = element; this.next = null; this.prev = null; }; var length = 0; var head = null; var tail = null; this.insert = function(position, element){ if(position >=0 && position <= length){ var node = new Node(element), current = head, previous, index = 0; if (position === 0){ if (!head){ head = node; tail = node; } else { node.next = current; current.prev = node; head = node; } } else if (position === length){ current = tail; current.next = node; node.prev = current; tail = node; } else { while (index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; current.prev = node; node.prev = previous; } length++; return true; } else { return false; } } this.removeAt = function(position){ if (position > -1 && position < length){ var current = head, previous, index = 0; if (position === 0){ head = current.next; if (length === 1){ tail = null; } else { head.prev = null; } } else if(position == length-1){ current = tail; tail = current.prev; tail.next = null; } else { while (index++ < position){ previous = current; current = current.next; } previous.next = current.next; current.next.prev = previous; } length--; return current.element; } else { return null; } }}
javascript普通链表及双向链表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。