定义:
单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可。
带头结点
判断空链表的条件是head == head.next;
带尾节点
用尾节点rear表示的单循环链表对开始结点a1和终端结点an查找时间都是O(1)。而表的操作常常是在表的首尾位置上进行,因此,实用中多采用尾指针表示单循环链表。带尾指针的单循环链表可见下图。
注意:判断空链表的条件为rear==rear.next;
循环链表的运算与单链表的运算基本一致。所不同的有以下几点:
1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。
2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。
单循环链表的实现:package com.ds.link;
public class CircularLinkedList{
private Node head;
private int length; //the length of the list
private class Node {
private Node next;
private T data;
public Node(T data) {
this.data = data;
next = null;
}
public Node(T data, Node nextNode) {
this.data = data;
next = nextNode;
}
public void setData(T data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public T getData() {
return data;
}
public Node getNext() {
return next;
}
}
public CircularLinkedList() {
head = new Node(null, head);
length = 0;
}
public void insertAtPrior(T item) {//在头节点之后加一个节点
Node node = new Node(item, null); //encpsule the item to an node
node.setNext(head.getNext()); //node.next = head.next
head.setNext(node); //head.next = node
length ++;
}
public void add(T item) {//在最后节点上添加一个节点
Node tmp = head;
if (isEmpty()) { // if the list is null
Node node = new Node(item, head); // .. if next == null ?
head.setNext(node);
} else {
Node node = new Node(item, head);
// find the end node of the list
while (head != tmp.getNext()) {
tmp = tmp.getNext();
}
tmp.setNext(node);
}
length++;
}
public T get(int index) { //获取索引处的节点的数据
// 先判断索引正确性
if (index >length || index < 0) {
throw new RuntimeException("索引值有错:" + index);
}else if(isEmpty()){
return null;
}else{
Node tmp =head;
int i= 1;
while (head != tmp.getNext() && i <= index) {
tmp = tmp.getNext();
i++;
}
T e = tmp.getData();
return e;
}
}
public void insert(int index, T item) {//在索引处后添加节点
Node node = new Node(item, null);
Node tmp = head;
int i = 1;
if (index > length || index < 0) {
System.out.println("the index is out of bounds");
} else if (0 == length && 1 == index) {
node.setNext(head);
head.setNext(node);
length++;
} else {
//find the node index
while (head != tmp.getNext() && i <= index) {
tmp = tmp.getNext();
i++;
}
node.setNext(tmp.getNext());
tmp.setNext(node);
length++;
}
}
public void removeFromFront() {//删除头节点之后的第一个节点
Node tmp = head;
if (length < 1) {
System.out.println("The list is null and you can not delete any node!");
} else if (1 == length) {
head.setNext(head);
length--;
} else {
head.setNext(tmp.getNext().getNext());
length--;
}
}
public void remove(int index) {//删除索引处的节点
if (length < 1 || index > length) {
System.out.println("index is out of bounds");
} else if (1 == length && 1 == index) {
head.setNext(head);
length--;
} else {
Node tmp = head;
int i = 1;
//get the node before index
while (head != tmp.getNext() && i < index) {
tmp = tmp.getNext();
i++;
}
tmp.setNext(tmp.getNext().getNext());
length--;
}
}
public void removeFromLast() {//删除最后一个节点
if (length < 1) { // if the list is null
System.out.println("The list is null and you can not delete");
} else if (1 == length) {
head.setNext(head);
length--;
} else {
Node tmp1 = head;
Node tmp2 = head.getNext(); //set tmp2 -tmp1 = 1
while (head != tmp2.getNext()) {
tmp2 = tmp2.getNext();
tmp1 = tmp1.getNext();
}
tmp1.setNext(head);
length--;
}
}
public int getLength() {
return length;
}
public boolean isEmpty() {
return length==0;
}
public void display() {
if (length < 1) {
System.out.println("The list is null");
} else {
Node tmp = head;
while (head != tmp.getNext()) {
tmp = tmp.getNext();
System.out.print(tmp.getData() + " ");
}
}
}
//test the list
public static void main(String[] args) {
CircularLinkedList< Integer> l = new CircularLinkedList< Integer>();
System.out.println(l.isEmpty());
l.add(1);
l.add(2);
l.insertAtPrior(3);
l.insert(2, 4);
l.add(5);
System.out.println("the list is : ");
l.display();
System.out.println();
System.out.println("the length is :" + l.getLength());
l.removeFromFront();
l.removeFromLast();
l.display();
// System.out.println(l.get(3));
}
}
结果输出:true
the list is :
3 1 4 2 5
the length is :5
1 4 2
如果存在问题,我们随时交流!