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 11 of 11

Thread: Linked List problem, please help.

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lightbulb Linked List problem, please help.

    I am trying to make a method which receives the first list node of two different linked list's of ints and the method is supposed to insert the second list right before the smallest int of the first list. Here is what i have but it is very confusing and I do not know how to fix it. The other methods of the program are working fine.
    public static ListNode insertBeforeSmallest (ListNode headMain, ListNode headInsert)
        {
     
            ListNode start = headMain;
            ListNode prevNodeMain = headMain;
            ListNode prevNodeMain2 = headMain;
            ListNode nodePtrMain = headMain.getNext ();
            ListNode nodePtrMain2 = headMain.getNext ();
            ListNode nodePtrInsert = headInsert;
            int length = Length (prevNodeMain);
            String lowestObj = "";
            for (int j = 0 ; j < length ; j++)
            {
                if (j == 0)
                {
                    lowestObj = ((String)prevNodeMain.getValue ());
                    prevNodeMain = nodePtrMain;
                    nodePtrMain = nodePtrMain.getNext ();
                }
                else if (((String)prevNodeMain.getValue ()).compareTo (nodePtrMain.getValue ()) < 0)
                {
                    lowestObj = (String)nodePtrMain.getValue ();
                    prevNodeMain = nodePtrMain;
                    nodePtrMain = nodePtrMain.getNext ();
                }
                else
                {
                    prevNodeMain = nodePtrMain;
                    nodePtrMain = nodePtrMain.getNext ();
                }
            }
            while (nodePtrMain2 != null)
            {
                if(prevNodeMain2.getNext ().getValue ().equals (lowestObj))
     
                    {
                        prevNodeMain2.getNext ().setNext (nodePtrInsert);
                        for (int i = 0 ; i < (Length (nodePtrInsert) - 1) ; i++)
                        {
                            nodePtrInsert.setNext (nodePtrInsert.getNext ());
                        }
                        nodePtrInsert.setNext (nodePtrMain2);
                        return start;
                    }
                else
                {
                    prevNodeMain2 = nodePtrMain2;
                    nodePtrMain2 = nodePtrMain2.getNext ();
                }
            }
            return null;
     
        }
    Last edited by helloworld922; December 20th, 2010 at 01:01 PM.


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default Re: Linked List problem, please help.

    Hey Axeander,

    Just a small thing, please use

    code goes here
    while posting your java code.

    That would make the code more readable for us.

    We appreciate your co-operation.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Linked List problem, please help.

    Ok thanks, but I'm cant find how to use that. Is it build into the post box, or do I need to get it from somewhere else?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Linked List problem, please help.

    Quote Originally Posted by Axeander View Post
    Ok thanks, but I'm cant find how to use that. Is it build into the post box, or do I need to get it from somewhere else?
    Hi Axeander, you can get your code into the box by editing your post above and flanking the source code with either the [highlight=java][/highlight] or [code][/code], where the code goes between the bracketed tags.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Axeander (December 21st, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Linked List problem, please help.

    Ok thanks, but do you have any suggestions about the code its self? I really need some help on it.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Linked List problem, please help.

    ListNode<Integer> start as your ListNode stores Integers.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Linked List problem, please help.

    int length = Length (prevNodeMain);

    I don't see what this is for.

    Usually you look at the size of the entire list, not a just a single Node.

    Unless Length is another method in the class that you're not showing because you've already got it figured out.

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Linked List problem, please help.

    (String)prevNodeMain.getValue ()

    If you're using Integers,

    prevNodeMain.getValue().toString() would be better.

    I don't think the way you have it now will work.

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Linked List problem, please help.

    Also, the ListNodes you're passing as parameters should have their data types. I think that you said they were both Integer.

    public static ListNode<Integer> insertBeforeSmallest (ListNode<Integer> headMain, ListNode<Integer> headInsert)

  11. The Following User Says Thank You to javapenguin For This Useful Post:

    Axeander (December 21st, 2010)

  12. #10
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Linked List problem, please help.

    Yes I already have the method Length on the rest of the program which is working but for some reason when i run the program using this code,
     public static void main (String[] args)
        {
     
            ListNode startList, startInsert;
            System.out.println ("Entering the original list...");
            startList = getLinkedList ();
            System.out.println ("Entering the list to be inserted...");
            startInsert = getLinkedList ();
            startList = insertBeforeSmallest (startList, startInsert);
     
            // prints elements of a linked list
            ListNode tempNode = startList;
            while (tempNode != null)
            {
                System.out.println (tempNode.getValue ().toString ());
                tempNode = tempNode.getNext ();
            }
        } // main method
     
     
        public static ListNode getLinkedList ()
        { //gets linked list
            ListNode startNode, node, lastNode;
            System.out.println ("Type in a name,'none' to quit..");
            String name = Stdin.readLine ();
            if (name.equals ("none"))
                startNode = null;
            else
            {
                startNode = new ListNode (name, null);
                lastNode = startNode;
                do
                {
                    System.out.println ("Type in a name, 'none' to quit.");
                    name = Stdin.readLine ();
                    if (!name.equals ("none"))
                    {
                        node = new ListNode (name, null);
                        lastNode.setNext (node);
                        lastNode = node;
                    }
                }
                while (!name.equals ("none"));
            } //else
            return startNode;
        }

    I get stuck in the while loop "while(tempNode != null)" and keeps printing the same 2 number over and over.

  13. #11
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Re: Linked List problem, please help.

    Ok thanks everyone, I just finished it up. Thanks for the help.

Similar Threads

  1. Having trouble with linked list
    By joecool594 in forum Collections and Generics
    Replies: 6
    Last Post: March 21st, 2011, 08:50 PM
  2. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  3. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  4. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM
  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