繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM