[英]How to get length and isEmpty methods as a return value to get method used for my Circular Linked List?
当我看到链表中从未使用过的两种方法时,我遇到了一个问题:新更新!!!
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;
}
}
我尝试通过尝试使方法无效来为我的循环链表解决此问题。 但是,我想测试一下是否可以通过打印出长度和 isEmpty 的值来使该方法正常工作。 有没有办法在 IntelliJ 上解决这个问题?
该方法的返回值从不用于 length 和 isEmpty
这只是一个警告,其他一些代码正在调用该方法但忽略了结果。
解决方案是修复其他代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.