微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取长度和isEmpty方法作为返回值,以获取用于我的循环链表的方法?

如何解决如何获取长度和isEmpty方法作为返回值,以获取用于我的循环链表的方法?

当我看到链表中从未使用过的两种方法时遇到一个问题: 新更新!!!

public class CircularLinkedList {

    private ListNode last;
    private int length;

    private static class ListNode {
        private ListNode next;
        private final int data;

        public ListNode(int data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        CircularLinkedList insertBeg = new CircularLinkedList();
        CircularLinkedList insertEnd = new CircularLinkedList();

        System.out.println("creating a traverse list");
        cll.createCircularLinkedList();
        cll.display();
        System.out.println("Insert Starting Node");
        insertBeg.insertFirst(10);
        insertBeg.insertFirst(15);
        insertBeg.insertFirst(20);
        insertBeg.insertFirst(25);
        insertBeg.display();

        insertEnd.insertLast(25);
        insertEnd.insertLast(20);
        insertEnd.insertLast(15);
        insertEnd.insertLast(10);

        // I just need to print out the isEmpty amd length
        System.out.println(insertEnd.isEmpty());
        System.out.println(insertEnd.length());
        insertEnd.display();
    }

    public CircularLinkedList () {
        last = null;
        length = 0;
    }

    public void display() {
        if(last == null) {
            return;
        }

        ListNode first = last.next;
        while(first != last) {
            System.out.print(first.data + " --> ");
            first = first.next;
        }
        System.out.println(first.data);
    }

    public void insertFirst(int data) {
        ListNode temp = new ListNode(data);
        if (last == null) {
            last = temp;
        } else {
            temp.next = last.next;
        }
        last.next = temp;
        length++;
    }

    public void insertLast(int data) {
        ListNode temp = new ListNode(data);
        if (last == null) {
            last = temp;
            last.next = temp;
        } else {
            temp.next = last.next;
            last.next = temp;
            last = temp;
        }
        length++;
    }

    public int length () {
        ListNode temp = last.next;
        int count = length;
        while (temp != last) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    public boolean isEmpty(){
        return length == 0;
    }

    public void createCircularLinkedList() {
        ListNode first = new ListNode(1);
        ListNode second = new ListNode(5);
        ListNode third = new ListNode(10);
        ListNode fourth = new ListNode(15);

        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = first;

        last = fourth;
    }
}

我尝试通过使该方法无效来为我的循环链表解决此问题。但是,我想测试一下,看看是否可以通过打印出length和isEmpty的值来使该方法正常工作。可以在IntelliJ上解决此问题吗?

image

方法的返回值从不用于长度和isEmpty

解决方法

这只是一个警告,其他代码正在调用该方法但忽略了结果。

解决方案是修复其他代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。