2014-03-30 29 views
-1

我試圖做一個雙向鏈表類。當我嘗試編譯時,出現錯誤「Node can not be resolved to a type」。此外,我不確定在哪裏開始製作displayAllBackward方法。我是否會讓一個節點跟隨列表直到最後一個節點,然後使用一個循環讓它回到列表中?雙向鏈表,節點無法解析到類型

public class DoublyLL 
{ 
private StudentListings l; 
private Node h; 

public DoublyLL() 
{ 
    h = new Node(); 
    h.1 = null; 
    h.next = null; 
} 

public boolean insert(StudentListings newStudentListings) 
{ 
    Node n = new Node(); 
    n.l = newStudentListings.deepCopy(); 
    Node p = h.next; 
    Node q = h; 
    while(p != null && (p.l.compareTo(n.l.getKey()) > 0)) 
    { 
     q = p; 
     p = p.next; 
    } 
    if(n == null)//out of memory 
     return false; 
    else 
    { 
     n.next = p; 
     q.next = n; 
     n.back = q; 
     if(p == null) 
      return true; 
     else 
     { 
      p.back=n; 
      return true; 
     } 
    } 
}//end insert 

public StudentListings fetch(String targetKey) 
{ 
    Node p = h.next; 
    while(p != null && !(p.l.compareTo(targetKey) < 0) && !(p.l.compareTo(targetKey) == 0)) 
    { 
     p = p.next; 
    } 
    if(p !=null) 
     return null; 
    else if (p.l.compareTo(targetKey)== 0) 
     return p.l.deepCopy(); 
    else 
     return null;  
}//end fetch 
public boolean delete(String targetKey) 
{ 
    Node q = h; 
    Node p = h.next; 
    while(p != null &&!(p.l.compareTo(targetKey) < 0) && !(p.l.compareTo(targetKey)== 0)) 
    { 
      q = p; 
      p = p.next; 
    } 
    if(p == null) 
     return false; 
    else if(p.next == null) 
    { if(p.l.compareTo(targetKey)== 0) 
     {  q.next = null; 
       return true; 
     } 
     else 
     { return false; 

     } 
    } 
    else if(p.l.compareTo(targetKey)== 0 && p.next != null) 
    { 
     Node s = p.next; 
     q.next = s; 
     s.back = q; 
     return true;  
    } 
    else 
     return false; 
}//end delete 

public boolean update(String targetKey, StudentListings newStudentListings) 
{ 
    { 
     if(delete(targetKey) == false) 
      return false; 
      else if (insert(newStudentListings) == false) 
       return false; 
      return true; 
    } 

}//end update 

public void showAll() 
{ 
    Node p = h.next; 
    while(p != null) 
    { System.out.println(p.1.toString()); 
     p = p.next; 
    } 
} 
} 

編輯:這是我的學生列表類:

import javax.swing.JOptionPane; 
public class StudentListings 
{//start class 
private String name; 
private int ID; 
private int GPA; 

public StudentListings() 
{ 

} 

public StudentListings(String n, int i, int g) 

{ name = n; 
    ID = i; 
    GPA = g; 
} 
public String toString() 
{return("Name is " + name + 
     "\nID is " + ID + 
     "\nGPA is " + GPA); 
} 
public StudentListings deepCopy() 
{ StudentListings clone = new StudentListings(name,ID,GPA); 

    return clone; 
} 
public int compareTo(String targetKey) 
{ return(name.compareTo(targetKey)); 
} 
public void input() 
{ name = JOptionPane.showInputDialog("Enter a name"); 
    ID = Integer.parseInt(JOptionPane.showInputDialog("Enter an ID")); 
    GPA = Integer.parseInt(JOptionPane.showInputDialog("Enter the GPA")); 
} 
} 
+0

錯誤的哪部分你不明白?你有一個'Node'類嗎? – SLaks

+0

我有一個節點信息的StudentListings類。我是否應該將節點的位置更改爲StudentListings? – Bygonaut

+2

您是在製作自己的Node類還是使用現有的類?如果你自己做,請包括它。 –

回答

0

你是不是認識一個雙向鏈表的結構正確。

一個雙向鏈表由一系列節點組成,每一個節點都有一個引用序列中的下一個和前一個節點。

你想要做的是創建一個節點類(或修改你現有的類)來獲得這些引用,然後當你想從列表中的東西中搜索時,你只需要引用系列的頭部,然後遍歷所有這些,你可以這樣做,因爲它們都鏈接到系列中的下一個。