I'm trying to make a doubly linked list class. When I try to compile I get the error "Node cannot be resolved to a type". Also I'm not sure on where I would start to make a displayAllBackward method. Would I make a node follow down the list until the last node, then use a loop to have it go back up the list?
我想要创建一个双向链表类。当我试图编译时,我得到错误“节点不能被解析为一个类型”。我也不确定在哪里开始做displayallback方法。我是否会让一个节点沿着列表向下,直到最后一个节点,然后使用一个循环将它返回到列表中?
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;
}
}
}
Edit: Here's my student listings class:
编辑:这是我的学生列表课:
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"));
}
}
1 个解决方案
#1
0
You are not understanding the structure of a doubly linked list correct.
您不理解双链表的结构是否正确。
A doubly linked list consists of a series of nodes, each of which has a reference to the next in the series and the previous.
双链表由一系列节点组成,每一个节点都有一个指向序列中的下一个节点和前一个节点。
What you want to do is create a node class(or modify your existing class) to have these references, then when you want to search from something in the list, you only need a reference to the head of the series, then iterate through all of them, which you can do because they all have a link to the next in the series.
你想要做的是创建一个节点类(或修改现有的类)这些引用,然后当你想搜索列表中,你只需要一个参考系列的头部,然后遍历所有人,你可以做,因为他们都有一个链接到下一个系列的。
#1
0
You are not understanding the structure of a doubly linked list correct.
您不理解双链表的结构是否正确。
A doubly linked list consists of a series of nodes, each of which has a reference to the next in the series and the previous.
双链表由一系列节点组成,每一个节点都有一个指向序列中的下一个节点和前一个节点。
What you want to do is create a node class(or modify your existing class) to have these references, then when you want to search from something in the list, you only need a reference to the head of the series, then iterate through all of them, which you can do because they all have a link to the next in the series.
你想要做的是创建一个节点类(或修改现有的类)这些引用,然后当你想搜索列表中,你只需要一个参考系列的头部,然后遍历所有人,你可以做,因为他们都有一个链接到下一个系列的。