双链表,Node无法解析为一个类型

时间:2014-03-30 17:37:37

标签: java algorithm data-structures linked-list nodes

我试图建立一个双向列表类。当我尝试编译时,我得到错误"节点无法解析为类型"。此外,我不确定我将在哪里开始制作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"));
}
}

1 个答案:

答案 0 :(得分:0)

您不了解双向链表的结构是否正确。

双向链表包含一系列节点,每个节点都引用了系列中的下一个节点和前一个节点。

您要做的是创建一个节点类(或修改您现有的类)以获得这些引用,然后当您想要从列表中的某些内容进行搜索时,您只需要引用该系列的头部,然后迭代所有这些,你可以做,因为它们都有一个链接到系列中的下一个。