美文网首页
3.循环链表

3.循环链表

作者: 据分专家 | 来源:发表于2020-10-07 15:08 被阅读0次

    双向链表的增删改查功能

    
    package com.zhiyang.linkedlist;
    
    public class DoubleLinkedListDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            HeroNode2 hero1 = new HeroNode2(1, "song", "及时雨");
            HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
            HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
            HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
    
            DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
    //      doubleLinkedList.addNode(hero1);
    //      doubleLinkedList.addNode(hero2);
    //      doubleLinkedList.addNode(hero3);
    //      doubleLinkedList.addNode(hero4);
    
            doubleLinkedList.addByOrder(hero1);
            doubleLinkedList.addByOrder(hero4);
            doubleLinkedList.addByOrder(hero3);
            doubleLinkedList.addByOrder(hero2);
            doubleLinkedList.showList();
    
    //      HeroNode2 newnode=new HeroNode2(4, "公孙胜", "入云龙");
    //      doubleLinkedList.update(newnode);
    //      doubleLinkedList.showList();
        }
    
    }
    
    class DoubleLinkedList {
    
        private HeroNode2 head = new HeroNode2(0, "", "");
    
        public HeroNode2 getHead() {
            return head;
        }
    
        // 链表遍历
        public void showList() {
            // 判断链表是否为空
            if (head.next == null) {
                return;
            }
    
            HeroNode2 tempHeroNode = head.next;
            while (true) {
                // 判断是否已经到达最后一个节点
                if (tempHeroNode == null) {
                    break;
                }
                // 打印
                System.out.println(tempHeroNode);
                //
                tempHeroNode = tempHeroNode.next;
    
            }
    
        }
    
        public void addNode(HeroNode2 heronode) {
            HeroNode2 tempHeroNode = head;
            while (true) {
                // 获取最后的节点
                if (tempHeroNode.next == null) {
                    break;
                }
                tempHeroNode = tempHeroNode.next;
    
            }
            // 将最后节点的next
            tempHeroNode.next = heronode;
            heronode.pre = tempHeroNode;
    
        }
    
        //
        // 根据编号添加节点
        public void addByOrder(HeroNode2 heronode) {
            HeroNode2 tmpHeroNode = head;
            boolean flag = false;
            while (true) {
                // 如果是最后一个节点,证明需要插入到最后,直接退出
                if (tmpHeroNode.next == null) {
                    break;
                }
                // 找到插入位置
                if (tmpHeroNode.next.no > heronode.no) {
                    break;
                } else if (tmpHeroNode.next.no == heronode.no) {
                    flag = true;
                    break;
                }
                tmpHeroNode = tmpHeroNode.next;
    
            }
            // 判断是否能够插入
            if (flag) {
                System.out.println("待插入节点已存在");
    
            } else {
                heronode.next = tmpHeroNode.next;
                tmpHeroNode.next = heronode;
                heronode.pre = tmpHeroNode;
            }
    
        }
    
        // 节点内容修改
        // 修改节点信息,编号不能变
        // 根据节点编号修改
        public void update(HeroNode2 newheroNode) {
            // 判断链表是否为空
            if (head.next == null) {
                System.out.println("链表为空");
                return;
            }
            HeroNode2 tmpHeroNode = head.next;
            boolean flag = false;
            while (true) {
                if (tmpHeroNode == null) {
                    break;
                }
                if (tmpHeroNode.no == newheroNode.no) {
                    flag = true;
                    break;
                }
                tmpHeroNode = tmpHeroNode.next;
            }
    
            if (flag) {
                tmpHeroNode.name = newheroNode.name;
                tmpHeroNode.nickname = newheroNode.nickname;
    
            } else {
                System.out.printf("编号为%d的节点不存在\n", newheroNode.no);
            }
    
        }
    
        // 从双向链表中删除一个节点
        public void deletenode(int no) {
            if (head.next == null) {
                System.out.println("链表为空");
                return;
            }
            // 找到需要删除节点的前一个节点。
            HeroNode2 tmpHeroNode = head.next;
            boolean flag = false;
            while (true) {
                if (tmpHeroNode == null) {
                    break;
                }
    
                if (tmpHeroNode.no == no) {
                    flag = true;
                    break;
                }
                tmpHeroNode = tmpHeroNode.next;
    
            }
    
            if (flag) {
                tmpHeroNode.pre.next = tmpHeroNode.next;
                // 如果是最后一个节点就不能执行下面这句,否则会执行空指针异常
                if (tmpHeroNode.next != null) {
                    tmpHeroNode.next.pre = tmpHeroNode.pre;
                }
    
            } else {
                System.out.printf("编号为%d的节点不存在,无法删除\n", no);
            }
    
        }
    
    }
    
    class HeroNode2 {
        public int no;
        public String name;
        public String nickname;
        public HeroNode2 next;
        public HeroNode2 pre;
    
        public HeroNode2(int no, String name, String nickname) {
            super();
            this.no = no;
            this.name = name;
            this.nickname = nickname;
        }
    
        @Override
        public String toString() {
            return "HeroNode2 [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
        }
    
    }
    

    相关文章

      网友评论

          本文标题:3.循环链表

          本文链接:https://www.haomeiwen.com/subject/pyvfpktx.html