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();
    }

}

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

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解您的要求。关于您的问题,Java可以实现循环单链表和约瑟夫环。循环单链表是一种特殊的单向链表,它的尾节点指向头节点,形成一个环。它的节点包含数据和指向下一个节点的指针。约瑟夫环是一个数学问题,也可以用链表来实现。算法如下: 1. 构造循环单链表并初始化节点数,设为 n。 2. 设定一个指针,指向头节点。 3. 从头节点开始遍历,每 m 个节点删除一个节点,直到链表只剩一个节点。 4. 输出最后剩下的节点。 Java代码实现如下: //定义节点类 class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } //循环单链表类 class CircularLinkedList { private ListNode head; //头节点 private ListNode tail; //尾节点 private int size; //节点数 public CircularLinkedList() { this.head = null; this.tail = null; this.size = 0; } //添加节点 public void addNode(int val) { ListNode node = new ListNode(val); if (head == null) { head = node; tail = node; tail.next = head; } else { tail.next = node; tail = node; tail.next = head; } size++; } //约瑟夫环 public int josephus(int m) { if (size == 0) return -1; //链表为空 ListNode cur = head; while (size > 1) { for (int i = 1; i < m - 1; ++i) { cur = cur.next; } cur.next = cur.next.next; //删除第 m 个节点 size--; cur = cur.next; //指向下一个节点 } return cur.val; } } //测试代码 public class Main { public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); for (int i = 1; i <= 10; ++i) { cll.addNode(i); } System.out.println(cll.josephus(3)); //输出最后剩下的节点 } } 以上就是Java实现循环单链表和约瑟夫环的代码。希望能够帮到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值