Search code examples
javaalgorithmdata-structureslinked-listcircular-list

How to get length and isEmpty methods as a return value to get method used for my Circular Linked List?


There is a problem I came across when I have seen two methods never used in the Linked List: New Update!!!

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

I try to fix this problem for my Circular Linked List by trying to void the method. However, I want to test and see if I can get the method working by printing out the value for the length and isEmpty. Is there a way to work around this issue on IntelliJ?

image

Return value of the method is never used for both length and isEmpty


Solution

  • It's just a warning, that some other code is calling the method but ignoring the result.

    The solution is to fix that other code.