码迷,mamicode.com
首页 > 其他好文 > 详细

双向链表

时间:2019-12-22 12:23:06      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:删除   bool   pac   link   break   head   err   tostring   一个   

双向链表中不同于单向链表,有两个指针域,一个负责向后连接,一个负责向前连接

相对于单向链表来说:查找更加方便,但添加删除相对复杂

package linkedlist;

public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero4);
        doubleLinkedList.list();

//        doubleLinkedList.add(hero1);
//        doubleLinkedList.add(hero2);
//        doubleLinkedList.add(hero3);
//        doubleLinkedList.add(hero4);
//        doubleLinkedList.list();
//
//        System.out.println("修改后");
//
//        HeroNode2 heroNode2 = new HeroNode2(4, "魔*林冲", "豹子头");
//        doubleLinkedList.update(heroNode2);
//        doubleLinkedList.list();
//
//        System.out.println("删除后");
//        doubleLinkedList.delete(4);
//        doubleLinkedList.list();
    }

}

class DoubleLinkedList{
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    //添加排序
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            } else if (temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;//后移
        }
        if (flag) {
            System.out.printf("待英雄编号 %d 添加已存在\n", heroNode.no);
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    //最后位置添加
    public void add(HeroNode2 heroNode2){
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode2;
        heroNode2.pre = temp;
    }

    public void update(HeroNode2 newHeroNode){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;//遍历完
            }
            if (temp.no == newHeroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else{
            System.out.printf("没有找到编号 %d 信息,无法修改", newHeroNode.no);
        }
    }

    public void delete(int no){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;//遍历完
            }
            if (temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            //最后一个即为赋空
            temp.pre.next = temp.next;
            if(temp.next != null){
                temp.next.pre = temp.pre;
            }
        }else{
            System.out.printf("没有找到编号 %d 信息,无法删除", no);
        }
    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

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) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode[" +
                "no=" + no +
                ", name=‘" + name + ‘\‘‘ +
                ", nickname=‘" + nickname + ‘\‘‘ +
                ‘]‘;
    }
}

双向链表

标签:删除   bool   pac   link   break   head   err   tostring   一个   

原文地址:https://www.cnblogs.com/bingbug/p/12079408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!