I am trying to implement a toString() method that returns the values in my singly-linked list in a string format by iterating through the list. See below for the SinglyLinkedList.java file which contains the toString() method.
import java.util.NoSuchElementException;
import java.util.*;
/**
* Your implementation of a Singly-Linked List.
*/
public class SinglyLinkedList<T> {
/*
* Do not add new instance variables or modify existing ones.
*/
private SinglyLinkedListNode<T> head;
private SinglyLinkedListNode<T> tail;
private int size;
/*
* Do not add a constructor.
*/
/**
* Adds the element to the front of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the front of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToFront(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
SinglyLinkedListNode<T> newNode = new SinglyLinkedListNode<T>(data);
if (head == null){
head = newNode;
tail = newNode;
}
else{
newNode.setNext(head);
head = newNode;
}
size++;
}
/**
* Adds the element to the back of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the back of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToBack(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
if (head == null){
head = new SinglyLinkedListNode<T>(data);
}
else {
SinglyLinkedListNode<T> current = head;
while (current.getNext() != null){
current = current.getNext();
}
current.setNext(new SinglyLinkedListNode<T>(data));
}
size++;
}
/**
* Removes and returns the first data of the list.
*
* Method should run in O(1) time.
*
* @return the data formerly located at the front of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromFront() {
if (head == null){
throw new NoSuchElementException("You cannot remove elements from the front of an empty list");
}
SinglyLinkedListNode<T> var = head;
head = head.getNext();
size--;
return var.getData();
}
/**
* Removes and returns the last data of the list.
*
* Method should run in O(n) time.
*
* @return the data formerly located at the back of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromBack() {
if (head == null){
throw new NoSuchElementException("You cannot remove an element from the back of an empty list");
}
else if (head.getNext() == null){
SinglyLinkedListNode<T> var = head;
head = null;
}
SinglyLinkedListNode<T> current = head;
while (current.getNext().getNext() != null){
current = current.getNext();
}
SinglyLinkedListNode<T> var = current.getNext();
current.setNext(null);
size--;
return var.getData();
}
/**
* Returns the head node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the head of the list
*/
public SinglyLinkedListNode<T> getHead() {
// DO NOT MODIFY THIS METHOD!
return head;
}
/**
* Returns the tail node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the tail of the list
*/
public SinglyLinkedListNode<T> getTail() {
// DO NOT MODIFY THIS METHOD!
return tail;
}
/**
* Returns the size of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the size of the list
*/
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
public String toString() {
String answer = "";
SinglyLinkedListNode<T> current = head;
while (current != null){
answer += current + " ";
current = current.getNext();
}
return answer;
}
public static void main(String[] args){
SinglyLinkedList<Integer> list = new SinglyLinkedList<>();
list.addToFront(1);
System.out.println(list.toString());
}
}
And for my SinglyLinkedListNode.java file...
/**
* Node class used for implementing the SinglyLinkedList.
*
* DO NOT MODIFY THIS FILE!!
*
* @author CS 1332 TAs
* @version 1.0
*/
public class SinglyLinkedListNode<T> {
private T data;
private SinglyLinkedListNode<T> next;
/**
* Constructs a new SinglyLinkedListNode with the given data and next node
* reference.
*
* @param data the data stored in the new node
* @param next the next node in the list
*/
SinglyLinkedListNode(T data, SinglyLinkedListNode<T> next) {
this.data = data;
this.next = next;
}
/**
* Creates a new SinglyLinkedListNode with only the given data.
*
* @param data the data stored in the new node
*/
SinglyLinkedListNode(T data) {
this(data, null);
}
/**
* Gets the data.
*
* @return the data
*/
T getData() {
return data;
}
/**
* Gets the next node.
*
* @return the next node
*/
SinglyLinkedListNode<T> getNext() {
return next;
}
/**
* Sets the next node.
*
* @param next the new next node
*/
void setNext(SinglyLinkedListNode<T> next) {
this.next = next;
}
}
Whenever I call the toString() method from my SinglyLinkedList class, I get the following printed in my command prompt.
Does anyone know why I do not see the value "1" which I am adding to the front of the linkedlist prior to calling the toString() method? And if so, what can I do to fix it? Instead, it keeps printing the node object.
The problem is you have an object 'current' of type SinglyLinkedList whose toString method is not overridden.
public String toString() {
String answer = "";
SinglyLinkedListNode<T> current = head;
while (current != null){
answer += current + " ";
current = current.getNext();
}
return answer;
}
If you cannot override toString method in SinglyLinkedListNode class then you can modify the while loop to:
while(current != null){
answer += current.getData() + " ";
current = current.getNext();
}
Assuming your T has appropriate toString() method overridden.
To make the code more efficient you can use StringBuilder rather than concatenating strings.
public String toString() {
//String answer = " ";
StringBuilder answer = new StringBuilder();
SinglyLinkedListNode<T> current = head;
while (current != null){
//answer += current + " ";
answer.append(current.getData());
answer.append(" ");
current = current.getNext();
}
return answer.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.