How to insert String data in a sorted doubly linked list?
I have a sorted doubly linked list in which the first and last elements are null. This means when I insert the values a, b, c. The result should look as follows: {null, a, b, c, null}
The empty sorted doubly linked list should look like this: {null, null} in which the first and last elements are always are null.
The problem is that when I insert data in the sorted doubly linked list, the data is not sorted correctly and the 2 null values are always at the end of the list. How can I fix this?
Here is my current insert method:
public void addElement(String element) {
// new node which will be inserted in the list
Node newNode = new Node();
newNode.data = element;
// if the list is empty
if (size == 0) {
last = newNode;
newNode.next = first;
first = newNode;
size++;
} else {
Node current = first;
// if the element should be at the beginning of the list
if (current.data.compareTo(element) > 0) {
newNode.next = current;
newNode.previous = null;
current.previous = newNode;
first = newNode;
} else {
while (current != null) {
if (current.data.compareTo(element) <= 0) {
if (current.next == null) {
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
break;
}
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
}
size++;
}
}
java doubly-linked-list
add a comment |
I have a sorted doubly linked list in which the first and last elements are null. This means when I insert the values a, b, c. The result should look as follows: {null, a, b, c, null}
The empty sorted doubly linked list should look like this: {null, null} in which the first and last elements are always are null.
The problem is that when I insert data in the sorted doubly linked list, the data is not sorted correctly and the 2 null values are always at the end of the list. How can I fix this?
Here is my current insert method:
public void addElement(String element) {
// new node which will be inserted in the list
Node newNode = new Node();
newNode.data = element;
// if the list is empty
if (size == 0) {
last = newNode;
newNode.next = first;
first = newNode;
size++;
} else {
Node current = first;
// if the element should be at the beginning of the list
if (current.data.compareTo(element) > 0) {
newNode.next = current;
newNode.previous = null;
current.previous = newNode;
first = newNode;
} else {
while (current != null) {
if (current.data.compareTo(element) <= 0) {
if (current.next == null) {
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
break;
}
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
}
size++;
}
}
java doubly-linked-list
Please, provideNode
class implementation,first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve
– Sergei Sirik
Nov 13 '18 at 23:28
add a comment |
I have a sorted doubly linked list in which the first and last elements are null. This means when I insert the values a, b, c. The result should look as follows: {null, a, b, c, null}
The empty sorted doubly linked list should look like this: {null, null} in which the first and last elements are always are null.
The problem is that when I insert data in the sorted doubly linked list, the data is not sorted correctly and the 2 null values are always at the end of the list. How can I fix this?
Here is my current insert method:
public void addElement(String element) {
// new node which will be inserted in the list
Node newNode = new Node();
newNode.data = element;
// if the list is empty
if (size == 0) {
last = newNode;
newNode.next = first;
first = newNode;
size++;
} else {
Node current = first;
// if the element should be at the beginning of the list
if (current.data.compareTo(element) > 0) {
newNode.next = current;
newNode.previous = null;
current.previous = newNode;
first = newNode;
} else {
while (current != null) {
if (current.data.compareTo(element) <= 0) {
if (current.next == null) {
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
break;
}
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
}
size++;
}
}
java doubly-linked-list
I have a sorted doubly linked list in which the first and last elements are null. This means when I insert the values a, b, c. The result should look as follows: {null, a, b, c, null}
The empty sorted doubly linked list should look like this: {null, null} in which the first and last elements are always are null.
The problem is that when I insert data in the sorted doubly linked list, the data is not sorted correctly and the 2 null values are always at the end of the list. How can I fix this?
Here is my current insert method:
public void addElement(String element) {
// new node which will be inserted in the list
Node newNode = new Node();
newNode.data = element;
// if the list is empty
if (size == 0) {
last = newNode;
newNode.next = first;
first = newNode;
size++;
} else {
Node current = first;
// if the element should be at the beginning of the list
if (current.data.compareTo(element) > 0) {
newNode.next = current;
newNode.previous = null;
current.previous = newNode;
first = newNode;
} else {
while (current != null) {
if (current.data.compareTo(element) <= 0) {
if (current.next == null) {
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
break;
}
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
}
size++;
}
}
java doubly-linked-list
java doubly-linked-list
edited Nov 13 '18 at 22:32
dchi
212
212
asked Nov 13 '18 at 20:54
user3740970user3740970
150117
150117
Please, provideNode
class implementation,first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve
– Sergei Sirik
Nov 13 '18 at 23:28
add a comment |
Please, provideNode
class implementation,first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve
– Sergei Sirik
Nov 13 '18 at 23:28
Please, provide
Node
class implementation, first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve– Sergei Sirik
Nov 13 '18 at 23:28
Please, provide
Node
class implementation, first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve– Sergei Sirik
Nov 13 '18 at 23:28
add a comment |
3 Answers
3
active
oldest
votes
It is not so clear what you are doing in your code, so I modified it a bit and made more OO style, so here it is:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("nlist reverse: ");
sortedDLL.printReverse();
}
Output:
list:
null a b c c null
list reverse:
null c c b a null
add a comment |
the problem starts at the first call when size == 0
you push the first null to the end.. and the first node becomes the new node.
then, if you fix this you will get null pointer exception at the row :
if (current.data.compareTo(element) > 0) {
because the current will be null and the will not have data.
you should ignore the first null in the first insert and every insert after that.
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
add a comment |
Depending on implementation I think you're just doing the right thing in the wrong place.
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
Instead of checking if the currently selected node is smaller you need to check if the node after is bigger because then you can place the node.
And checking if current.next is null needs to be done outside of that comparison.
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%2f53289347%2fhow-to-insert-string-data-in-a-sorted-doubly-linked-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is not so clear what you are doing in your code, so I modified it a bit and made more OO style, so here it is:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("nlist reverse: ");
sortedDLL.printReverse();
}
Output:
list:
null a b c c null
list reverse:
null c c b a null
add a comment |
It is not so clear what you are doing in your code, so I modified it a bit and made more OO style, so here it is:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("nlist reverse: ");
sortedDLL.printReverse();
}
Output:
list:
null a b c c null
list reverse:
null c c b a null
add a comment |
It is not so clear what you are doing in your code, so I modified it a bit and made more OO style, so here it is:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("nlist reverse: ");
sortedDLL.printReverse();
}
Output:
list:
null a b c c null
list reverse:
null c c b a null
It is not so clear what you are doing in your code, so I modified it a bit and made more OO style, so here it is:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("nlist reverse: ");
sortedDLL.printReverse();
}
Output:
list:
null a b c c null
list reverse:
null c c b a null
edited Nov 13 '18 at 23:24
answered Nov 13 '18 at 23:02
Sergei SirikSergei Sirik
6881521
6881521
add a comment |
add a comment |
the problem starts at the first call when size == 0
you push the first null to the end.. and the first node becomes the new node.
then, if you fix this you will get null pointer exception at the row :
if (current.data.compareTo(element) > 0) {
because the current will be null and the will not have data.
you should ignore the first null in the first insert and every insert after that.
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
add a comment |
the problem starts at the first call when size == 0
you push the first null to the end.. and the first node becomes the new node.
then, if you fix this you will get null pointer exception at the row :
if (current.data.compareTo(element) > 0) {
because the current will be null and the will not have data.
you should ignore the first null in the first insert and every insert after that.
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
add a comment |
the problem starts at the first call when size == 0
you push the first null to the end.. and the first node becomes the new node.
then, if you fix this you will get null pointer exception at the row :
if (current.data.compareTo(element) > 0) {
because the current will be null and the will not have data.
you should ignore the first null in the first insert and every insert after that.
the problem starts at the first call when size == 0
you push the first null to the end.. and the first node becomes the new node.
then, if you fix this you will get null pointer exception at the row :
if (current.data.compareTo(element) > 0) {
because the current will be null and the will not have data.
you should ignore the first null in the first insert and every insert after that.
answered Nov 13 '18 at 21:11
Liad SaubronLiad Saubron
794
794
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
add a comment |
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
but how should the code look like?
– user3740970
Nov 13 '18 at 21:28
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
you should initialize the first 2 null's as nodes with null data where the first one points to second and the second points to null. then, you should check in the first insert if the node has null value, and set the new node as its successor and the 2nd null as predecessor. do the same for the logic of size !=0
– Liad Saubron
Nov 13 '18 at 21:35
add a comment |
Depending on implementation I think you're just doing the right thing in the wrong place.
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
Instead of checking if the currently selected node is smaller you need to check if the node after is bigger because then you can place the node.
And checking if current.next is null needs to be done outside of that comparison.
add a comment |
Depending on implementation I think you're just doing the right thing in the wrong place.
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
Instead of checking if the currently selected node is smaller you need to check if the node after is bigger because then you can place the node.
And checking if current.next is null needs to be done outside of that comparison.
add a comment |
Depending on implementation I think you're just doing the right thing in the wrong place.
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
Instead of checking if the currently selected node is smaller you need to check if the node after is bigger because then you can place the node.
And checking if current.next is null needs to be done outside of that comparison.
Depending on implementation I think you're just doing the right thing in the wrong place.
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
Instead of checking if the currently selected node is smaller you need to check if the node after is bigger because then you can place the node.
And checking if current.next is null needs to be done outside of that comparison.
answered Nov 13 '18 at 22:48
GhorkGhork
85
85
add a comment |
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.
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%2f53289347%2fhow-to-insert-string-data-in-a-sorted-doubly-linked-list%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
Please, provide
Node
class implementation,first
last
initialization, and actual output that you are getting. Take a look at this stackoverflow.com/help/mcve– Sergei Sirik
Nov 13 '18 at 23:28