简体   繁体   English

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

[英]how can i store a node before deleting it in a linkedlist in java?

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?我开始检查 id 是否存在于链表中,因此我可以显示已存在节点的数据并将其存储在另一个链表中以打印已删除节点的数据,我无法制作新的 object 因为我不认为它会识别节点,它会产生一个全新的节点有什么想法吗?

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预期的 output 附在照片中 预期的输出时间表是节点数据

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:更新:我做了删除编码,但问题在于存储已删除的 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());



    }

here is the output:这是 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).显然,在这一点上,我们仍然有一个对 B 的引用(否则, B.next甚至都不是一个东西)。 Since B is the deleted node, just add it to the other linked list:由于B是被删除的节点,只需将其添加到另一个链表中即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM