循环链表是一种特殊的链表。它跟单链表的区别在于,循环链表的尾节点指针指向的是链表开头的节点。
循环链表的优点在于,从链表尾部到链表头部比较方便。所以,当我们需要处理的数据有类似于环状的结构,我们可以使用循环链表。
接下来我们了解下循环链表的结构

接下来我们看一下循环链表的定义
function Node(element)
{
this.element=element;
this.next=next;
}
function recycleList()
{
this.head=new Node("head");
this.head.next=this.head;
this.find=find;
this.findLast=findLast;
this.insert=insert;
this.remove=remove;
this.display=display;
}
除了display方法之外,其他方法和之前的单链表一样。
function display()
{
var curNode=this.head;
while(curNode.next!=null&&curNode.next.element!="head")
{
console.log(curNode.next.element);
curNode=curNode.next;
}
}
网友评论