错误:LinkedList <t>无法转换为T </t>

时间:2015-03-04 00:40:17

标签: java linked-list

我目前正在尝试将两个链接列表添加到一起。但每次我都尝试编译。我得到错误:LinkedList无法转换为T.我理解链表如何与头和游标一起工作,我只是在实现引用参数时遇到了麻烦。如果我能弄清楚,我可以让其余的工作。这是代码。

ListDriver:

public class ListDriver
{
    public static void main(String [] args)
    {

LinkedList<Integer> l = new LinkedList<Integer>();
LinkedList<Integer> l2 = new LinkedList<Integer>();

int i;


for(i = 0; i < 4; i++)
    l.listInsert(new Integer(i+3));

System.out.println("After the first for loop (3,4,5,6)");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}


l.listHeadInsert(new Integer(500));
System.out.println("After the head insert(500,3,4,5,6)");
System.out.println("----------------------------------");
    l.reset();
while(!l.isAtEnd())
{
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}


l.listRemove(new Integer(5));
System.out.println("After the remove (500,3,4,6)");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}

while(!l.isAtEnd())
    l.advance();

for(i = 1; i < 4; i++)
    l.listInsert(new Integer(i*100));

System.out.println("After the inserting (100,200,300) at the end");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}

l.reset();
if(l.listSearch(new Integer(200)) != null)
{
    System.out.println("\nSearched and found the 200");
    System.out.println("---------------------------\n");
}

l.listInsert(new Integer(150));
System.out.println("After the inserting 150 before the 200");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
{
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}

l2.listInsert(new Integer(500));
l2.listInsert(new Integer(7));
l2.listInsert(new Integer(9));
l2.listInsert(new Integer(200));
l2.listInsert(new Integer(301));
l.addList(l2);

System.out.println("After the inserting list 2 (500,7,9,200,301)");
System.out.println("----------------------------------");
l.reset();
while(!l.isAtEnd())
    {
    Node<Integer> tmp = l.getCurrent();
    Integer n = tmp.getValue();

    System.out.println(n.intValue());
    l.advance();
}

}

}

链表:

 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)
{
    cursor.setNext(new Node<T>(list, cursor.getNext()));
    cursor = cursor.getNext();
}

// 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());

    }

}


}

节点:

 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;}

}

1 个答案:

答案 0 :(得分:1)

问题在于方法addList

public void addList(LinkedList<T> list)
{
    cursor.setNext(new Node<T>(list, cursor.getNext()));
    cursor = cursor.getNext();
}

在这里,您正在构建Node类型为T的新实例。因此,构造函数希望您传入类型为T的对象作为第一个参数,而是传入list,类型为LinkedList<T>

假设目标是将一个LinkedList的元素添加到另一个cursor.setNext(new Node<T>(list.getFirst(), cursor.getNext())); ,你想要做更多的事情:

{{1}}
相关问题