我有麻烦让我的链表打印唯一的条目是在它里面。它应该打印"Yellow",但却返回空。我试着看看这是否是for each循环的问题,看看那里是否有一个条目,似乎是这样的。换句话说,我试着看看这个循环是否能运行,但它不能运行。在这一点上,我真的不知道是什么问题,所以任何帮助将非常感谢。
public class SampleDriver {
public static void main(String[] args) {
BasicLinkedList<String> basicList = new BasicLinkedList<>();
basicList.addToEnd("Red").addToFront("Yellow").addToFront("Blue");
System.out.println("First: " + basicList.getFirst());
System.out.println("Last: " + basicList.getLast());
System.out.println("Size: " + basicList.getSize());
System.out.println("Retrieve First: " + basicList.retrieveFirstElement());
System.out.println("Retrieve Last: " + basicList.retrieveLastElement());
System.out.println("Removing Red");
basicList.remove("Red", String.CASE_INSENSITIVE_ORDER);
System.out.print("Iteration: ");
for (String entry : basicList) {
System.out.print(entry);
}
}
}
现在这是我的basicLinkedList类的代码:
public class BasicLinkedList < T > implements Iterable < T > {
public BasicLinkedList() {
head = null;
tail = null;
listSize = 0;
}
public BasicLinkedList < T > addToEnd(T data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
listSize++;
return this;
}
public BasicLinkedList < T > addToFront(T data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
listSize++;
return this;
}
public T getFirst() {
if (head == null) {
return null;
}
return head.data;
}
public T getLast() {
if (tail == null) {
return null;
}
return tail.data;
}
public BasicLinkedList < T > remove(T targetData, java.util.Comparator < T > comparator) {
Node previousNode = null;
Node currentNode = head;
while (currentNode != null) {
if (comparator.compare(currentNode.data, targetData) == 0) {
//this is for the first entry specifically
if (currentNode == head) {
head = head.next;
currentNode = head;
}
//this is for the last entry
else if (currentNode == tail) {
currentNode = null;
tail = previousNode;
previousNode.next = null;
} else {
previousNode.next = currentNode.next;
currentNode = currentNode.next;
}
listSize--;
} else {
previousNode = currentNode;
currentNode = currentNode.next;
}
}
return this;
}
public T retrieveFirstElement() {
if (head == null) {
return null;
}
Node firstElement = head;
Node currentNode = head.next;
head = currentNode;
listSize--;
return firstElement.data;
}
public T retrieveLastElement() {
Node currentNode = head;
Node previousNode = head;
if (head == null) {
return null;
} else {
if (head.next == null) {
currentNode = head;
head = null;
} else {
while (currentNode.next != null) {
previousNode = currentNode;
currentNode = currentNode.next;
}
tail = previousNode;
tail.next = null;
}
}
listSize--;
return currentNode.data;
}
public int getSize() {
return listSize;
}
/* Node definition */
protected class Node {
protected T data;
protected Node next;
protected Node(T data) {
this.data = data;
next = null;
}
}
/* We have both head and tail */
protected Node head,
tail;
/* size */
protected int listSize;
@Override
public Iterator < T > iterator() {
return new IteratorForLinkedList();
}
private class IteratorForLinkedList implements Iterator < T > {
Node nextNode;
@Override
public boolean hasNext() {
return nextNode != null;
}
@Override
public T next() {
// TODO Auto-generated method stub
return null;
}
}
}
更新Iterator
实现中的next
方法,从First
节点检索,然后返回数据。
,更新remove
方法使其拆除后破碎,并更新retrieveFirstElement
和retrieveLastElement
class BasicLinkedList<T> implements Iterable<T> {
public BasicLinkedList() {
head = null;
tail = null;
listSize = 0;
}
public BasicLinkedList<T> addToEnd(T data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
listSize++;
return this;
}
public BasicLinkedList<T> addToFront(T data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
listSize++;
return this;
}
public T getFirst() {
if (head == null) {
return null;
}
return head.data;
}
public T getLast() {
if (tail == null) {
return null;
}
return tail.data;
}
public BasicLinkedList<T> remove(T targetData, java.util.Comparator<T> comparator) {
Node previousNode = null;
Node currentNode = head;
while (currentNode != null) {
if (comparator.compare(currentNode.data, targetData) == 0) {
//this is for the first entry specifically
if (currentNode == head) {
head = head.next;
}
//this is for the last entry
else if (currentNode == tail) {
tail = previousNode;
previousNode.next = null;
} else {
previousNode.next = currentNode.next;
}
listSize--;
break;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
return this;
}
public T retrieveFirstElement() {
if (head == null) {
return null;
}
Node firstElement = head;
head = head.next;
listSize--;
return firstElement.data;
}
public T retrieveLastElement() {
Node currentNode = head;
T data = null;
if (tail == null) {
return null;
} else {
while (currentNode != null && currentNode.next != tail) {
currentNode = currentNode.next;
}
data = tail.data;
tail = currentNode;
tail.next = null;
}
listSize--;
return data;
}
public int getSize() {
return listSize;
}
/* Node definition */
protected class Node {
protected T data;
protected Node next;
protected Node(T data) {
this.data = data;
next = null;
}
}
/* We have both head and tail */
protected Node head, tail;
/* size */
protected int listSize;
@Override
public Iterator<T> iterator() {
return new IteratorForLinkedList();
}
private class IteratorForLinkedList implements Iterator<T> {
Node first = head;
@Override
public boolean hasNext() {
return first != null;
}
@Override
public T next() {
Node oldFirst = first;
T item = oldFirst.data;
first = first.next;
return item;
}
}
}
,这里是retrieveFirstElement
和retrieveLastElement
的实现,如果你只想检索和保存元素
public T retrieveFirstElement() {
if (head == null) {
return null;
}
Node firstElement = head;
return firstElement.data;
}
public T retrieveLastElement() {
if (tail == null) {
return null;
}
return tail.data;
}
First: Blue
Last: Red
Size: 3
Retrieve First: Blue
Retrieve Last: Red
Removing Red
trace: Yellow
Size: 1
Iteration: Yellow