2012-10-18 105 views
1

我有一个任务来创建一个链表并分成两个子列表 ,现在我的代码有错误。如果任何人可以帮助我的代码,我不知道哪里需要改变,因为我改变了一些它给了我更多的错误。Java分割列表

public class UnorderedLinkedList<E extends Comparable<? super E>> extends LinkedList<T> 
{ 
    public void splitMid(LinkedList<String> subList) 
    { 
     LinkedList<T> current;//the head pointer 
     LinkedList<T> mid;//the mid point 

     //Node first = firstNode; 
     //Node last = firstNode; 

     //Node subListFirst; 
     //Node subListLast; 
     int i; 

     if(head == null) 
     { 
      subList.head = null; 
      subList.last = null; 
      subList.count = 0; 
     } 
     else 
     { 
      //mid = 
      head = null; 
      current = head.next; 
      i = 1; 

      if(current != null) 
       current = current.next; 

      while(current != null) 
      { 
       mid = mid.next; 
       current = current.next; 
       i++; 

       if(current != null) 
        current = current.next; 
      } 

      subList.head = head.next; 
      subList.last = last; 
      last = mid ; 
      last.next = null; 

      subList.count = count - i; 
      count = i; 
     } 
    } 
} 

错误消息

G:\LinkedList\src\LinkedList.java:184: error: cannot find symbol subList.count = 0;

symbol: variable count.

location: variable subList of type LinkedList.Node

where T,E are type-variables:

T extends Object declared in class LinkedList.

E extends Comparable declared in class LinkedList.UnorderedLinkedList

我的主类:

public void main(String args[]) 
{ 
    LinkedList<Integer> myList = new LinkedList<Integer>(); 
    LinkedList<Integer> subList = new LinkedList<Integer>(); 

    myList.addLast(34); 
    myList.addLast(65); 
    myList.addLast(87); 
    myList.addLast(29); 
    myList.addLast(12); 

    myList.splitMid(subList); 
} 

错误信息

G:\LinkedList\src\LinkedTest.java:31: error: cannot find symbol.

myList.splitMid(subList);

symbol: method splitMid(LinkedList)

location: variable myList of type LinkedList

+0

什么乌尔meaing关于 “分成两个子列表” ??? – zetsin

回答

1

关于编译错误:

您正在使用的LinkedList实例调用你为类UnorderedLinkedList

0

1.

G:\LinkedList\src\LinkedList.java:184: error: cannot find symbol subList.count = 0; 

尝试删除subList.count定义的方法;它没有必要为计数赋值;

2.

G:\LinkedList\src\LinkedTest.java:31: error: cannot find symbol. 

myList.splitMid(subList); 

做到这一点:

UnorderedLinkedList<Integer> myList = new UnorderedLinkedList<Integer>(); 
+0

请注意,'myList'需要键入为'UnorderedLinkedList',以便调用'splitMid'。 –

+0

行,即时通讯错误... – zetsin

+0

'myList.splitMid(subList);'不会编译,因为'myList'静态类型为'LinkedList',它没有声明'splitMid'。 'myList'必须声明为'UnorderedLinkedList'。 –