【问题标题】:Deleting all occurrences in a circular linked list. (java)删除循环链表中的所有匹配项。 (爪哇)
【发布时间】:2021-05-11 19:05:47
【问题描述】:

我想实现一个方法,删除作为参数给出的某个值的所有出现。
我创建了 2 个临时元素并将它们引用到头部,我将使用它们浏览列表。我能够编写完整的代码,但我没有得到任何结果,我不知道问题出在哪里,我也没有收到错误。

任何帮助将不胜感激。

这是方法(我的方法):

public void deleteAllOccurrences(int value) {
        Element cur = this.head;
        Element prev = null;
        
        //deleting head
        while(cur != this.rear && cur.data == value) {
            cur.next = this.head;
            cur = this.head;
        }
        
        while(cur != this.rear) {
            while(cur != this.rear && cur.data != value) {
                prev = cur;
                cur = cur.next;
            }
            if(cur == this.rear)
                return;
            
            prev.next = cur.next;
            cur = prev.next;
        }
    }

这是整个班级,也许你需要检查一下:


public class CircularLinkedList {
    
class Element{
        
        int data;     // int type used as example
        Element next; // reference of the successor
        
        Element(int value) {
            this.data = value;
            this.next = this;
        }
        
    }

    private Element head = null;
    private Element rear = null;

    public CircularLinkedList() {
        this.head = this.rear = null;
    }
    
    
    public boolean isEmpty() {
        return head == null;
    }
    
    
    public boolean findValue(int value) {
        Element cur = this.head;
        
        while(cur != null) {
            if (cur.data == value)
                return true;
            cur = cur.next;
        }
        return false;
    }
    
    
    public int countValue(int value) {
        int c = 0; // counter
        Element cur = this.head;
        
        if(cur == null) 
            return 0;
        
        do {
            if(cur.data == value) 
                c++;
            cur = cur.next;
            
        }while (cur != this.head);
            
        return c;
    }
    
    
    @Override
    public String toString() {
        String str = "";
        Element cur = this.head;
        
            if(cur == null) 
                return "The list is empty";
        
            do {
                str += cur.data + " | ";
                cur = cur.next;
            
            }while (cur != this.head);
            
        return str;
        }
    
    
    
    public void insert(int value) {
        Element tmp = new Element (value);
        
        //special case: empty list
        if(this.head == null) {
            this.head = tmp;
            
        }else { // general case
            tmp.next = this.head.next;
            this.head.next = tmp; 
        }
            
    }
    
    
    public void deleteAtHead() {
        if(this.head == null) {
            return;
        }
        else {
            if(this.head != this.rear) {
                this.head = this.head.next;
                this.rear.next = this.head;
            }
        else {
            this.head = this.rear = null;
            }
        }
        
    }
    
        
    public boolean delete(int value) {
        Element cur = this.head;
        
        if(this.head.data == value) {                  //if the node to be deleted is head node
        
            while(cur.next != this.head) {         //iterate till the last node i.e. the node which is pointing to head         
                cur = cur.next;
            }
            cur.next = cur.next.next;       // update current node pointer to next node of head
            this.head = this.head.next;                //update head node
            return true;
        }
        else {                              // if node to be deleted is other than head node
        
            Element prev = cur;               // track previous node from current (node)
            while(cur.data != value) {       // find the node           
                prev = cur;
                cur = cur.next;
            }
            prev.next = cur.next; //updating next field of previous node to next of current node.current node deleted
            return true;
        }
    }
    
    
    
