Linked List (adding alphabetically)
Hello, I am trying to add String objects into a linked list alphabetically. However, when I put multiple strings in and go to print it it will only print the first thing I input; losing the rest of the Strings.
My problem I think is adding a New Node(Store) at the beginning and then keeping track of the next Node when added is still pointing to null or just not adding it at all
Code Java:
public static void addStore(Store newStore, String location) {
Store current = head;
if(head == null) {
Store newNode = new Store(location);
head = newNode;
}
else
while(current.getNext() != null && newStore.getStoreLocation().compareTo(current.getStoreLocation()) == -1) {
current = current.getNext();
}
newStore.setNextStore(current);
}
Re: Linked List (adding alphabetically)
when you are making a linked list. common areas of extra caution are:
The root/head/top/front:
No matter what you call it, the first node is different because when it is added to the linked list, it has no previous link to link to, rather it has to know to set itself as the root.
check where you add to make sure you have a special case for the first node.
check where you add to make sure you are a) connecting it to the list properly, and b updating the end of the list properly.
note: since you are adding to the center of the list, when you are traveling through the list, you need to break the list, adding the new node, and then setting the next node as the one you broke.
last thing to check up on, is your print method, in some cases, your linked list performs perfectly, and just your print statement has a small problem.
getting to your code
Code :
public static void addStore(Store newStore, String location) {
Store current = head;
if(head == null) {
Store newNode = new Store(location);
head = newNode;
}
else
while(current.getNext() != null && newStore.getStoreLocation().compareTo(current.getStoreLocation()) == -1) {
current = current.getNext();
}
newStore.setNextStore(current);
}
I am making some assumptions since we only have your add method, but Store objects are your Node class? so they have a "Store" variable to find the next store right? when you make a new Store(String) is its next Store variable initialized to null?
you are creating the new Store in seemingly two locations. 1, it seems you are passing the method newStore as a method parameter.
also internally it seems you are making newNode if it is your first Node.
is there a reason you dont just set head = to newStore?
because although it doesnt seem to hurt much, newStore.setNextStore(current) is not doing what it should if head == null which is newNode.setNextStore(current) AKA newNode.setNextStore(null).
on your second pass you reach the else, because
head = newNode = newStore(location FIRST PASS). AKA head != null
but while statement never executes because
current.getNext() = head.getNext() = newNode.getNext() = new Store(location FIRST PASS).getNext() SHOULD BE NULL STILL
so newStore never gets added to the list and then you update the next node of newStore which is irrelevant because it is never added to the list.
fix this, and also consider compareTo and make sure you have the proper toString methods in your Store class so they compare how oyu want them too (by location) because otherwise i think you are comparing Store pointers/ addresses.
Hope this helps,
Jon
Re: Linked List (adding alphabetically)
Still unsure how to implement what you said; however, I am going to try and make sense of what I am misunderstanding. If you need more code I can provide it.
My first pass would take in a Store node created in my (Main) class with a location already assigned in that Node. Changed in the code below
Code java:
public static void addStore(Store newStore) {
Store current = head;
if(head == null) {
head = newStore;
return;
}
This will return the first Node; skipping everything else in the method because it is not needed.
Now a call to addStore method again will make current and head not equal to null so we enter the else. HOWEVER, I removed current.getNext() != null in the while because there is no second Node yet when we enter - Therefore current.getNext() equaling to null.
Right now I think my while loop is invalid to proceed because it only compares addresses like you said. So far I can only add right now with the method a max of 2 Stores that I can print.
Should I look into my while or does the code itself stop it from moving onto the nextNode?
Code Java:
else if (head != null) {
while(newStore.getStoreLocation().compareTo(current.getStoreLocation()) == -1) {
current = current.getNext();
}
}
current.setNextStore(newStore);
Re: Linked List (adding alphabetically)
If you want a collection that automatically sorts it in alphabetical order each time you add a new element to the list, try using a TreeSet. ( TreeSet<String> treeset = new TreeSet<String>(); ) Each time you add an element, it will insert it at a location where it will be in alphabetical order. If you wanna still want to use a list, after each time you add an element to it, just call Collections.sort(list); and it will sort the list alphabetically.