[英]Doubly linked list, Node cannot 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"));
}
}
您沒有正確理解雙向鏈表的結構。
雙向鏈表由一系列節點組成,每個節點都有對系列中下一個和前一個的引用。
你想要做的是創建一個節點類(或修改你現有的類)來擁有這些引用,然后當你想從列表中的某些東西中搜索時,你只需要一個對系列頭部的引用,然后遍歷所有其中,您可以這樣做,因為它們都有指向系列中下一個的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.