双向链表的各种删除方法不起作用(java)(deleteEven(); deleteMultiple())

如何解决双向链表的各种删除方法不起作用(java)(deleteEven(); deleteMultiple())

我正在练习删除双向链表中元素的新方法。
我实现了一个方法 deleteEven() 删除列表中的所有偶数值,deleteHigher(int value) 删除列表中所有大于参数中的值的值,deleteMultiple(int value) 删除所有值参数中给定值的倍数的列表。

我没有收到错误,只是没有删除所有想要的值。
任何帮助表示赞赏。 (我还是编程新手)

这是我的代码:

public class List {

    class Element{
        int data;
        Element next;
        Element previous;
        
        Element(int value){
            this.data = value;
            this.next = null;
            this.previous = null;
        }
    }
    
    
    private Element head;
    private Element rear;
    
    
    public List() {
        this.head = null;
        this.rear = null;
    }
    
    public List(List a) {
        this();
        if(a.head == null)
            return;
        
        Element cur = a.head;
        Element tmp = new Element(cur.data);
        
        head = rear = tmp;
        cur = cur.next;
        
        while(cur != null) {
            tmp = new Element(cur.data);
            rear.next = tmp;
            tmp.previous = rear;
            rear = tmp;
            cur = cur.next;
        }
    }
    
    
    public List List1(List list,int value) {
        Element cur = list.head;
        List a = new List();
        
        if(list.head == null)
            return a;
        
        while(cur != null) {
            if(list.head.data < value)
                a.insert(list.head.data);
            cur = cur.next;
        }
        return a;
    }
    
    
    public String toString() {  
        String str;
        Element cur = this.head;
        
        if(this.head == null)
            str = "The linked list is empty";
        else {
            str = "The linked list contains ";
            while(cur != null) {
                str += cur.data + " | ";
                cur = cur.next;
            }
        }       
        return str; 
    }
    
    
    public int getLength() {
        Element cur = this.head;
        int c = 0;
        
        if(this.head == null)
            return 0;
        
        while(cur != null) {
            c++;
            cur = cur.next;
        }
        return c;
    }
    
    
    public int countOdd() {
        Element cur = this.head;
        int c = 0;
        
        if(this.head == null)
            return 0;
        
        while(cur != null) {
            if(cur.data % 2 != 0)
                c++;
            cur = cur.next;
        }
        return c;
    }
    
    
    public boolean insert(int value) {
        Element tmp = new Element(value);
        Element cur = this.head;
        
        if(this.isInList(value))
            return false;
        
        if(this.head == null) {
            this.head = this.rear = tmp;
            return true;
        }
        
        if(this.head.data < value) {
            tmp.next = this.head;
            this.head.previous = tmp;
            this.head = tmp;
            return true;
        }
        
        if(this.rear.data > value) {
            tmp.previous = this.rear;
            this.rear.next = tmp;
            this.rear = tmp;
            return true;
        }
        
        while(cur.next != null && cur.next.data > value) {
            
            cur = cur.next;
        }
        
        if(cur.data == tmp.data)
            return false;
        
        tmp.next = cur.next;
        tmp.previous = cur;
        cur.next.previous = tmp;
        cur.next = tmp;
        return true;
    }
    
    
    public boolean isInList(int value) {
        Element cur = this.head;
        
        if(this.head == null)
            return false;
        
        while(cur != null) {
            if(cur.data == value)
                return true;
            cur = cur.next;
        }
        return false;
    }
    
    
    public List inter(List a,List b) {
        Element curA = a.head;
        List c = new List();
        
        if(a.head == null || b.head == null)
            return c;
        
        while(curA != null ) {
            if(b.isInList(curA.data))
                c.insert(curA.data);
            curA = curA.next;
        }
        return c;
    }
    
    
    public List union(List a,List b) {
        Element curA = a.head;
        Element curB = b.head;
        List c = new List();
        
        while(curA != null && curB != null) {
            if(curA.data > curB.data) {
                c.insert(curA.data);
                curA = curA.next;
            }else {
                c.insert(curB.data);
                curB = curB.next;
            }       
        }
        
        while(curA != null) {
            c.insert(curA.data);
            curA = curA.next;
        }
        
        while(curB != null) {
            c.insert(curB.data);
            curB = curB.next;
        }
        return c;
    }
    
    
    public int findLastOccurence(int value) {
        Element cur = this.head;
        int length = this.getLength() - 1;
        
        if(this.head == null)
            return -100;
        
        while(cur != null) {
            if(cur.data == value)
                length--;
            cur = cur.previous;
        }
        return length;
    }
    
    
    public void deleteEven() {
        Element cur = this.head;
        
        //empty list
        if(this.head == null)
            return;
        
        //deleting the head
        while(this.head != null && this.head.data % 2 == 0) {
            this.head = this.head.next;
        
        //case of if after deleting the head,the list becomes empty
        if(this.head == null) 
            this.rear = null;
        else 
            this.head.previous = null;
            return;
        }
            
        while(this.rear != null && this.rear.data % 2 == 0) {
            this.rear = this.rear.previous;
            this.rear.next = null;
        }
        
        while(cur != null ) {
            if(cur.data % 2 == 0) {
                cur.previous.next = cur.next;
                cur.next.previous = cur.previous;
            }
            cur = cur.next;
        }
        
        if ((cur == null) )
            return;
    }
    
    
    public boolean deleteHigher(int value) {
        Element cur = this.head;
        
        if(this.head == null)
            return false;
        
        while(this.head != null && this.head.data > value) {
            this.head = this.head.next;
        }
        
        if(this.head == null) {
            this.rear = null;
            return true;
        }else {
            this.head.previous = null;
        }
            
        while(this.rear != null && this.rear.data > value)
            this.rear = this.rear.previous;
        
        while(cur != null && cur.data < value) {            
            cur = cur.next;
        }
        
        if ((cur == null) || (cur.data != value))
            return false;
        
        cur.previous.next = cur.next;
        cur.next.previous = cur.previous;
        
        return true;
    }
    
    
    public void deleteMultipleOf(int value) {
        Element cur = this.head;
        
        if(this.head == null)
            return ;
        
        //case of deleting the head
        while(this.head != null && this.head.data % value == 0) {
            this.head = this.head.next;     
        
        //case of if the head is deleted,the list becomes empty
        if(this.head == null) 
            this.rear = null;
        else 
            this.head.previous = null;
        }
        
        //case of deleting the rear
        while(this.rear != null && this.rear.data % value == 0) {
            this.rear = this.rear.previous;
        }
        
        while(cur != null && cur.data % value != 0) 
            cur = cur.next;
        
        if(cur == null || cur.data % value != 0)
            return ;
        
        cur.next.previous = cur.previous;
        cur.previous.next = cur.next;

    }
    
    
    public void deleteLAstOccurence(int value) {
        Element cur = this.head;
        
        if(this.head == null)
            return ;
        
        while(cur != null) {
            if(cur.data == value) {
                cur.next.previous = cur.previous;
                cur.previous.next = cur.next;
                 
            }
            cur = cur.previous;
        }
    }
    
    
    public void deleteAllOccurences(int value) {
        Element cur = this.head;
        
        //deleting head
        while(cur != null && cur.data == value) {
            cur.next = this.head;
            cur.next.previous = null;
            cur = this.head;
        }
        
        while(cur != null) {
            while(cur != null && cur.data != value) {
                cur = cur.next;
            }
            if(cur == null)
                return;
            
            cur.next.previous = cur.previous;
            cur.previous.next = cur.next;
        }
    }
    
    
    
