Frage

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?

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"));
}
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top