【问题标题】:How can i implement a Linked List and another class我如何实现链接列表和另一个类
【发布时间】:2012-03-19 21:15:59
【问题描述】:

我有一个我写的 LinkedList 类,我还有另一个学生类。每个学生都有一个ID、姓名、GPA……

如何将这些变量作为 1 个元素添加到链表中。好像我需要在该链表中搜索一个元素,我可以显示该学生的所有信息。

如果您需要进一步解释,请复制一些代码。

【问题讨论】:

  • 请这样做,但基本上每个节点都应该有该信息,例如一个节点将有:{nextNode, id, name, gpa, ...} 或者如果你愿意,{nextNode, element } 其中一个元素可能是一个学生。

标签: java class linked-list


【解决方案1】:

按照 java.util.List 示例:

List<Student> roster = new ArrayList<Student>();

只需替换您自己的引用和实现类:

YourLinkedList roster = new YourLinkedList();
Student s = new Student();
roster.add(s);

至于搜索给定的学生实例,我会编写一个迭代器,它可以接受一个 Comarator 并返回链接列表的过滤版本。

【讨论】:

    【解决方案2】:

    我猜你有StudentMyLinkedList 类,现在你想使用它们,因为你的链表可能只支持整数项。你可以使用这样的东西

    public class Student {
        private int id;
        private String name;
        private double gpa;
        //getters and setters...
    }
    

    现在您需要添加 Student 类作为链表中节点的信息:

    public class MyLinkedList {
        class MyNode {
            private Student student;
            private MyNode next;
            public MyNode(Student student) {
                this.student = student;
                this.next = null;
            }
            public Student getStudent() {
                return this.student;
            }
        }
        private MyNode root;
        private int size;
        public MyLinkedList {
            this.root = null;
        }
        public void add(Student student) {
            //this is just one way to implement the insert method
            //you can rewrite to use your own implementation
            MyNode node = new MyNode(student);
            if (root == null) {
                root = node;
            } else {
                MyNode currentNode = root;
                while (currentNode.next != null) {
                    currentNode = currentNode.next;
                }
                currentNode.next = node;
            }
            size++;
        }
        public void printData() {
            //method used to print the content of the linked list
            MyNode currentNode = root;
            while (currentNode != null) {
                Student student = currentNode.getStudent();
                System.out.println("Id: " + student.getId + " Name: " + student.getName());
                currentNode = currentNode.next;
            }
        }
    }
    

    通过这种方式,您可以使用Student 类实现一个新的链表。让我们试试代码:

    public static void main(String args[]) {
        MyLinkedList mll = new MyLinkedList;
        Student student;
        student = new Student();
        student.setId(1);
        student.setName("Luiggi");
        mll.add(student);
        student = new Student();
        student.setId(2);
        student.setName("Mendoza");
        mll.add(student);
        mll.printData();
    }
    

    这只是一个示例,您可以改进代码,但您了解主要思想。

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2013-08-12
      • 2017-10-24
      相关资源
      最近更新 更多