如何创建多个数据类型的双向链表

问题描述 投票:0回答:1

我目前正在编写一个创建学生的程序,并根据他们的自然顺序(姓氏,名字,GPA,然后学生ID)将它们存储在双向链表中。我刚刚开始使用泛型,以及它们是如何工作的,所以我有点失落。我相信我的大多数代码都在工作;我需要帮助的唯一部分是在我的双向链表类的主方法中将学生(有多种数据类型)添加到我的列表中。任何帮助是极大的赞赏!这是我的学生,双向链表,节点类以及我正在读取的输入文件的片段以及每个学生的数据:

学生班:

public class Student{
long studentID;
String firstName;
String lastName;
float GPA;

public Student(String lastName, String firstName, float GPA, long studentID){
    this.lastName = lastName;
    this.firstName = firstName;
    this.GPA = GPA;
    this.studentID = studentID;
}

public int compareTo(Student s){
    int result = this.lastName.compareTo(s.lastName);
    if(result == 0){
        result = this.firstName.compareTo(s.firstName);
        if(result == 0){
            result = Float.compare(this.GPA, s.GPA);
            if(result == 0){
                result = Long.compare(this.studentID, s.studentID);
            }
        }
    }
    return result;
}

public String toString(){
    return this.lastName + ", " + this.firstName +
     " GPA: " + this.GPA + " ID: " + this.studentID;
}

}

节点类:

public class Node<T>{
Node<T> previous;
Node<T> next;
Student data;

public Node(Student data){
    this(data, null, null);
}

public Node(Student data, Node<T> previous, Node<T> next){
    this.data = data;
    this.previous = previous;
    this.next = next;
}
}

双重链接列表类:

import java.io.*;
import java.util.*;
import csci1140.*;

public class DoublyLinkedList<T> implements Iterable<Node>{
private Node root;
private Node tail;
private Node previous;

 private class ListIterator implements Iterator<Node>{
    Node current = root;
    public boolean hasNext(){
        return (current != null);
    }


    public Node next(){
        Node answer;

        answer = current;
        current = current.next;

        return answer;
    }

} 

 public Iterator<Node> iterator(){
    ListIterator listIterator = new ListIterator();
    return listIterator;
}  

public void add(T data){
    Node<Student> newNode = new Node<Student>(data);

    if(root == null){
        root = newNode;
        tail = root;
        return;
    }

    Node current = root;
    for( ; current!= null; current = current.next){
        if(newNode.data.compareTo(current.data)<= 0){
            break;
        }

    }

    if(previous == null){
        previous.next = newNode;
        newNode.next = current;
        if(current == null){
            tail = newNode;
        }
    } else {
        newNode.next = root;
        root = newNode;
    }
}

public static final void main(String[] args){

   FileInputStream fileIn = null;
    try{ 
        fileIn = new FileInputStream("student_input.txt"); 
        System.setIn(fileIn);            
    } catch(FileNotFoundException fnfe){ 
        fnfe.printStackTrace(System.err); 
    } 

    //Do work here to create list of students

    }
    try{                        
        fileIn.close();         
    } catch(Exception e){}            
}
}

Student_input.txt:

1000
Lisa
Licata
2.28
1001
Shelley
Santoro
1.56
1002
Ok
Ota
3.33
1003
Cindi
Caggiano
1.65
java generics doubly-linked-list
1个回答
0
投票

仍然不完全确定,也许是一些变化。

特别是这在第一个Node更大之前插入,我仍然不确定在这种情况下泛型是什么,T需要是扩展Student的东西(它需要compareTo方法):

public void add(T data) {
    for(Node<T> current = root; current != null; current = current.next) {
        if (data.compareTo(current.data) <= 0) {
            current = new Node<>(data,current.previous,current);
            if(null == current.previous){
                root = current;
            }else {
                current.previous.next = current; 
            }
            if(null == current.next){
               tail = current; 
            } else {
               current.next.previous = current; 
            }
            return;
        }
    }
    tail = new Node<>(data,tail,null);
    if(null == tail.previous) root=tail;
}

所以你的列表可能看起来像这样(为了确保T有compareTo方法):

public class DoublyLinkedList<T extends Student> implements Iterable<Node<T>> {
...
}

所有这些(将Node作为一个单独的文件就像你做的那样更好 - 但为了简洁起见,我将它放入列表中):

public class DoublyLinkedList<T extends Student> implements Iterable<Node<T>> {

    public static class Node<S> {

        Node<S> previous;
        Node<S> next;
        S data;

        public Node(S data) {
            this(data, null, null);
        }

        public Node(S data, Node<S> previous, Node<S> next) {
            this.data = data;
            this.previous = previous;
            this.next = next;
        }
    }
    private Node<T> root = null;
    private Node<T> tail = null;

    private class ListIterator implements Iterator<Node<T>> {

        Node<T> current = root;

        @Override
        public boolean hasNext() {
            return (current != null);
        }

        @Override
        public Node<T> next() {
            Node<T> answer;

            answer = current;
            current = current.next;

            return answer;
        }

    }

    @Override
    public Iterator<Node<T>> iterator() {
        ListIterator listIterator = new ListIterator();
        return listIterator;
    }

    public  void add(T data) {
        for(Node<T> current = root; current != null; current = current.next) {
            if (data.compareTo(current.data) <= 0) {
                current = new Node<>(data,current.previous,current);
                if(null == current.previous){
                    root = current;
                }else {
                    current.previous.next = current; 
                }
                if(null == current.next){
                   tail = current; 
                } else {
                   current.next.previous = current; 
                }
                return;
            }
        }
        tail = new Node<>(data,tail,null);
        if(null == tail.previous) root=tail;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.