Solved

Merge two linked lists help

Posted on 2011-03-23
16
339 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 39
Java Options in Zimbra Server 2 47
tomcat not starting 6 32
servlet example 11 19
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now