循环链表和单向链表相似,节点类型都是一样的。唯一的区别是,在创建循环链表时,让其头节点的next属性指向它本身,即 head.next = head;
如果你希望可以从后向前遍历链表,但是又不想付出额外代价来创建一个双向链表,那么就需要使用循环链表。从循环链表的尾节点向后移动,就等于从后向前遍历链表。
修改构造函数如下:
function LList() {
this.head = new Node("head");
<strong> this.head.next = this.head;</strong>
this.find = find;
this.insert = insert;
this.display = display;
this.findPrevious = findPrevious;
this.remove = remove;
}
display方法如下:
function display() {
var currNode = this.head;
while (!(currNode.next == null) &&!(currNode.next.element == "head")) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}