博客
关于我
单链表的增删查改
阅读量:631 次
发布时间:2019-03-14

本文共 2465 字,大约阅读时间需要 8 分钟。

class SingleLinkList4{       //创建头节点    private Hero head = new Hero(0);    public Hero getHead() {           return head;    }    //删除节点    public void del(int no){           if (head.getNext() == null){               System.out.println("链表为空!!!");            return;        }        boolean flag = false;        Hero temp = head;        while (true){               if (temp.getNext() == null){                   break;            }    if (temp.getNext().getNo() == no){   //找到要删除节点的前一个节点                flag = true;                break;            }            temp = temp.getNext();        }        if (flag){               temp.setNext(temp.getNext().getNext());        }else {               System.out.println("没有找到这个节点!!!");        }    }    //添加节点到链表中    public void add(Hero node){           Hero temp = head;        while (true){               if (temp.getNext() == null){   //找到链表的最后                break;            }            temp = temp.getNext();//像后移动        }        temp.setNext(node);    }    //按照编号顺序添加链表    public void addByOrder(Hero node){           Hero temp = head;        boolean flag = false;        while (true){               if (temp.getNext() == null){                   break;            }            //如果要加入的节点的编号比链表中编号要小            if (temp.getNext().getNo() >  node.getNo()){                   break;            }else if(temp.getNext().getNo() ==  node.getNo()){                   flag = true;                break;            }            temp= temp.getNext();        }        if (flag){               System.out.println("链表中已经存在此节点!!");        }else {               node.setNext(temp.getNext()); //像头插法            temp.setNext(node);        }    }    //展示链表里面的数据    public void show(){           if (head.getNext() == null){               System.out.println("链表为空!!!");            return;        }        Hero temp = head.getNext();        while (temp != null){   //当前节点不为空            System.out.println(temp);            temp = temp.getNext();        }    }}class Hero{       private int no;    private Hero next;    @Override    public String toString() {           return "Hero{" +                "no=" + no +                '}';    }    public int getNo() {           return no;    }    public void setNo(int no) {           this.no = no;    }    public Hero getNext() {           return next;    }    public void setNext(Hero next) {           this.next = next;    }    public Hero(int no) {           this.no = no;    }}

转载地址:http://rzooz.baihongyu.com/

你可能感兴趣的文章
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mysql 1045解决方法
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
mui折叠面板点击事件跳转
查看>>
MySQL 8 公用表表达式(CTE)—— WITH关键字深入用法
查看>>
mysql 8 远程方位_mysql 8 远程连接注意事项
查看>>
MUI框架里的ajax的三种方法
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
Mysql 8.0 新特性
查看>>
MultCloud – 支持数据互传的网盘管理
查看>>
MySQL 8.0.23中复制架构从节点自动故障转移
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>