Results 1 to 2 of 2
  1. #1
    Loraeron is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Java Linked List Homework Help

    I am having an issue trying to figure out this program, I think I am overthinking it greatly and making it a lot harder than it actually should be. The objective is to create two linked list then merge and sort them into another 3rd list, then display the two original list and the third merge/sorted list. I can not use the built in list class for this assignment. I will paste my code in below and hope someone can help me out or show me my mistakes. Please note I am using the NetBeans IDE so you may have to create a new project and name it HW3_Vinson to run the code i have below, also note the code runs, but it is an infinite loop. Thanks in advance:

    package hw3_vinson;
    import java.util.LinkedList;
    import javax.xml.*;

    public class HW3_Vinson
    {
    public static void main(String[] args)
    {
    //Linked list one holding integers
    HW3_Vinson LL1 = new HW3_Vinson();
    LL1.add(1);
    LL1.add(3);
    LL1.add(2);

    //Linked list two holding integers
    HW3_Vinson LL2 = new HW3_Vinson();
    LL2.add(4);
    LL2.add(8);
    LL2.add(5);

    //Will hold combintation of LL1 and LL2
    HW3_Vinson LL3 = new HW3_Vinson();

    //Print each list
    LL1.print();
    LL2.print();
    LL3 = LL3.TraverseMerge(LL1, LL2);
    LL3.print();
    }

    //Node Class creation
    public class Node
    {
    public int element;
    public Node next;

    Node(int el, Node n)
    {
    element = el;
    next = n;
    }

    Node(int el)
    {
    this(el, null);
    }
    }

    //Set up nodes to store first and last element
    public Node firstElement;
    public Node lastElement;

    //Constructor for class
    public HW3_Vinson()
    {
    firstElement = null;
    lastElement = null;
    }

    //check to see if list is empty, return null if true
    public boolean isEmpty()
    {
    return firstElement == null;
    }

    //add to end of list
    public void add(int e)
    {
    if (isEmpty())
    {
    firstElement = new Node(e);
    lastElement = firstElement;
    }
    else
    {
    lastElement.next = new Node(e);
    lastElement = lastElement.next;
    }
    }

    //Print out linked list
    public void print()
    {
    Node ref = firstElement;
    while (ref != null)
    {
    System.out.println(ref.element + " ");
    ref = ref.next;
    }
    }

    public int size()
    {
    int count = 0;
    Node ref = firstElement;
    while(ref != null)
    {
    count++;
    ref = ref.next;
    }

    return count;
    }

    public Node get(int x)
    {
    int searchLoc = x;
    Node ref = firstElement;
    Node foundNode = null;
    int counter = 0;

    while(ref != null)
    {
    if(counter == searchLoc)
    {
    foundNode = ref;
    }
    }

    return foundNode;
    }

    public HW3_Vinson TraverseMerge(HW3_Vinson LL01, HW3_Vinson LL02)
    {
    HW3_Vinson LL03 = new HW3_Vinson();
    Node temp1 = LL01.get(0);
    Node temp2 = LL02.get(0);

    while (LL01 != null && LL02 != null)
    {
    int temp2int = temp2.element;
    int temp1int = temp1.element;
    if(temp1int > temp2int)
    {
    LL03.add(temp2int);
    temp2 = temp2.next;
    }
    else if (temp2int > temp1int)
    {
    LL03.add(temp1int);
    temp1 = temp1.next;
    }
    else if (temp1 == null)
    {
    LL03.add(temp2int);
    temp2 = temp2.next;
    }
    else if (temp2 == null)
    {
    LL03.add(temp1int);
    temp1 = temp1.next;
    }
    }

    return LL03;
    }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Java Linked List Homework Help

    Please format your code using the [code][/code] to make it more readable.

    Do you think this loop will finish when ref is not null?

    Java Code:
    while (ref != null) {
        if (counter == searchLoc) {
            foundNode = ref;
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Java Linked List
    By nomss in forum New To Java
    Replies: 0
    Last Post: 02-19-2011, 06:43 AM
  4. Java Linked List
    By nomss in forum New To Java
    Replies: 7
    Last Post: 12-10-2010, 01:02 PM
  5. Java Linked List
    By nomss in forum New To Java
    Replies: 8
    Last Post: 12-09-2010, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •