简体   繁体   English

SortedLinkedList移除两个节点之间头,尾的方法

[英]SortedLinkedList Remove methods head, tail, between two nodes

I need to remove a key in SortedLinkedList, but I can't. 我需要删除SortedLinkedList中的键,但不能删除。 There is no syntax error but code doesn't work. 没有语法错误,但是代码不起作用。 Deleting methods for begging, ending and middle of the node list. 乞求,结束和中间节点列表的删除方法。 Here is my remove methods: 这是我的删除方法:

    public void Remove(int key){

      LinkedListNode find = Find(key);
      if(find == null){
          System.out.println("Item is not found.");
      return;}

      LinkedListNode helper = new LinkedListNode();
        helper.next = head;
        LinkedListNode p = helper;

        while(p.next != null){
            if(p.next.key == key){
                LinkedListNode next = p.next;
                p.next = next.next; 
            }else{
                p = p.next;
            }
            return;
        } 
        noOfNodes--; 
    }

    public void Remove(LinkedListNode node)  {
        Remove(node.key);
    }

another code: 另一个代码:

     public void Remove(int key){
      LinkedListNode temp = Find(key);
      if(temp==null)
      {
          System.out.println("Item not found");
          return;
      }
      if(head==tail){
          head=null;
          tail=null;
      }

     else if(temp.key==head.key){
             head=temp.next;
                 head.prev=null;
         //
     }else if(temp.key==tail.key){
         tail=tail.prev;
         tail.next=null;
     }else { 
         temp.prev.next=temp.next;
         temp.next.prev=temp.prev;
     }
         noOfNodes--;
  } 

     public void Remove(LinkedListNode node){
     Remove(node.key);
  } 

Thanks in advance! 提前致谢!

Problem is here : 问题在这里:

while(p.next != null){
            if(p.next.key == key){
                LinkedListNode next = p.next;
                p.next = next.next; 
            }else{
                p = p.next;
            }
            return;  //--> why are you returning here ... Line 1
        } 

Returning from Line 1, means you have just checked the first value, weather its equal to key or not equal to key, you are just breaking the while loop and returning to calling method or parent method. 从第1行返回,意味着您刚刚检查了第一个值,使其等于键或不等于键,只是中断了while循环并返回到调用方法或父方法。 Which definitely doesn't remove the value. 这绝对不会删除价值。

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

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