Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Merge two linked lists help

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
wjenlow
Asked:
wjenlow
  • 8
  • 8
1 Solution
 
for_yanCommented:

But this does not compile, as there is undefined variable newHead in TestProgOrderedLinkedList
0
 
wjenlowAuthor Commented:
That's what Im trying to fix
0
 
wjenlowAuthor Commented:
It compiles when I take that line of code out but Im trying to print the new merged list
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
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
 
wjenlowAuthor Commented:
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
 
wjenlowAuthor Commented:
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
 
for_yanCommented:
Where did you get this code?
0
 
for_yanCommented:
Maybe we can find in the same place something whicvh merges the lists?
0
 
wjenlowAuthor Commented:
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
 
for_yanCommented:
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
 
wjenlowAuthor Commented:
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
 
for_yanCommented:
Then you don't need to have necessarily LINKED lists?
0
 
for_yanCommented:


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
 
wjenlowAuthor Commented:
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
 
wjenlowAuthor Commented:
Any more advice?
0
 
for_yanCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now