Java:从ID中删除链接列表中的学生不起作用

发布于 2025-01-21 21:54:21 字数 11633 浏览 0 评论 0原文

我对此方法有问题,我想从链接列表中删除学生信息,但它不起作用,并且输出也与所需的一个不同 你能发现错误吗?

 public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {
            if (head == tail) {
                if (head.student.id.equals(id))
                    System.out.println(head.student.id + " Deleted!");
                head = tail = null;
                return;


            } else if (head.student.id.equals(id)) {
                // if “value” is in the first node
                System.out.println(head.student.id + " Deleted!");
                head = head.next;
                head.prev = null;
                return;


            } else if (tail.student.id.equals(id)) {
                // if “value” is in the last node tail = tail.prev;
                System.out.println(tail.student.id + " Deleted!");
                tail.next = null;
                return;

            } else {

                while (help_ptr != null) {
                    if (help_ptr.student.id.equals(id)) {
                        System.out.println(help_ptr.student.id + " Deleted!");
                        help_ptr.prev.next = help_ptr.next;
                        help_ptr.next.prev = help_ptr.prev;
                        return;
                    }
                    help_ptr = help_ptr.next;
                }

            }

所需的输出:

********* Delete Student **********

0000001 Deleted

********* Delete Student **********

0000003 Deleted

********* Delete Student **********

00000023 Not found in the list

我获得的输出:

************** Delete Student **************

************** Delete Student **************

************** Delete Student **************

主类:

 package classreport;
import java.util.*;
import java.io.*;


public class ClassReport {
        /*
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String id;
            String name;
            Double gpa;
            StudentList list = new StudentList();
            try {
                // create file object
                File f = new File("studentsList.txt");
                // create scanner object that reads from file
                Scanner sf = new Scanner(f);
                // start reading
                String line;
                while (sf.hasNextLine()) {
                    while ((line = sf.nextLine().trim()).isEmpty())
                        ;
                    name = line;
                    id = sf.nextLine().trim();
                    gpa = Double.parseDouble(sf.nextLine().trim());
                    // System.out.println("name: " + name + ", id: " + id + ", gpa: " + gpa);
                    list.add(name, id, gpa);
                }

                sf.close();
                // commands file
                File commands = new File("commands.txt");
                Scanner sc = new Scanner(commands);
                // output file
                File output = new File("output.txt");
 

               PrintWriter out = new PrintWriter(output);
                 System.out.println("*********************************************************\n"
                            + "\t\tData Structures Final Report\n"
                            + "******

***************************************************");

                // start reading
                while (sc.hasNext()) {
                    String command = sc.next().trim();
                    // System.out.println("command: " + command);
                    if (command.equals("TOTALSTUDENT")) {
                        int total = list.getTotal();
                        System.out.println("\nTotal number of students in the class = " + total);
                    } else if (command.equals("PRINTLIST")) {
                        System.out.println("\n************** Students List **************");
                        list.printList();
                    } else if (command.equals("ADDSTUDENT")) {
                        String newName = sc.nextLine();
                        newName = newName.trim();
                        String newId = sc.nextLine().trim();
                        Double newGpa = Double.parseDouble(sc.nextLine().trim());
                        System.out.println("\n************** Add Student **************");
                        list.addStudent(newName, newId, newGpa);
                    } else if (command.equals("STUDENTINFO")) {
                        String idToSearch = sc.next().trim();
                       System.out.println("\n************** Student Info **************");
                        list.studentInfo(idToSearch);
                    } else if (command.equals("DELETESTUDENT")) {
                        String idToDelete = sc.next().trim();
                        System.out.println("\n************** Delete Student **************");
                        list.deleteStudent(idToDelete);
                    } else if (command.equals("REVERSEPRINTLIST")) {
                        System.out.println("\n************** Reverse List **************");
                        list.printReverse();
                    } else if (command.equals("FINDLARGEST")) {
                        System.out.println("\n************** Highest GPA **************");
                        list.findLargest();
                    } else if (command.equals("QUIT")){
                        System.out.println(" ");
                        System.out.println("Quit");
                        System.out.println("*********************************************************");
                    }
                    else {
                        System.out.print("*********************************************************");
                        out.flush();
                        out.close();
                        sc.close();
                        sf.close();
                        System.exit(0);
                    }
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

studentList.java类: 包班级;

导入java.io..printwriter;

公共班级学生列表{ printwriter out;

void setOut(PrintWriter out) {
    this.out = out;
}

    Node head;
    Node tail;

    static class Node {
        Student student;
        Node next;
        Node prev;

        Node(Student s) {
            student = s;
            next = null;
        }
    }

    StudentList() {
        head = null;
    }

    StudentList(Student student) {
        head = new Node(student);
    }

    void printStudent (Student student){
        System.out.println(student.name);
       System.out.println(student.id);
        System.out.println(student.gpa);
    }

    void addStudent (String studentName, String id, Double GPA){
        Node newStudent = new Node(new Student(studentName, id, GPA));
        Node helpPtr = head;


        while (helpPtr != null && helpPtr.next != null) {
            helpPtr = helpPtr.next;
        }
        if (helpPtr != null) {
            helpPtr.next = newStudent;
            printStudent(helpPtr.next.student);
        } else
            head = newStudent;

        System.out.println("Student added successfully!");
    }
    void add (String studentName, String id, Double GPA){
        Node newStudent = new Node(new Student(studentName, id, GPA));
        Node temp = head;
        while (temp != null && temp.next != null) {
            temp = temp.next;
        }
        if (temp != null)
            temp.next = newStudent;
        else
            head = newStudent;

    }

    int getTotal () {
        // Counter for number of student.
        int numStudent = 0;
        // HelpPtr object for Node.
        Node helpPtr = head;
        // Check if head is empty or not.
        if (head == null) {
            // Return Counter.
            return numStudent;
        }
        // Loop for count student.
        while (helpPtr != null) {
            // If helpPtr not equal to null, then increment counter.
            numStudent++;
            // For get next node of student.
            helpPtr = helpPtr.next;
        } // End loop.
        // return counter.
        return numStudent;
    }

    void studentInfo (String id){
        Node helpPtr = head;
        while (helpPtr != null) {
            if (helpPtr.student.id.equals(id)) {
                printStudent(helpPtr.student);
                return;
            }
            helpPtr = helpPtr.next;
        }
        System.out.println(id + " Not found in the list");
    }

    public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {
            if (head == tail) {
                if (head.student.id.equals(id))
                    System.out.println(head.student.id + " Deleted!");
                head = tail = null;
                return;


            } else if (head.student.id.equals(id)) {
                // if “value” is in the first node
                System.out.println(head.student.id + " Deleted!");
                head = head.next;
                head.prev = null;
                return;


            } else if (tail.student.id.equals(id)) {
                // if “value” is in the last node tail = tail.prev;
                System.out.println(tail.student.id + " Deleted!");
                tail.next = null;
                return;

            } else {

                while (help_ptr != null) {
                    if (help_ptr.student.id.equals(id)) {
                        System.out.println(help_ptr.student.id + " Deleted!");
                        help_ptr.prev.next = help_ptr.next;
                        help_ptr.next.prev = help_ptr.prev;
                        return;
                    }
                    help_ptr = help_ptr.next;
                }

            }
            if (help_ptr == null) {
                System.out.println("There is no student with id " + id + " in the system.");
                return;
            }
        }


    }

    void findLargest () {
        Student largestGPA = null;
        Node helpPtr = head;
        if (head == null) {
            System.out.println("Not found in the list");
            return;
        }


        while (helpPtr != null && head != null) {
            if (largestGPA == null) {
                largestGPA = helpPtr.student;
            } else {
                if (largestGPA.gpa < helpPtr.student.gpa) {
                    largestGPA = helpPtr.student;
                }
            }
            helpPtr = helpPtr.next;
        }
        System.out.println("Student having highest GPA:");
        printStudent(largestGPA);
    }

    void printList () {
        Node helpPtr = head;
        while (helpPtr != null && head != null) {
            printStudent(helpPtr.student);
            helpPtr = helpPtr.next;
        }
    }


    public void printReverseLinkedList (Node head){
        if (null == head) {

            return;
        }
        printReverseLinkedList(head.next);
        printStudent(head.student);
    }
    public void printReverse () {
        printReverseLinkedList(head);
    }

}

Student.Java课:

    package classreport;

public class Student {
    String name;
    String id;
    Double gpa;

    Student(String name, String id, Double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
}

}

i have a problem with this method, i want to delete student information from the linked list but it is not working and the output also is not the same as the required one
can you spot the error?

 public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {
            if (head == tail) {
                if (head.student.id.equals(id))
                    System.out.println(head.student.id + " Deleted!");
                head = tail = null;
                return;


            } else if (head.student.id.equals(id)) {
                // if “value” is in the first node
                System.out.println(head.student.id + " Deleted!");
                head = head.next;
                head.prev = null;
                return;


            } else if (tail.student.id.equals(id)) {
                // if “value” is in the last node tail = tail.prev;
                System.out.println(tail.student.id + " Deleted!");
                tail.next = null;
                return;

            } else {

                while (help_ptr != null) {
                    if (help_ptr.student.id.equals(id)) {
                        System.out.println(help_ptr.student.id + " Deleted!");
                        help_ptr.prev.next = help_ptr.next;
                        help_ptr.next.prev = help_ptr.prev;
                        return;
                    }
                    help_ptr = help_ptr.next;
                }

            }

the output that is required:

********* Delete Student **********

0000001 Deleted

********* Delete Student **********

0000003 Deleted

********* Delete Student **********

00000023 Not found in the list

the output that i get:

************** Delete Student **************

************** Delete Student **************

************** Delete Student **************

main class:

 package classreport;
import java.util.*;
import java.io.*;


public class ClassReport {
        /*
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String id;
            String name;
            Double gpa;
            StudentList list = new StudentList();
            try {
                // create file object
                File f = new File("studentsList.txt");
                // create scanner object that reads from file
                Scanner sf = new Scanner(f);
                // start reading
                String line;
                while (sf.hasNextLine()) {
                    while ((line = sf.nextLine().trim()).isEmpty())
                        ;
                    name = line;
                    id = sf.nextLine().trim();
                    gpa = Double.parseDouble(sf.nextLine().trim());
                    // System.out.println("name: " + name + ", id: " + id + ", gpa: " + gpa);
                    list.add(name, id, gpa);
                }

                sf.close();
                // commands file
                File commands = new File("commands.txt");
                Scanner sc = new Scanner(commands);
                // output file
                File output = new File("output.txt");
 

               PrintWriter out = new PrintWriter(output);
                 System.out.println("*********************************************************\n"
                            + "\t\tData Structures Final Report\n"
                            + "******

***************************************************");

                // start reading
                while (sc.hasNext()) {
                    String command = sc.next().trim();
                    // System.out.println("command: " + command);
                    if (command.equals("TOTALSTUDENT")) {
                        int total = list.getTotal();
                        System.out.println("\nTotal number of students in the class = " + total);
                    } else if (command.equals("PRINTLIST")) {
                        System.out.println("\n************** Students List **************");
                        list.printList();
                    } else if (command.equals("ADDSTUDENT")) {
                        String newName = sc.nextLine();
                        newName = newName.trim();
                        String newId = sc.nextLine().trim();
                        Double newGpa = Double.parseDouble(sc.nextLine().trim());
                        System.out.println("\n************** Add Student **************");
                        list.addStudent(newName, newId, newGpa);
                    } else if (command.equals("STUDENTINFO")) {
                        String idToSearch = sc.next().trim();
                       System.out.println("\n************** Student Info **************");
                        list.studentInfo(idToSearch);
                    } else if (command.equals("DELETESTUDENT")) {
                        String idToDelete = sc.next().trim();
                        System.out.println("\n************** Delete Student **************");
                        list.deleteStudent(idToDelete);
                    } else if (command.equals("REVERSEPRINTLIST")) {
                        System.out.println("\n************** Reverse List **************");
                        list.printReverse();
                    } else if (command.equals("FINDLARGEST")) {
                        System.out.println("\n************** Highest GPA **************");
                        list.findLargest();
                    } else if (command.equals("QUIT")){
                        System.out.println(" ");
                        System.out.println("Quit");
                        System.out.println("*********************************************************");
                    }
                    else {
                        System.out.print("*********************************************************");
                        out.flush();
                        out.close();
                        sc.close();
                        sf.close();
                        System.exit(0);
                    }
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

studentlist.java class:
package classreport;

import java.io.PrintWriter;

public class StudentList {
PrintWriter out;

void setOut(PrintWriter out) {
    this.out = out;
}

    Node head;
    Node tail;

    static class Node {
        Student student;
        Node next;
        Node prev;

        Node(Student s) {
            student = s;
            next = null;
        }
    }

    StudentList() {
        head = null;
    }

    StudentList(Student student) {
        head = new Node(student);
    }

    void printStudent (Student student){
        System.out.println(student.name);
       System.out.println(student.id);
        System.out.println(student.gpa);
    }

    void addStudent (String studentName, String id, Double GPA){
        Node newStudent = new Node(new Student(studentName, id, GPA));
        Node helpPtr = head;


        while (helpPtr != null && helpPtr.next != null) {
            helpPtr = helpPtr.next;
        }
        if (helpPtr != null) {
            helpPtr.next = newStudent;
            printStudent(helpPtr.next.student);
        } else
            head = newStudent;

        System.out.println("Student added successfully!");
    }
    void add (String studentName, String id, Double GPA){
        Node newStudent = new Node(new Student(studentName, id, GPA));
        Node temp = head;
        while (temp != null && temp.next != null) {
            temp = temp.next;
        }
        if (temp != null)
            temp.next = newStudent;
        else
            head = newStudent;

    }

    int getTotal () {
        // Counter for number of student.
        int numStudent = 0;
        // HelpPtr object for Node.
        Node helpPtr = head;
        // Check if head is empty or not.
        if (head == null) {
            // Return Counter.
            return numStudent;
        }
        // Loop for count student.
        while (helpPtr != null) {
            // If helpPtr not equal to null, then increment counter.
            numStudent++;
            // For get next node of student.
            helpPtr = helpPtr.next;
        } // End loop.
        // return counter.
        return numStudent;
    }

    void studentInfo (String id){
        Node helpPtr = head;
        while (helpPtr != null) {
            if (helpPtr.student.id.equals(id)) {
                printStudent(helpPtr.student);
                return;
            }
            helpPtr = helpPtr.next;
        }
        System.out.println(id + " Not found in the list");
    }

    public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {
            if (head == tail) {
                if (head.student.id.equals(id))
                    System.out.println(head.student.id + " Deleted!");
                head = tail = null;
                return;


            } else if (head.student.id.equals(id)) {
                // if “value” is in the first node
                System.out.println(head.student.id + " Deleted!");
                head = head.next;
                head.prev = null;
                return;


            } else if (tail.student.id.equals(id)) {
                // if “value” is in the last node tail = tail.prev;
                System.out.println(tail.student.id + " Deleted!");
                tail.next = null;
                return;

            } else {

                while (help_ptr != null) {
                    if (help_ptr.student.id.equals(id)) {
                        System.out.println(help_ptr.student.id + " Deleted!");
                        help_ptr.prev.next = help_ptr.next;
                        help_ptr.next.prev = help_ptr.prev;
                        return;
                    }
                    help_ptr = help_ptr.next;
                }

            }
            if (help_ptr == null) {
                System.out.println("There is no student with id " + id + " in the system.");
                return;
            }
        }


    }

    void findLargest () {
        Student largestGPA = null;
        Node helpPtr = head;
        if (head == null) {
            System.out.println("Not found in the list");
            return;
        }


        while (helpPtr != null && head != null) {
            if (largestGPA == null) {
                largestGPA = helpPtr.student;
            } else {
                if (largestGPA.gpa < helpPtr.student.gpa) {
                    largestGPA = helpPtr.student;
                }
            }
            helpPtr = helpPtr.next;
        }
        System.out.println("Student having highest GPA:");
        printStudent(largestGPA);
    }

    void printList () {
        Node helpPtr = head;
        while (helpPtr != null && head != null) {
            printStudent(helpPtr.student);
            helpPtr = helpPtr.next;
        }
    }


    public void printReverseLinkedList (Node head){
        if (null == head) {

            return;
        }
        printReverseLinkedList(head.next);
        printStudent(head.student);
    }
    public void printReverse () {
        printReverseLinkedList(head);
    }

}

Student.java class:

    package classreport;

public class Student {
    String name;
    String id;
    Double gpa;

    Student(String name, String id, Double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2025-01-28 21:54:21
public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {

这种情况应该

if(head!=null)

不允许其余的代码执行

public void deleteStudent (String id){
        Node help_ptr = head;

        if (head == null) {

This condition should be

if(head!=null)

It's not allowing the rest of the code to execute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文