I'm having trouble getting my linked list to print the sole entry that is inside of it. It's supposed to print "Yellow", but instead returns empty. I tried seeing if it was an issue with the for each loop seeing if there was an entry there at all and it seems like that's the case. In other words, I tried seeing if the loop would run at all, and it didn't. At this point, I'm not really sure what the problem is so any help would be greatly appreciated.
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);
}
}
}
And this is the code for my basicLinkedList class at the moment:
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;
}
}
}
CodePudding user response:
Update the next
method in the Iterator
implementation to retrieve from the First
node then return the data.
, Update remove
method to break after removing, and update retrieveFirstElement
and 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;
}
}
}
, here are the implementations of retrieveFirstElement
and retrieveLastElement
if you want just to retrieve and keep elements
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;
}
output
First: Blue
Last: Red
Size: 3
Retrieve First: Blue
Retrieve Last: Red
Removing Red
trace: Yellow
Size: 1
Iteration: Yellow