参考
单链表(LinkList)
function LinkList(){
this.head = null;
this.length = 0;
function Node(data){
this.data = data;
this.next = null;
}
LinkList.prototype.append = function(element){
var newNode = new Node(element);
if(this.length === 0){
this.head = newNode;
}else{
var node = this.head;
while(node.next){
node = node.next;
}
node.next = newNode;
}
return ++ this.length;
}
LinkList.prototype.insert = function(position,element){
if(position < 0 || position > this.length || !this.length)
return false;
var newNode = new Node(element);
if(position == 0){
newNode.next = this.head;
this.head = newNode;
}else{
var node = this.head;
var prev = null;
while(position){
prev = node;
node = node.next;
position --;
}
newNode.next = node;
prev.next = newNode;
}
return ++ this.length;
}
LinkList.prototype.get = function(position){
if(position < 0 || position > this.length - 1 || !this.length)
return -1;
var node = this.head;
while(position){
node = node.next;
position --;
}
return node.data;
}
LinkList.prototype.indexOf = function(element){
var node = this.head;
var index = 0;
while(node){
if(node.data === element){
return index;
}
index ++;
node = node.next;
}
return -1;
}
LinkList.prototype.update = function(position,element){
if(position < 0 || position > this.length - 1 || !this.length)
return false;
var node = this.head;
while(position){
node = node.next;
position --;
}
node.data = element;
return true;
}
LinkList.prototype.removeAt = function(position){
if(position < 0 || position > this.length - 1 || !this.length)
return false;
var node = this.head;
if(position == 0){
this.head = node.next;
}else{
var prev = null;
while(position){
prev = node;
node = node.next;
position --;
}
prev.next = node.next;
}
node.next = null;
this.length --;
return node.data;
}
LinkList.prototype.remove = function(element){
var index = this.indexOf(element);
return this.removeAt(index);
}
LinkList.prototype.isEmpty = function(){
return this.length === 0;
}
LinkList.prototype.size = function(){
return this.length;
}
LinkList.prototype.toString = function(){
var resultString = '';
var node = this.head;
while(node.next){
resultString += node.data;
node = node.next;
}
return resultString;
}
}
双向链表(DoubleLinkList)
- 区别单链表
- 1、多了尾指针指向链表尾部(tail)
- 2、每个节点除了next指针、还多了prev指针指向前一个节点
function DoubleLinkList(){
this.head = null;
this.tail = null;
this.length = 0;
function Node(data){
this.data = data;
this.prev = null;
this.next = null;
}
DoubleLinkList.prototype.append = function(element){
var newNode = new Node(element);
if(this.length == 0){
this.head = newNode;
this.tail = newNode;
}else{
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
return ++ this.length;
}
DoubleLinkList.prototype.insert = function(position,element){
if(position < 0 || position > this.length)
return false;
var newNode = new Node(element);
if(this.length === 0){
this.head = newNode;
this.tail = newNode;
}else{
if(position == 0){
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}else if(position == this.length){
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}else{
var node = this.head;
while(position){
node = node.next;
position --;
}
newNode.next = node;
newNode.prev = node.prev;
node.prev.next = newNode;
node.prev = newNode;
}
}
this.length ++;
return true;
}
DoubleLinkList.prototype.get = function(position){
if(position < 0 || position > this.length - 1)
return null;
var temp = Math.ceil(this.length / 2);
var node = temp > position
? this.head : this.tail;
var index_p = temp > position
? position : this.length - 1 - position;
while(index_p){
node = temp > position
? node.next : node.prev;
index_p --;
}
return node;
}
DoubleLinkList.prototype.indexOf = function(element){
var node = this.head;
var index = 0;
while(node){
if(node.data === element){
return index;
}
node = node.next;
index ++;
}
return -1;
}
DoubleLinkList.prototype.update = function(position,element){
if(position < 0 || position > this.length - 1 || !this.length)
return false;
var node = this.head;
while(position){
node = node.next;
position --;
}
node.data = element;
return true;
}
DoubleLinkList.prototype.removeAt = function(position){
if(position < 0 || position > this.length - 1 || !this.length)
return false;
var node = this.head;
if(this.length === 1){
this.head = null;
this.tail = null;
}else{
if(position == 0){
this.head.next.prev = null;
this.head = this.head.next;
}else if(position == this.length - 1){
this.tail.prev.next = null;
node = this.tail;
this.tail = this.tail.prev;
}else{
while(position){
node = node.next;
position --;
}
node.prev.next = node.next;
node.next.prev = node.prev;
}
node.prev = null;
node.next = null;
}
this.length --;
return node;
}
DoubleLinkList.prototype.remove = function(element){
var index = this.indexOf(element);
return this.removeAt(index);
}
DoubleLinkList.prototype.isEmpty = function(){
return this.length === 0;
}
DoubleLinkList.prototype.size = function(){
return this.length;
}
DoubleLinkList.prototype.toString = function(){
var resultString = '';
var node = this.head;
while(node){
resultString += node.data;
node = node.next;
}
return resultString;
}
DoubleLinkList.prototype.forwardString = function(){
var resultString = '';
var node = this.head;
while(node){
resultString += node.data;
node = node.next;
}
return resultString;
}
DoubleLinkList.prototype.backwordString = function(){
var resultString = '';
var node = this.tail;
while(node){
resultString += node.data;
node = node.prev;
}
return resultString;
}
}
var doubleList = new DoubleLinkList();
doubleList.append("0");
doubleList.append("1");
doubleList.insert(0,"2");
doubleList.insert(4,"3");
doubleList.insert(2,"4");