//链表
//内存中不是连续的空间,元素本身节点和指向下一个元素的引用
//单向链表
function LinkedList() {
function Node(data) {
this.data = data;
this.next = null;
}
this.head = null;
this.length = 0;
LinkedList.prototype.append = function(data) {
if(this.length == 0) {
var newNode = new Node(data);
this.head = newNode;
}else{
var newNode = new Node(data);
var current = this.head;
while(current.next) {
current = current.next;
}
current.next = newNode;
}
this.length += 1;
}
LinkedList.prototype.toString = function() {
var current = this.head;
var listString = "";
while(current) {
listString += current.data + "";
current = current.next;
}
return listString;
}
LinkedList.prototype.insert = function(position, data) {
if(position < 0 || position > this.length) return false;
var newNode = new Node(data);
if(position == 0) {
newNode.next = this.head;
this.head = newNode;
}else{
var index = 0;
var current = this.head;
var previous = null;
while(index++ < position) {
previous = current;
current = current.next;
}
newNode.next = current;
previous.next = newNode;
}
this.length += 1;
return true;
}
LinkedList.prototype.get = function(position) {
if(position < 0 || position >= this.length) return null;
var current = this.head;
var index = 0;
while(index++ < position) {
current = current.next;
}
return current.data;
}
LinkedList.prototype.indexOf = function(data) {
var current = this.head;
var index = 0;
while(current) {
if(current.data == data) {
return index;
}
current = current.next;
index += 1;
}
return -1;
}
LinkedList.prototype.update = function(position, newData) {
if(position < 0 || position >= this.length) return false;
var current = this.head;
var index = 0;
while(index++ < position) {
current = current.next;
}
current.data = newData;
return true;
}
LinkedList.prototype.removeAt = function(position) {
if(position < 0 || position >= this.length) return false;
if(position == 0) {
this.head = this.head.next;
}else{
var index = 0;
var current = this.head;
var previous = null;
while(index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.length -= 1;
return true;
}
LinkedList.prototype.remove = function(data) {
var position = this.indexOf(data);
return this.removeAt(position);
}
}