链表的数据结构
简单链表的代码实现
<script>
function LinkedList() {
//创建新结点函数
function Node(data) {
this.data = data;
this.next = null;
}
this.head = null;
this.length = 0;
// 1、append方法
LinkedList.prototype.append = function(data) {
//1、创建新的结点
var newNode = new Node(data);
// 2、判断是不是第一个节点
if (this.length == 0) { //是第一个结点
this.head = newNode;
} else { //不是第一个结点
var current = this.head;
// 找到最后一个结点
while (current.next) {
current = current.next;
}
// 最后结点的next指向新的结点
current.next = newNode
}
// 长度+1
this.length += 1;
}
// 2、toString方法
LinkedList.prototype.toString = function() {
var current = this.head;
var listString = "";
while (current) {
listString += current.data + " ";
current = current.next;
}
return listString;
}
// 3、get方法
LinkedList.prototype.get = function(position) {
//越界判断
if (position < 0 || position >= this.length) return null;
var index = 0;
var current = this.head;
while (index++ < position) {
current = current.next;
}
return current.data;
}
// 4、insert方法
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 percious = null;
var current = this.head;
while (index++ < position) {
percious = current;
current = current.next;
}
percious.next = newNode;
newNode.next = current;
}
this.length += 1;
return true;
}
// 5、updata方法
LinkedList.prototype.update = function(position, newData) {
if (position < 0 || position >= this.length) return false;
// if (position == 0) {
// this.head.data = newData;
// } else {
var current = this.head;
var index = 0;
while (index++ < position) {
current = current.next;
}
current.data = newData;
// }
return true;
}
// 6、indexOf方法
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;
}
// 7、removeAt方法
LinkedList.prototype.removeAt = function(position) {
if (position < 0 || position >= this.length) return null;
var current = this.head;
if (position == 0) {
this.head = this.head.next;
} else {
var index = 0;
var percious = null;
while (index++ < position) {
percious = current;
current = current.next;
}
percious.next = current.next;
}
this.length -= 1;
return current.data;
}
// 8、remove方法
LinkedList.prototype.remove = function(olddata) {
var index = this.indexOf(olddata);
this.removeAt(index);
}
// 9、isEmpty方法
LinkedList.prototype.isEmpty = function() {
return this.length == 0;
}
// 10、size方法
LinkedList.prototype.size = function() {
return this.length;
}
}
var linked = new LinkedList();
linked.append(1);
linked.append(3);
linked.append(2);
var index = linked.toString();
alert(index);
// var get = linked.get(2);
// alert(get);
// var index = linked.indexOf(0);
// alert(index);
// linked.insert(3, "ab");
// linked.update(2, "ab");
// linked.removeAt(2);
linked.remove(2);
var index1 = linked.toString();
alert(index1);
var length = linked.size();
alert(length);
</script>
我个人认为链表只要搞清楚其指针的指向也就是next的指向,链表还是非常容易理解的,在插入和删除结点的时候,要特别注意结点的指向问题,删除结点时,当没有引用指向该要删除的结点时,结点就会自动被收回。