gpt4 book ai didi

java - 在java中的链表中删除节点之前如何存储节点?

转载 作者:行者123 更新时间:2023-12-02 09:27:41 25 4
gpt4 key购买 nike

我正在尝试将要删除的节点存储在另一个链表中,以便在完全删除该节点之前打印有关已删除节点的信息如何将想要的节点存储在另一个链表中?我开始检查 id 是否存在于链表中,以便我可以显示现有节点的数据并将其存储在另一个链表中以打印已删除节点的数据,我无法创建新对象,因为我不认为它将识别该节点并创建一个全新的节点有什么想法吗?

这是我到目前为止尝试过的代码

 System.out.println("1- Remove student by his/her ID\n" +
"2- Remove students that have not to complete the minimum requirement");
int cho=input.nextInt();
switch(cho){
case 1:
System.out.print("Please enter the student ID that you would like to remove:");
String id=input.next();
if(list.check_std(id)==false){//checking if it does exist
SeniorProjectSystem deletelist=new SeniorProjectSystem();
//the new linked list
//i stopped here
}
break;
case 2:
//did not done this yet break;
}

预期输出附在照片中 the expected out put the schedule is the node data

更新:我做了删除编码,但问题在于存储已删除的对象,因为你们都帮助了我,我尝试制作一种方法来存储对象并打印它,但它效果不佳,它打印了整个对象(删除了未删除的对象) )这是删除我的链接列表类中的对象的方法:

public void RemoveStudent(String id){
stuhead = RemoveStudentID(stuhead,id);

}
private Student RemoveStudentID(Student s, String id){
if (!isStuEmpty()) {
// IF the first node (at the head) has the data value we are wanting to delete
// we found it. Delete by skipping the node and making head point to the next node.
if (stuhead.getStudentID().equals(id)) {
stuhead = stuhead.getNext();
}
else {
Student helpPtr = stuhead;
// Traverse to correct deletion point
while (helpPtr.getNext() != null) {
if (helpPtr.getNext().getStudentID().equals(id)) {
helpPtr.setNext(helpPtr.getNext().getNext());
break; }
helpPtr = helpPtr.getNext();
}

printDeletedStu(helpPtr);}
return stuhead;
}
return stuhead;
}

public void printDeletedStu(Student s){
String.format(s.getStudentID()," ",s.getResearch_intrest()," ",s.getTopic()," ",s.getCourse()," ",s.isApproval()," ",s.getSupervisorID());



}

这是输出:

1.Add a new student.
2.Print supervisor list.
3.Print student list.
4. Add research topic.
5.Remove student.
6.Print senior project list in ascending order
7."Exit.
Enter your choice: 5
1- Remove student by his/her ID
2- Remove students that have not to complete the minimum requirement
1
Please enter the student ID that you would like to remove:1777
Student ID ,Research interest ,Suggsted topic ,courses ,Approval ,SupervisorID
1723 ,Asma ,artificial intelligent,How the machine thinks: intelligent learning ,[1, 1, 1, 1, 1, 1] ,true ,00023
1743 ,Roaa ,artificial intelligent, ,[1, 1, 1, 0, 0, 0] ,false ,0
1003 ,Sara ,network , ,[1, 1, 1, 1, 1, 1] ,false ,00013
1777 ,Rania ,database , ,[1, 1, 1, 0, 0, 0] ,false ,0

deleted
1.Add a new student.
2.Print supervisor list.
3.Print student list.
4. Add research topic.
5.Remove student.
6.Print senior project list in ascending order
7."Exit.
Enter your choice: 3
Student ID ,Research interest ,Suggsted topic ,courses ,Approval ,SupervisorID
1723 ,Asma ,artificial intelligent,How the machine thinks: intelligent learning ,[1, 1, 1, 1, 1, 1] ,true ,00023
1743 ,Roaa ,artificial intelligent, ,[1, 1, 1, 0, 0, 0] ,false ,0
1003 ,Sara ,network , ,[1, 1, 1, 1, 1, 1] ,false ,00013

1.Add a new student.
2.Print supervisor list.
3.Print student list.
4. Add research topic.
5.Remove student.
6.Print senior project list in ascending order
7."Exit.
Enter your choice: 7
BUILD SUCCESSFUL (total time: 33 seconds)

最佳答案

请记住,以下是我们删除链表中的节点的方法:

do:
find B // which is to-be-deleted
Start:
A -> B -> C

do:
A.next = B.next

Result:
A -> C

显然,此时我们仍然有对 B 的引用(否则,B.next 根本就不是一个东西)。由于B是被删除的节点,只需将其添加到另一个链表中即可:

linked-list deletedNodes = ...;
do:
deletedNodes.append(B);

关于java - 在java中的链表中删除节点之前如何存储节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com