RandomStranger
RandomStranger

Reputation: 5

Need help adding one linked list to another linked list

I am trying to add one LinkedList to another LinkedList. I have been trying to do this for the past hour or two and I am at my wits end. Here is the line I am trying to run in main.

l.addList(l2); //l is the original list and l2 is the list I am trying to add, l2 does have elements in it.

Here's the LinkedList class:

public class LinkedList<T>
{

private Node<T> head;   // head of the list always at the front
private Node<T> cursor; // cursor that moves along the one way list

// constructor
public LinkedList ()
{
    // the first node is not used, dummy node
    // so we're always dealing with the element to the right of
    // the cursor not what the cursor is pointing to.
    head = new Node<T>(null, null);
    cursor = head;
}

// if the cursor's next is null, then we're at the end
public boolean isAtEnd()
{

return(cursor.getNext() == null);

}

// move the cursor to the beginning of the list
public void reset()
{

cursor = head;

}

// advance the cursor one spot to the right
public void advance()
{

cursor = cursor.getNext();

}

// return the node to the right of the cursor
public Node<T> getCurrent()
{

return cursor.getNext();

}

// return the first node in the list
public Node<T> getFirst()
{

return head.getNext();

}

public void addList(LinkedList<T> list)
{
    Node current = head;

    while (current.getNext() != null)
    {
        current = current.getNext();
    }
    current.setNext(list);
                    ^error here
}

// insert at the beginning of the list, this insert is done to the
// right of the dummy node, but to the left of the first meaningful
// node.
public void listHeadInsert(T value)
{

head.setNext(new Node<T>(value, head.getNext()));

}

// wherever the cursor is, insert to the right of it, and move the
// cursor to point to the newly inserted node
// you may remove the line that advances the cursor, but you need
// to make sure that you advance the cursor when inserting elements
// at the end of the list one after another.
public void listInsert(T value)
{
// insert to the right of the cursor
cursor.setNext(new Node<T>(value, cursor.getNext()));

cursor = cursor.getNext();

}


// move the cursor to the head of the list, and keep moving it
// looking for the value, stop if you either find the value
// or you have reached the end of the list without finding it.
// return the node that contains the given value back to me.
// this return will return null if the value is not found.
public Node<T> listSearch(T value)
{
cursor = head;
while(cursor.getNext() != null &&
      !cursor.getNext().getValue().equals(value))
    cursor = cursor.getNext();

return cursor.getNext();

}


// first search (first 4 lines of the code)
// if you find it (not null) then just remove it by making the
// cursor's next pointer point to the node next to it's next
// pointer (skip a node)
public void listRemove(T value)
{
cursor = head;
while(cursor.getNext() != null &&
      !cursor.getNext().getValue().equals(value))
    cursor = cursor.getNext();

if(cursor.getNext() != null)
    {
    cursor.setNext(cursor.getNext().getNext());

    }

}

// don't search, just remove the node to the right of the cursor
// if it's not null.
public void listRemoveCurrent()
{

if(cursor.getNext() != null)
    {
    cursor.setNext(cursor.getNext().getNext());

    }

}


}

And here's the Node class:

// Node of any Reference type T

public class Node<T>
{

private T value;        // this is the data value
private Node<T> next;   // this is pointing to the next node


// the node constructor
public Node (T v, Node<T> n)
{
    value = v;
    next = n;
}

// getters and setters for the node's value and next pointer
public T getValue() {return value;}
public Node<T> getNext() {return next;}
public void setValue(T v){value = v;}
public void setNext(Node<T> n){next = n;}

}

I get the error: Incompatible types, LinkedList cannot be converted into Node in class LinkedList, line 69. I understand the theory when using headers and cursors I am just having issues putting it into practice.

Upvotes: 0

Views: 3172

Answers (2)

Evan Bechtol
Evan Bechtol

Reputation: 2865

You are trying to pass a list, when setNext only accepts a node. That is exactly why you are getting the error.
What you need to do is pass setNext the head node of the list that is to be merged (l2).

public void addList(LinkedList<T> list)
{
    Node current = head;

    while (current.getNext() != null)
    {
        current = current.getNext();
    }
    current.setNext(list.getFirst()); //Try this

}

Upvotes: 3

KyleP
KyleP

Reputation: 11

Rather than:

current.setNext(list);

use:

current.setNext(list.getFirst());

Upvotes: 0

Related Questions