    public static void main(String[] args) {
        List l = new List();
        List l1 = new List();
//      List l2 = new List();
//      List l3 = new List();

        l.insert(2);
        l.insert(4);
        l.insert(6);
        l.insert(7);
        l.insert(3);
        l.insert(2);
        System.out.println(l);
        
        l1.insert(4);
        l1.insert(6);
        l1.insert(7);
        l1.insert(0);
        l1.insert(10);
//      System.out.println(l1);
//      
//      System.out.println(l.isInList(6));      
//      
//      System.out.println(l2.inter(l,l1));
//      
//      System.out.println(l3.union(l,l1));
        
//      l.deleteEven();        //not everything is being deleted
//      System.out.println(l);
    
//      l.deleteHigher(2);    //working
//      System.out.println(l);
        
        l.deleteMultipleOf(2); // not working
        System.out.println(l);
        
//      l.deleteLAstOccurence(3);// not working
//      System.out.println(l);
        
//      System.out.println(l.findLastOccurence(-1));
    }

}

解决方法

public void deleteEven() {
        
        //empty list
        if(this.head == null)
            return;
        
        //deleting the head
        while(this.head != null && this.head.data % 2 == 0) 
            this.head = this.head.next;
            
        
        //case of if after deleting the head,the list becomes empty
        if(this.head == null) {
            this.rear = null;
        }else 
            this.head.previous = null;          
        
            
        while(this.rear != null && this.rear.data % 2 == 0) {
            this.rear = this.rear.previous;
            this.rear.next = null;
        }
        
        Element cur = this.head;
        while(cur.next != null ) {
            if(cur.next.data % 2 == 0) {
                cur.next = cur.next.next;
                cur.next.previous = cur;
            }else 
            cur = cur.next;
        }
    }
    
    
    public boolean deleteHigher(int value) {
        
        if(this.head == null)
            return false;
        
        while(this.head != null && this.head.data > value) {
            this.head = this.head.next;
        }
        
        if(this.head == null) {
            this.rear = null;
            return true;
        }else {
            this.head.previous = null;
        }
            
        while(this.rear != null && this.rear.data > value)
            this.rear = this.rear.previous;
        
        Element cur = this.head;
        while(cur.next != null) {
            if(cur.next.data > value) {
                cur.next = cur.next.next;
                cur.next.previous = cur;
            }else
            cur = cur.next;
        }
        return true;
    }
    
    
    public void deleteMultipleOf(int value) {       
        if(this.head == null)
            return ;
        
        //case of deleting the head
        while(this.head != null && this.head.data % value == 0) 
            this.head = this.head.next;     
        
        //case of if the head is deleted,the list becomes empty
        if(this.head == null) {
            this.rear = null;
        }else 
            this.head.previous = null;
                
        //case of deleting the rear
        while(this.rear != null && this.rear.data % value == 0) {
            this.rear = this.rear.previous;
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-