Solved

Merge two linked lists help

Posted on 2011-03-23
16
336 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

17 Experts available now in Live!

Get 1:1 Help Now