js数据结构中的链表
单向链表
/**
* Created by Administrator on 2016/12/9.
*/
function Node(element) {
this.element=element;
this.next=null;
}
/*LList 类提供了对链表进行操作的方法。该类的功能包括插入删除节点、在列表中查找给
定的值。该类也有一个构造函数,链表只有一个属性,那就是使用一个 Node 对象来保存该
链表的头节点。*/
function LList() {
this.head=new Node("head");
this.find=find;
this.findPrevious=findPrevious;
this.insert=insert;
this.remove=remove;
this.display=display;
}
/*插入新节点
我们要分析的第一个方法是 insert ,该方法向链表中插入一个节点。向链表中插入新节点
时,需要明确指出要在哪个节点前面或后面插入。首先介绍如何在一个已知节点后面插入
元素。
在一个已知节点后面插入元素时,先要找到“后面”的节点。为此,创建一个辅助方法
find() ,该方法遍历链表,查找给定数据。如果找到数据,该方法就返回保存该数据的节
点。 find() 方法的实现代码如下所示:*/
function find(item) {
var currNode=this.head;
while(currNode.element!=item){
currNode = currNode.next;
}
return currNode;
}
function insert(newElement, item) {
var newNode=new Node(newElement);
var current=this.find(item);
newNode.next=current.next;
current.next=newNode;
}
function display() {
var currNode=this.head;
while(!(currNode.next==null)){
console.log(currNode.next.element);
currNode=currNode.next;
}
}
function findPrevious(item) {//查询item的前一个元素
var currNode=this.head;
while(!(currNode.next==null)&&(currNode.next.element!=item)){
currNode=currNode.next;
}
return currNode;
}
function remove(item) {
var preNode=this.findPrevious(item);
if (!(preNode.next==null)){
preNode.next=preNode.next.next;
}
}
测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="Node.js"></script>
<script>
var cities = new LList();
cities.insert("AAA", "head");
cities.insert("BBB", "AAA");
cities.insert("CCC", "BBB");
cities.display();
console.log("*******测试 remove()*******");
/********测试 remove() 方法******/
var cities = new LList();
cities.insert("AAA", "head");
cities.insert("BBB", "AAA");
cities.insert("CCC", "BBB");
cities.insert("DDD", "CCC");
cities.display();
console.log("************")
cities.remove("AAA");
cities.display();
</script>
</head>
<body>
</body>
</html>
双向链表
/**
* Created by Administrator on 2016/12/10.
*/
function Node(element) {
this.element = element;
this.next = null;
this.previous = null;
}
function LList() {
this.head = new Node("head");
this.find = find;
this.insert = insert;
this.display = display;
this.remove = remove;
this.findLast = findLast;
this.dispReverse = dispReverse;
}
function dispReverse() {
var currNode = this.head;
currNode = this.findLast();
while (!(currNode.previous == null)) {
console.log(currNode.element);
currNode = currNode.previous;
}
}
//找出了链表中的最后一个节点,同时免除了从前往后遍历链表之苦:
function findLast() {
var currNode = this.head;
while (!(currNode.next == null)) {
currNode = currNode.next;
}
return currNode;
}
function remove(item) {
var currNode = this.find(item);
if (!(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
//findPrevious 没用了,注释掉
/*function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) &&
(currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}*/
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
}
测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="Node02.js"></script>
<script>
var cities = new LList();
cities.insert("AAA", "head");
cities.insert("BBB", "AAA");
cities.insert("CCC", "BBB");
cities.insert("DDD", "CCC");
cities.display();
console.log(" ");
cities.remove("CCC");
cities.display();
console.log("");
cities.dispReverse();
</script>
</head>
<body>
</body>
</html>
循环链表:
/**
* Created by Administrator on 2016/12/10.
*/
/*如果你希望可以从后向前遍历链表,但是又不想付出额外代价来创建一个双向链表,那么
就需要使用循环链表。从循环链表的尾节点向后移动,就等于从后向前遍历链表。
创建循环链表,只需要修改 LList 类的构造函数*/
function LList() {
this.head = new Node("head");
this.head.next = this.head;
this.find = find;
this.insert = insert;
this.display = display;
this.findPrevious = findPrevious;
this.remove = remove;
}
/*只需要修改一处,就将单向链表变成了循环链表。但是其他一些方法需要修改才能工作正
常。比如, display() 就需要修改,原来的方式在循环链表里会陷入死循环。 while 循环的
循环条件需要修改,需要检查头节点,当循环到头节点时退出循环。
循环链表的 display() 方法如下所示:*/
function display() {
var currNode = this.head;
while (!(currNode.next == null) &&
!(currNode.next.element == "head")) {
print(currNode.next.element);
currNode = currNode.next;
}
}
链表的其他方法
advance(n)在链表中向前移动 n 个节点。
back(n) 在双向链表中向后移动 n 个节点。
show() 只显示当前节点。