I'am trying to store the node that I want to delete it in another linked list so I can print the information about the deleted node before I completely delete the node how can I store the wanted node in another linkedlist? I started checking if the id exists in the linked list so I can show the data of the existed node and store it in another linked list to print the data of the deleted node, i can not make a new object because i don't think it will recognize the node and it will make a whole new node any ideas?
here is the code i tried so far
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 output is attached in a photo the expected out put the schedule is the node data
update: i did the remove coding but the problem is in storing the deleted object since you all helped me i tried to make a method to store the object and print it but it did not work very well it prints the whole objects (deleted an non deleted ones) here is a method foe]r deleting an object in my link list class:
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());
}
here is the 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)
Remember, here's how we delete a node in a linked list:
do:
find B // which is to-be-deleted
Start:
A -> B -> C
do:
A.next = B.next
Result:
A -> C
Clearly, at this point, we still have a reference to B (otherwise, B.next
isn't even a thing). Since B
is the deleted node, just add it to the other linked list:
linked-list deletedNodes = ...;
do:
deletedNodes.append(B);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.