How to insert a string in a sorted linked list in java?
I'm trying to make a sorted linkedlist. The addElement and removeElement methods works almost as they should except that the addElement method don't always insert the elements in the correct order.
Here's my code so far:
public class SortedLinkedList {
// Node class
class Node {
public String data;
public Node next;
}
// attributes
private Node first;
private int size;
public SortedLinkedList() {
first = null;
}
public void addElement(String element) {
if (first == null) {
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
first = newNode;
size++;
} else if (first.toString().compareTo(element) > 0) {
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
size++;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
Node newNode = new Node();
newNode.data = element;
if (after.toString().compareTo(element) > 0) {
before.next = newNode;
newNode.next = after;
}
before = after;
after = after.next;
}
size++;
}
}
public boolean removeElement(String element) {
boolean removed = false;
try {
if (first.data.toString().equals(element)) {
first = first.next;
removed = true;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
if (after.data.toString().compareTo(element) == 0) {
before.next = after.next;
break;
}
before = after;
after = after.next;
}
size--;
removed = true;
}
} catch (NullPointerException e) {
removed = false;
}
return removed;
}
public int countElements() {
return size;
}
@Override
public String toString() {
String result = "{";
Node current = this.first;
while (current != null) {
if (current.next != null) {
result = result + current.data.toString() + ", ";
} else {
result = result + current.data.toString();
}
current = current.next;
}
result = result + "}";
return result;
}
For example, I have tried to insert the values: 'b, a, d, c' and the output is: 'c, d, a, b' but it should be 'a, b, c, d'.
I really can't see what I'm doing wrong.
Any help would be appreciated!
java
add a comment |
I'm trying to make a sorted linkedlist. The addElement and removeElement methods works almost as they should except that the addElement method don't always insert the elements in the correct order.
Here's my code so far:
public class SortedLinkedList {
// Node class
class Node {
public String data;
public Node next;
}
// attributes
private Node first;
private int size;
public SortedLinkedList() {
first = null;
}
public void addElement(String element) {
if (first == null) {
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
first = newNode;
size++;
} else if (first.toString().compareTo(element) > 0) {
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
size++;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
Node newNode = new Node();
newNode.data = element;
if (after.toString().compareTo(element) > 0) {
before.next = newNode;
newNode.next = after;
}
before = after;
after = after.next;
}
size++;
}
}
public boolean removeElement(String element) {
boolean removed = false;
try {
if (first.data.toString().equals(element)) {
first = first.next;
removed = true;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
if (after.data.toString().compareTo(element) == 0) {
before.next = after.next;
break;
}
before = after;
after = after.next;
}
size--;
removed = true;
}
} catch (NullPointerException e) {
removed = false;
}
return removed;
}
public int countElements() {
return size;
}
@Override
public String toString() {
String result = "{";
Node current = this.first;
while (current != null) {
if (current.next != null) {
result = result + current.data.toString() + ", ";
} else {
result = result + current.data.toString();
}
current = current.next;
}
result = result + "}";
return result;
}
For example, I have tried to insert the values: 'b, a, d, c' and the output is: 'c, d, a, b' but it should be 'a, b, c, d'.
I really can't see what I'm doing wrong.
Any help would be appreciated!
java
You need to implement atoString()method in the classNodeor directly access it with.datai.e.first.data.compareTo(element).
– Turamarth
Nov 12 at 14:21
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
1
You shouldn't be usingtoStringat all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should usefirst.data.
– RealSkeptic
Nov 12 at 14:27
add a comment |
I'm trying to make a sorted linkedlist. The addElement and removeElement methods works almost as they should except that the addElement method don't always insert the elements in the correct order.
Here's my code so far:
public class SortedLinkedList {
// Node class
class Node {
public String data;
public Node next;
}
// attributes
private Node first;
private int size;
public SortedLinkedList() {
first = null;
}
public void addElement(String element) {
if (first == null) {
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
first = newNode;
size++;
} else if (first.toString().compareTo(element) > 0) {
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
size++;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
Node newNode = new Node();
newNode.data = element;
if (after.toString().compareTo(element) > 0) {
before.next = newNode;
newNode.next = after;
}
before = after;
after = after.next;
}
size++;
}
}
public boolean removeElement(String element) {
boolean removed = false;
try {
if (first.data.toString().equals(element)) {
first = first.next;
removed = true;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
if (after.data.toString().compareTo(element) == 0) {
before.next = after.next;
break;
}
before = after;
after = after.next;
}
size--;
removed = true;
}
} catch (NullPointerException e) {
removed = false;
}
return removed;
}
public int countElements() {
return size;
}
@Override
public String toString() {
String result = "{";
Node current = this.first;
while (current != null) {
if (current.next != null) {
result = result + current.data.toString() + ", ";
} else {
result = result + current.data.toString();
}
current = current.next;
}
result = result + "}";
return result;
}
For example, I have tried to insert the values: 'b, a, d, c' and the output is: 'c, d, a, b' but it should be 'a, b, c, d'.
I really can't see what I'm doing wrong.
Any help would be appreciated!
java
I'm trying to make a sorted linkedlist. The addElement and removeElement methods works almost as they should except that the addElement method don't always insert the elements in the correct order.
Here's my code so far:
public class SortedLinkedList {
// Node class
class Node {
public String data;
public Node next;
}
// attributes
private Node first;
private int size;
public SortedLinkedList() {
first = null;
}
public void addElement(String element) {
if (first == null) {
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
first = newNode;
size++;
} else if (first.toString().compareTo(element) > 0) {
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
first = newNode;
size++;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
Node newNode = new Node();
newNode.data = element;
if (after.toString().compareTo(element) > 0) {
before.next = newNode;
newNode.next = after;
}
before = after;
after = after.next;
}
size++;
}
}
public boolean removeElement(String element) {
boolean removed = false;
try {
if (first.data.toString().equals(element)) {
first = first.next;
removed = true;
} else {
Node before = first;
Node after = first.next;
while (first.next != null) {
if (after.data.toString().compareTo(element) == 0) {
before.next = after.next;
break;
}
before = after;
after = after.next;
}
size--;
removed = true;
}
} catch (NullPointerException e) {
removed = false;
}
return removed;
}
public int countElements() {
return size;
}
@Override
public String toString() {
String result = "{";
Node current = this.first;
while (current != null) {
if (current.next != null) {
result = result + current.data.toString() + ", ";
} else {
result = result + current.data.toString();
}
current = current.next;
}
result = result + "}";
return result;
}
For example, I have tried to insert the values: 'b, a, d, c' and the output is: 'c, d, a, b' but it should be 'a, b, c, d'.
I really can't see what I'm doing wrong.
Any help would be appreciated!
java
java
edited Nov 12 at 14:25
asked Nov 12 at 14:17
user3740970
150117
150117
You need to implement atoString()method in the classNodeor directly access it with.datai.e.first.data.compareTo(element).
– Turamarth
Nov 12 at 14:21
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
1
You shouldn't be usingtoStringat all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should usefirst.data.
– RealSkeptic
Nov 12 at 14:27
add a comment |
You need to implement atoString()method in the classNodeor directly access it with.datai.e.first.data.compareTo(element).
– Turamarth
Nov 12 at 14:21
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
1
You shouldn't be usingtoStringat all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should usefirst.data.
– RealSkeptic
Nov 12 at 14:27
You need to implement a
toString()method in the class Node or directly access it with .data i.e. first.data.compareTo(element).– Turamarth
Nov 12 at 14:21
You need to implement a
toString()method in the class Node or directly access it with .data i.e. first.data.compareTo(element).– Turamarth
Nov 12 at 14:21
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
1
1
You shouldn't be using
toString at all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should use first.data.– RealSkeptic
Nov 12 at 14:27
You shouldn't be using
toString at all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should use first.data.– RealSkeptic
Nov 12 at 14:27
add a comment |
2 Answers
2
active
oldest
votes
You have various small issues.
- You are comparing the
Nodeinstead of theNode's data. - Your while loop is looping
while (first.next != null). Shouldn't this be checkingbeforeorafter? - Why are you creating the new
Nodeinside thewhileloop?
You could change the Node like this:
class Node implements Comparable<Node> {
private final String data; //once initialised shouldn't change
private Node next; //this can change
public Node(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int compareTo(Node that) {
return this.data.compareTo(that.data);
}
}
In your addElement(String element) I would immediately do the following before the if conditions, it is all the same:
Node newNode = new Node(element);
size++;
Your while loop in the last case can then be:
Node after = first;
while ((after != null) && (after.compareTo(newNode) <= 0)) {
//we traverse to the next
before = after;
after = after.getNext();
}
//either we found the spot, or it is the last element in the list
before.setNext(newNode);
newNode.setNext(after);
You can probably also include the case of when the new node should be inserted at the head of the list too... but I leave it to you.
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
add a comment |
If you want a sorted linked list, you can use the normal LinkedList library and call List#sort(Comparator) on it.
For example, if we want to sort Strings by alphabetical order:
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list); // ["b","d","c","a"]
list.sort(new Comparator<String>() {
//This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
@Override
public int compare(String s1, String s2){
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(list); // ["a","b","c","d"]
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use aLinkedListbut something like aPriorityQueue, which is much more efficient.
– jbx
Nov 12 at 14:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264071%2fhow-to-insert-a-string-in-a-sorted-linked-list-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have various small issues.
- You are comparing the
Nodeinstead of theNode's data. - Your while loop is looping
while (first.next != null). Shouldn't this be checkingbeforeorafter? - Why are you creating the new
Nodeinside thewhileloop?
You could change the Node like this:
class Node implements Comparable<Node> {
private final String data; //once initialised shouldn't change
private Node next; //this can change
public Node(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int compareTo(Node that) {
return this.data.compareTo(that.data);
}
}
In your addElement(String element) I would immediately do the following before the if conditions, it is all the same:
Node newNode = new Node(element);
size++;
Your while loop in the last case can then be:
Node after = first;
while ((after != null) && (after.compareTo(newNode) <= 0)) {
//we traverse to the next
before = after;
after = after.getNext();
}
//either we found the spot, or it is the last element in the list
before.setNext(newNode);
newNode.setNext(after);
You can probably also include the case of when the new node should be inserted at the head of the list too... but I leave it to you.
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
add a comment |
You have various small issues.
- You are comparing the
Nodeinstead of theNode's data. - Your while loop is looping
while (first.next != null). Shouldn't this be checkingbeforeorafter? - Why are you creating the new
Nodeinside thewhileloop?
You could change the Node like this:
class Node implements Comparable<Node> {
private final String data; //once initialised shouldn't change
private Node next; //this can change
public Node(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int compareTo(Node that) {
return this.data.compareTo(that.data);
}
}
In your addElement(String element) I would immediately do the following before the if conditions, it is all the same:
Node newNode = new Node(element);
size++;
Your while loop in the last case can then be:
Node after = first;
while ((after != null) && (after.compareTo(newNode) <= 0)) {
//we traverse to the next
before = after;
after = after.getNext();
}
//either we found the spot, or it is the last element in the list
before.setNext(newNode);
newNode.setNext(after);
You can probably also include the case of when the new node should be inserted at the head of the list too... but I leave it to you.
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
add a comment |
You have various small issues.
- You are comparing the
Nodeinstead of theNode's data. - Your while loop is looping
while (first.next != null). Shouldn't this be checkingbeforeorafter? - Why are you creating the new
Nodeinside thewhileloop?
You could change the Node like this:
class Node implements Comparable<Node> {
private final String data; //once initialised shouldn't change
private Node next; //this can change
public Node(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int compareTo(Node that) {
return this.data.compareTo(that.data);
}
}
In your addElement(String element) I would immediately do the following before the if conditions, it is all the same:
Node newNode = new Node(element);
size++;
Your while loop in the last case can then be:
Node after = first;
while ((after != null) && (after.compareTo(newNode) <= 0)) {
//we traverse to the next
before = after;
after = after.getNext();
}
//either we found the spot, or it is the last element in the list
before.setNext(newNode);
newNode.setNext(after);
You can probably also include the case of when the new node should be inserted at the head of the list too... but I leave it to you.
You have various small issues.
- You are comparing the
Nodeinstead of theNode's data. - Your while loop is looping
while (first.next != null). Shouldn't this be checkingbeforeorafter? - Why are you creating the new
Nodeinside thewhileloop?
You could change the Node like this:
class Node implements Comparable<Node> {
private final String data; //once initialised shouldn't change
private Node next; //this can change
public Node(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int compareTo(Node that) {
return this.data.compareTo(that.data);
}
}
In your addElement(String element) I would immediately do the following before the if conditions, it is all the same:
Node newNode = new Node(element);
size++;
Your while loop in the last case can then be:
Node after = first;
while ((after != null) && (after.compareTo(newNode) <= 0)) {
//we traverse to the next
before = after;
after = after.getNext();
}
//either we found the spot, or it is the last element in the list
before.setNext(newNode);
newNode.setNext(after);
You can probably also include the case of when the new node should be inserted at the head of the list too... but I leave it to you.
edited Nov 12 at 14:52
answered Nov 12 at 14:28
jbx
10.4k1057109
10.4k1057109
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
add a comment |
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
I have tried to change them but it has not solved the problem
– user3740970
Nov 12 at 14:32
Check my updated answer.
– jbx
Nov 12 at 14:50
Check my updated answer.
– jbx
Nov 12 at 14:50
add a comment |
If you want a sorted linked list, you can use the normal LinkedList library and call List#sort(Comparator) on it.
For example, if we want to sort Strings by alphabetical order:
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list); // ["b","d","c","a"]
list.sort(new Comparator<String>() {
//This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
@Override
public int compare(String s1, String s2){
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(list); // ["a","b","c","d"]
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use aLinkedListbut something like aPriorityQueue, which is much more efficient.
– jbx
Nov 12 at 14:48
add a comment |
If you want a sorted linked list, you can use the normal LinkedList library and call List#sort(Comparator) on it.
For example, if we want to sort Strings by alphabetical order:
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list); // ["b","d","c","a"]
list.sort(new Comparator<String>() {
//This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
@Override
public int compare(String s1, String s2){
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(list); // ["a","b","c","d"]
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use aLinkedListbut something like aPriorityQueue, which is much more efficient.
– jbx
Nov 12 at 14:48
add a comment |
If you want a sorted linked list, you can use the normal LinkedList library and call List#sort(Comparator) on it.
For example, if we want to sort Strings by alphabetical order:
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list); // ["b","d","c","a"]
list.sort(new Comparator<String>() {
//This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
@Override
public int compare(String s1, String s2){
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(list); // ["a","b","c","d"]
If you want a sorted linked list, you can use the normal LinkedList library and call List#sort(Comparator) on it.
For example, if we want to sort Strings by alphabetical order:
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list); // ["b","d","c","a"]
list.sort(new Comparator<String>() {
//This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
@Override
public int compare(String s1, String s2){
return s1.compareToIgnoreCase(s2);
}
});
System.out.println(list); // ["a","b","c","d"]
answered Nov 12 at 14:42
Benjamin Urquhart
11
11
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use aLinkedListbut something like aPriorityQueue, which is much more efficient.
– jbx
Nov 12 at 14:48
add a comment |
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use aLinkedListbut something like aPriorityQueue, which is much more efficient.
– jbx
Nov 12 at 14:48
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use a
LinkedList but something like a PriorityQueue, which is much more efficient.– jbx
Nov 12 at 14:48
One would assume that the OP is trying to implement it himself to learn about linked lists. If you wanted a sorted list using the JDK collections you would not use a
LinkedList but something like a PriorityQueue, which is much more efficient.– jbx
Nov 12 at 14:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264071%2fhow-to-insert-a-string-in-a-sorted-linked-list-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to implement a
toString()method in the classNodeor directly access it with.datai.e.first.data.compareTo(element).– Turamarth
Nov 12 at 14:21
sorry i have forgotten to insert the toString() method
– user3740970
Nov 12 at 14:26
why should it be in the Node class?
– user3740970
Nov 12 at 14:27
1
You shouldn't be using
toStringat all for comparison here. That's a method that is supposed to be used for to make the object human readable - and in your case, includes extra characters. instead you should usefirst.data.– RealSkeptic
Nov 12 at 14:27