链式结构 node
建立LinkedList,而且链表包含如下方法。数组
function LinkedList() {
let length = 0, // 链表的长度
head = null; // 链表的头指针
// 辅助类
let Node = function(element) {
this.element = element;
this.next = null;
}
// 向链表尾部添加指定元素
this.append = function(element) {
let node = new Node(element),
current = '';
if (head === null) {
// 链表为空
head = node;
} else {
// 链表不为空,迭代到链表最后一项,最后一项的指针等于node
current = head;
while (current.next) {
current = current.next;
}
current.next = node;
}
length++;
};
// 从链表移除指定位置的元素
this.removeAt = function(position) {
// 检查是否越界
if (position > -1 && position < length) {
let current = head,
previous = '',
index = 0;
if (position === 0) {
head = current.next;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
// 将previous与current的下一项连接起来,跳过current,从而移除它
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
// 在任意位置插入元素
this.insert = function(position, element) {
// 检查是否越界
if (position > -1 && position < length) {
let node = new Node(element),
current = head,
previous = '',
index = 0;
if (position === 0) {
// 在第一个位置添加
head = node;
node.next = current;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = node;
node.next = current;
}
length++;
return true;
} else {
return false;
}
};
// 返回指定项的索引
this.indexOf = function(element) {
let current = head,
index = 0;
while (current) {
if (element === current.element) {
return index;
} else {
index++;
current = current.next;
}
}
};
// 删除指定元素
this.remove = function(element) {
let index = this.indexOf(element);
return this.removeAt(index, element);
};
// 链表是否为空
this.isEmpty = function() {
return length === 0;
};
// 链表长度
this.size = function() {
return length;
};
// 把链表转换为一个字符串
this.toString = function() {
let current = head,
string = '';
while(current) {
string += current.element + (current.next ? 'n' : '');
current = current.next;
}
return string;
};
}
复制代码
// 向链表添加一项bash
链表相对传统数组优势是:添加删除元素不会移动其它元素。数据结构
链表相对传统数组缺点是:访问链表中间的元素,须要从头迭代,直到找到所需元素。app
下一篇文章:双向链表、循环链表ui