循环链表:顾名思义就是将单链表的头和尾连起来,形成一条循环的链表,那么自然就是在单链表的基础上做一点改动,将单链表的第一个节点作为最后一个节点的下一个节点即可
package SingleList;
public class LoopNode {
int date;
LoopNode next = this;
public LoopNode(int date) {
this.date = date;
}
public void insert(LoopNode node){
LoopNode nextnext = this.next;
this.next = node;
node.next = nextnext;
}
public void remove(){
LoopNode newnext = next.next;
this.next = newnext;
}
public int getDate(){
return this.date;
}
}
双链表也是一个循环链表,不同的是他是一个双向的链表,所以他有两个指针,分别指向前一个节点和后一个节点,当他只有一个节点的时候,那么他的钱一个节点和后一个节点都是他自己。如图:
代码这里只展示增加,其他的就不展示了
package SingleList;
public class DoubleNode {
int date;
DoubleNode pre = this;
DoubleNode next = this;
public DoubleNode(int date) {
this.date = date;
}
public void insert(DoubleNode node){
DoubleNode nextnext = this.next;
this.next = node;
node.pre = this;
node.next = nextnext;
nextnext.pre = node;
}
public int getDate() {
return this.date;
}
}