    public void deleteEven() {
//      if(this.head == null)
//          return;
//      
//      //case of deleting the head
//      if(this.head.data % 2 == 0) {
//          this.head.next = this.head;
//          this.rear.next = this.head;
//      if(this.head == null) 
//          this.rear = null;
//      }
//      
//      Element cur = this.head;
//      Element prev = cur;
//      while(cur != this.head) {
//          prev = cur;
//          cur = cur.next;
//      }
//      prev.next = cur.next;
        
        if(this.head == null)
            return;
        Element cur = this.head;
        while(cur != this.rear) {
            if(cur.data % 2 == 0)
                this.delete(cur.data);
            cur = cur.next;
        }
    }
    
    
    public void deleteLastOccurence(int value) {
        Element cur = this.head;
        Element prev = null;
        Element tmp = null;
        
        if(this.head == null)
            return;
        
        if(this.head.data == value) {
            this.head = null;
            return;
        }
        
        while(cur != this.rear) {
            if(cur.next != null && cur.next.data == value) {
                prev = cur;
                tmp = cur.next;
            }
            cur = cur.next;
        }
        prev.next = tmp.next;
    }
    
    
    public void deleteAllOccurrences(int value) {
        Element cur = this.head;
        Element prev = null;
        
        //deleting head
        while(cur != this.rear && cur.data == value) {
            cur.next = this.head;
            cur = this.head;
        }
        
        while(cur != this.rear) {
            while(cur != this.rear && cur.data != value) {
                prev = cur;
                cur = cur.next;
            }
            if(cur == this.rear)
                return;
            
            prev.next = cur.next;
            cur = prev.next;
        }
    }
    
    
//  public CircularLinkedList union(CircularLinkedList a, CircularLinkedList b) {
//      
//  }
//  
//  
//  public CircularLinkedList inter(CircularLinkedList a, CircularLinkedList b) {
//      
//  }
    
    
    public int countOddNbrs() {
        if(this.head == null)
            return 0;
        
        int c = 0;
        Element cur = this.head;
        do {
            if(cur.data % 2 != 0)
                c++;
            cur = cur.next;
        }while(cur != this.head);
        return c;
    }

    
//  public int findLastOccurence(int value) {
//      
//  }
    
    

    public static void main(String[] args) {

        CircularLinkedList list = new CircularLinkedList();
        list.insert(8);
        list.insert(2);
        list.insert(4);
        list.insert(3);
        list.insert(10);
        list.insert(5);
        list.insert(-8);
        list.insert(4);     
        System.out.println(list);
        
//      System.out.println(list.findValue(2)); // working
        
//      list.delete(2);  // working
//      System.out.println(list);
                
//      System.out.println(list.countOddNbrs());  //working
        
//      list.deleteEven();  // not working
//      System.out.println(list);
        
//      list.deleteAtHead();  // not working
//      System.out.println(list);
        
//      list.deleteLastOccurence(4);  //not working
//      System.out.println(list);
        
        list.deleteAllOccurrences(4);
        System.out.println(list);

    }

}

【问题讨论】:

    标签: java list abstract-class circular-list


    【解决方案1】:

    您的方法正在运行到无限循环。试试这样的:

    public void deleteAllOccurrences(int value) {
        Element cur = this.head;
        Element next = null;
    
        if (cur.data == value) {
            cur = cur.next;
            this.head = cur;
        }
    
        do {
            next = cur.next;
            if (next.data == value) {
                cur.next = next.next;
            }
            cur = next;
    
        } while (cur != this.head);
    
    }
    

    当头部是可移动元素时,您也应该处理角盒。

    【讨论】:

    • 它有效,但是为什么我们在循环列表中使用 do - while 循环而不是常规的 while 循环。
    • 我从您的 toString () 方法中复制了迭代。我以为你更喜欢 do-while 循环。
    • 我的导师在循环列表中使用 do - while 循环,所以我认为最好使用另一个循环。还是谢谢你
    【解决方案2】:

    您忘记在 insert() 方法中更新后部,导致您在查找后部节点时在 deleteAllOccurrences() 方法中遇到无限循环。它应该看起来像这样:

            if (this.head == null) {
                this.head = tmp;
                this.rear = tmp;
            } else { // general case
                tmp.next = this.head.next;
                this.head.next = tmp;
                this.rear = tmp.next;
            }
    

    另请注意,在 deleteAllOccurrences() 方法中,您没有考虑到最后一个节点可能具有查找的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2021-01-12
      • 2020-07-21
      • 2016-03-03
      相关资源
      最近更新 更多