java:按id从链表中删除学生无效



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

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"
+ "ttData Structures Final Reportn"
+ "******
***************************************************");
// 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类:包classreport;

进口java.io.PrintWriter;

公共类StudentList {PrintWriter;

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;
}

}

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

这个条件应该是

if(head!=null)

不允许其他代码执行

最新更新