[英]how can i store a node before deleting it in a linkedlist in java?
我正在尝试将要删除的节点存储在另一个链表中,以便在完全删除节点之前打印有关已删除节点的信息如何将所需节点存储在另一个链表中? 我开始检查 id 是否存在于链表中,因此我可以显示已存在节点的数据并将其存储在另一个链表中以打印已删除节点的数据,我无法制作新的 object 因为我不认为它会识别节点,它会产生一个全新的节点有什么想法吗?
这是我到目前为止尝试的代码
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;
}
预期的 output 附在照片中 预期的输出时间表是节点数据
更新:我做了删除编码,但问题在于存储已删除的 object 因为你们都帮助了我删除的)这是一种方法 foe]r 在我的链接列表 class 中删除 object:
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());
}
这是 output:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.