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

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

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了刪除循環(huán)鏈表中的所有匹配項(xiàng)。(Java)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我要實(shí)現(xiàn)一個(gè)方法,該方法刪除作為參數(shù)給定的特定值的所有匹配項(xiàng)。
我創(chuàng)建了2個(gè)臨時(shí)元素,并將它們引用到頭部,我將使用它們?yōu)g覽列表。我能夠編寫(xiě)完整的代碼,但沒(méi)有得到任何結(jié)果,我不知道問(wèn)題出在哪里,也沒(méi)有收到錯(cuò)誤。

如有任何幫助,我們將不勝感激。

這是方法(我的方法):

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

這是整個(gè)類,您可能需要檢查一些內(nèi)容:


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

    }

}

推薦答案

您的方法正在無(wú)限循環(huán)中運(yùn)行。試著這樣做:

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

}

當(dāng)頭部是可拆卸的元素時(shí),您也應(yīng)該處理邊角情況。

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

分享到:
標(biāo)簽:Java 刪除 匹配 循環(huán) 鏈表
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定