日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了刪除循環鏈表中的所有匹配項。(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);

}

當頭部是可拆卸的元素時,您也應該處理邊角情況。

這篇關于刪除循環鏈表中的所有匹配項。(Java)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:Java 刪除 匹配 循環 鏈表
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定