删除循环链表中的所有匹配项。(Java)

2022-07-04 00:00:00 list abstract-class circular-list java

我要实现一个方法,该方法删除作为参数给定的特定值的所有匹配项。
我创建了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);

    }

}

解决方案

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

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);

}

当头部是可拆卸的元素时,您也应该处理边角情况。

相关文章