Solved

Merge two linked lists help

Posted on 2011-03-23
16
349 Views
Last Modified: 2013-11-23
I think I have my mergeList method correct but Im having trouble getting the new list to print out. Here is my code if anyone can help.
TestProgOrderedLinkedList.java
OrderedLinkedList.java
LinkedListADT.java
LinkedListClass.java
0
Comment
Question by:wjenlow
  • 8
  • 8
16 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35200515

But this does not compile, as there is undefined variable newHead in TestProgOrderedLinkedList
0
 

Author Comment

by:wjenlow
ID: 35200596
That's what Im trying to fix
0
 

Author Comment

by:wjenlow
ID: 35200610
It compiles when I take that line of code out but Im trying to print the new merged list
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 35201041
But if you take this line of code out where would you merge the lists.
I understand that this line is supposed to do the main job, isn't that right?

Therefore it is crucial to understand which type is newHead and where does this method mergeList lives.
I didn't find anything very applicable in your code - I found only
such method which merges nodes, not lists.
Did you take this code form somewhere?
0
 

Author Comment

by:wjenlow
ID: 35201118
I see now that is my problem.. it merges nodes and not lists. Is this a simple fix or will it take a lot of work? I
0
 

Author Comment

by:wjenlow
ID: 35201147
And yes that line is supposed to do the main job and Im getting the error because my method is written for nodes not my two lists. Im not sure how to fix it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35201234
Where did you get this code?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35201241
Maybe we can find in the same place something whicvh merges the lists?
0
 

Author Comment

by:wjenlow
ID: 35201263
I found it in a blog... I changed it some... but didn't realize it was used for nodes. There wasn't any other code.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35201701
Well, from your title I don't understand what is your task.
LinkedList is just one of the types of the list.
I don't see any problem merging them directly.
I'm not sure I understand your problem from the beginning.
0
 

Author Comment

by:wjenlow
ID: 35201720
My task is to ask for input.... two list of numbers... and the ouput should be the two lists merged together in correct order. I have the re program printing out each list with the numbers in correct order already. I just need to print out the merged list.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35201763
Then you don't need to have necessarily LINKED lists?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 35201883


Anyway, this will do what you need.
Don't know about LINKED lists though.
 
You need just one java file:
//Program to test various operations on an ordered linked list
//32 76 34 89 86 12 190 21 5 55 -999

import java.util.*;

public class TestProgOrderedLinkedList
{
   static Scanner console = new Scanner(System.in);

   public static void main(String[] args)
   {
      OrderedLinkedList<Integer> numList
              = new OrderedLinkedList<Integer>();  //Line 1

       ArrayList al = new ArrayList();

      Integer num, num2;                                  //Line 2

      System.out.println("Line 3: Enter integers "
                      + "ending with -999");       //Line 3


      num = console.nextInt();                      //Line 4



      while (num != -999)                           //Line 5
      {
         numList.insert(num);                    //Line 6
          al.add(new Integer(num));
         num = console.nextInt();                  //Line 7
      }

      System.out.println();                         //Line 8

      System.out.println("Line 9: numList: ");      //Line 9
      numList.print();                              //Line 10
      System.out.println();                         //Line 11


      System.out.println("Line 12: Length of "
                      + "numList: "
                      + numList.length());         //Line 12

      System.out.print("Line 13: Enter the number "
                    + "to be deleted: ");          //Line 13
      num = console.nextInt();                      //Line 14
      System.out.println();                         //Line 15

      numList.deleteNode(num);      //Line 16
       al.remove(new Integer(num));

      System.out.println("Line 17: After deleting "
                      + num + ", numList: ");      //Line 17
      numList.print();                              //Line 18
      System.out.println();                         //Line 19

      System.out.println("Line 20: Length of "
                      + "numList: "
                      + numList.length());         //Line 20


      OrderedLinkedList<Integer> numList2
             = new OrderedLinkedList<Integer>();   				 
      System.out.println("Line 3: Enter integers for second list "
                      + "ending with -999");
      num2 = console.nextInt();

      while (num2 != -999)                           //Line 5
      {
         numList2.insert(num2);                  //Line 6
          al.add(new Integer(num2));
         num2 = console.nextInt();                  //Line 7
      }
      System.out.println();                         //Line 8

      System.out.println("Line 9: numList2: ");      //Line 9
      numList2.print();                              //Line 10
      System.out.println();
       OrderedLinkedList newHead = new OrderedLinkedList(); 

     // newHead.mergeList(numList,numList2);

       Collections.sort(al);
       System.out.println("Merged list:");

       for(int j=0; j<al.size(); j++){
           System.out.print(al.get(j) + "  ");
       }
       System.out.println("");
       System.exit(0);
   }
}

Open in new window


This is the output:

Line 3: Enter integers ending with -999
12 15 60 17 -999

Line 9: numList: 
12 15 17 60 
Line 12: Length of numList: 4
Line 13: Enter the number to be deleted: 15

Line 17: After deleting 15, numList: 
12 17 60 
Line 20: Length of numList: 3
Line 3: Enter integers for second list ending with -999
1 9 3 -999

Line 9: numList2: 
1 3 9 
Merged list:
1  3  9  12  17  60  

Open in new window

0
 

Author Comment

by:wjenlow
ID: 35203368
Add a mergeLists method to the class OrderedLinkedList.  The mergeLists method shall accept two lists as inputs and creates a new list with all elements merged and in proper order.  Suppose list1 points to the list with the elements 2, 6, and 7 and list2 points to the list with the elements 3, 5, and 8.  The method call newlist.mergeLists(list1,list2);  should produce a list with the elements 2, 3, 5, 6, 7, and 8.  YOU MAY NOT USE THE INSERT METHOD PROVIDED IN THE ORDEREDLINKEDLIST CLASS.  Be sure to write the definition of the method mergeLists and thoroughly test your program.

This is my directions for the program... and I have to have all four java files.
0
 

Author Comment

by:wjenlow
ID: 35211918
Any more advice?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35211948
I'm sorry I don't understand the requirements.
And I don't know what to do with your four files.
As you saw I can merge two LinkedLists in one short java program, and I don't need to use insert method for that.
Sorry, perhpas someone else will give you better advice.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question