java实现循环单链表

前面已经介绍了java实现单链表
对于循环链表而言,关键要素是指定链表的头节点head、尾节点tail以及链表大小size;该数据结构支持在头部增加节点、在尾部增加节点,从头部删除节点及从尾部删除节点等。
其实两者的主要差别就在于如何判断是否到了链表的结尾:
在单链表中

while(temp.next!=null)
{
    temp=temp.next;
}

在循环链表中

while(temp.next!=header)
{
    temp=temp.next;
}

下面是循环链表的代码和测试代码:
SNode.java

package list;

public class SNode {
    public int data;// 数据区
    public SNode next;// 指针区

    public SNode(int data, SNode next) {
        this.data = data;
        this.next = next;
    }

    public SNode(int i) {
        this(i, null);
    }

    public SNode() {
        this(0, null);
    }

    public void setData(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setNext(SNode next) {
        this.next = next;

    }

    public SNode getNext() {
        return next;
    }

}

CircularLinkedList.java

package list;

public class CircularLinkedList {

    private SNode head;// 头结点
    private SNode tail;
    public int count;// 记录节点的长度

    public CircularLinkedList() { // 构造函数用来初始化
        tail = head = null;
        count = 0;
    }

    // 在链表头部增加节点
    public void addHead(SNode hd) {
        // 如果使用该方法增加链表的第一个节点,则head=tail=hd,且next指向自身
        if (count == 0) {
            hd.setNext(hd);
            tail = head = hd;
        } else {
            tail.setNext(hd);
            hd.setNext(head);
            head = hd;
        }
        count++;
    }

    // 在链表尾部增加节点
    public void addTail(SNode tl) {
        // 如果使用该方法增加链表的第一个节点,则tail=head=hd,且next指向自身
        if (count == 0) {
            tl.setNext(tl);
            tail = head = tl;
        } else {
            tail.setNext(tl);
            tl.setNext(head);
            tail = tl;
        }
        count++;
    }

    // 删除头部节点,被删掉的head将被自动回收
    public void delHead() {
        if (count > 1) {
            head = head.getNext();
            tail.setNext(head);
            count--;
        } else if (count == 1) {
            head = tail = null;
            count--;
        } else {
            System.out.println("There is no elements in the Circularlinked list.");
        }

    }

    // 打印全部节点
    public void printList() {
        SNode nd = new SNode();
        nd = head;
        try {
            while (nd.getNext() != head) {
                System.out.print(nd.getData());
                System.out.print("->");
                nd = nd.getNext();
            }
            System.out.print(nd.getData());
            System.out.print("->");
            System.out.print(head.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

测试类
CircularLinkedlstTest.java

package list;

public class CircularLinkedlstTest {

    public static void main(String[] args) {
        CircularLinkedList lst = new CircularLinkedList();

        SNode head = new SNode(11, null);
        lst.addHead(head);
        lst.addTail(new SNode(22));
        lst.addTail(new SNode(33));
        System.out.println("打印链表:");
        lst.printList();
        System.out.println();
        System.out.println("删除首端节点:");
        lst.delHead();
        lst.printList();
        System.out.println();
        System.out.println("删除首端节点:");
        lst.delHead();
        lst.printList();
    }

}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37941483/article/details/89299508