Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Linked List (adding alphabetically)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


    	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);
    	}
    Last edited by Usoda; March 7th, 2012 at 09:22 PM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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
    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

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
    	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?

    		else if (head != null) {
    			while(newStore.getStoreLocation().compareTo(current.getStoreLocation()) == -1) {
    				current = current.getNext();
    			}
    		}	
    		current.setNextStore(newStore);

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  2. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  3. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  4. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM

Tags for this Thread