实现链表的基本操作

实现链表的基本操作

  • 因为单链表和双链表很相似,所以我使用了MVC设计模式简化了思路,并且使用Java语言编译
  • 首先在dao层抽取出节点,用于存放信息
  • 然后在service层分别实现单链表和双链表的具体操作】
  • 最后在客户层调用service的方法
package com.guodaxia.dao;

/**定义王者荣耀榜,每个HeroKing对象就是一个节点
 * @ author Guo daXia
 * @ create 2022/11/15
 */
public class HeroNode {
        public int no;
        public String name;
        public HeroNode next;//指向下一个节点
        public HeroNode pre;//指向前一个节点
        //构造器
        public HeroNode() {}
        public HeroNode(int no, String name) {
            this.no = no;
            this.name = name;
        }
        @Override
        public String toString() {
            return "HeroKing{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
package com.guodaxia.service;

import com.guodaxia.dao.HeroNode;

/**
 * @ author Guo daXia
 * @ create 2022/11/15
 */
public class DoubleLinkedList {
    HeroNode head = new HeroNode(0,"");

    /**
     * 添加节点到双链表最后端
     * @param heroNode 传入一个新英雄信息
     */
    public void add(HeroNode heroNode){
        //因为头节点不能动,所以只能通过一个临时变量也是指向头节点堆中,进行遍历添加
        HeroNode temp  =head;
        //遍历链表,找到最后一个节点
        while (true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        //while循环结束后说明找到了,这是可操作链上 新节点
        temp.next = heroNode;
    }

    /**
     * 显示双链表
     */
    public void list(){
        //先判断链表 是不是为空
        if (head.next==null){
            System.out.println("链表为空!");
            return;
        }
        //因为头节点不能动,所以只能通过一个临时变量也是指向头节点堆中,进行遍历显示
        HeroNode temp  =head.next;
        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * 修改双链表的英雄信息
     * @param newHeroNode 传入一个新英雄名字,根据新英雄的编号替换已有的英雄,不修改编号
     */
    public void update(HeroNode newHeroNode){
        if (head.next==null){
            System.out.println("链表为空!");
            return;
        }
        //定义一个临时变量
        HeroNode temp = head.next;
        boolean flag=false;//flag标志是否找到编号,默认没有找到
        while (true){
            if (temp==null){
                break;//说明已经遍历完了
            }
            if (temp.no==newHeroNode.no){
                flag = true;//说明找到了指定no
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name= newHeroNode.name;
        }else {//咩有找到编号的节点
            System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
        }
    }

    /**
     * 删除双链表的一个英雄信息
     * @param no 传入一个双链表已有的编号
     */
    public void del(int no){

        if (head.next==null){
            System.out.println("链表为空,无法删除!");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;//flag标志是否要删除,默认不要
        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节点不存在!\n",no);
        }
    }
}
package com.guodaxia.service;

import com.guodaxia.dao.HeroNode;

/**
 * @ author Guo daXia
 * @ create 2022/11/15
 */
public class SingleLinkedList {
    HeroNode head = new HeroNode(0,"");

    /**
     * 添加节点到单链表最后端
     * @param heroNode 传入一个新英雄信息
     */
    public void addByOrder(HeroNode heroNode){

        //因为头节点不能动,所以只能通过一个临时变量也是指向头节点堆中,进行遍历添加
        HeroNode temp  =head;
        boolean flag = false; //flag标记添加的编号是否存在,默认不存在
        //遍历链表,找到最后一个节点
        while (true){
            if (temp.next==null){ //说明temp已经在链表的最后了
                break;
            }
            if (temp.next.no> heroNode.no){ //说明位置已经找到,就在temp的后面插入
                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 list(){
        //先判断链表 是不是为空
        if (head.next==null){
            System.out.println("链表为空!");
            return;
        }
        //因为头节点不能动,所以只能通过一个临时变量也是指向头节点堆中,进行遍历显示
        HeroNode temp  =head;
        while (true){
            if (temp==null){
                break;
            }
            if (temp.next!=null)
                System.out.println(temp.next);
            temp = temp.next;
        }
    }

    /**
     * 更新单链表
     * @param newHeroNode 传入一个链表中已有的编号,根据编号修改名字
     */
    public void update(HeroNode newHeroNode){
        if (head.next==null){
            System.out.println("链表为空!");
            return;
        }
        //定义一个临时变量
        HeroNode temp = head.next;
        boolean flag=false;//flag标志是否找到编号,默认没有找到
        while (true){
            if (temp==null){
                break;//说明已经遍历完了
            }
            if (temp.no==newHeroNode.no){
                flag = true;//说明找到了指定no
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name= newHeroNode.name;
        }else {//咩有找到编号的节点
            System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
        }
    }

    /**
     * 删除单链表一个英雄节点
     * @param no 传入一个编号值
     */
    public void del(int no){
        HeroNode temp = head;
        boolean flag = false;//flag标志是否要删除,默认不要
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.no==no){//找到了指定的编号
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            //可用删除
            temp.next=temp.next.next;
        }else {
            System.out.printf("要删除的%d节点不存在!\n",no);
        }
    }
}
package com.guodaxia.test;

import com.guodaxia.dao.HeroNode;
import com.guodaxia.service.DoubleLinkedList;
import com.guodaxia.service.SingleLinkedList;

/**
 * @ author Guo daXia
 * @ create 2022/11/15
 */
public class SingleTest {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "小乔");
        HeroNode hero2 = new HeroNode(2, "大桥");
        HeroNode hero4 = new HeroNode(4, "吕布");
        HeroNode hero3 = new HeroNode(3, "诸葛亮");

        SingleLinkedList list =new SingleLinkedList();
        list.addByOrder(hero1);
        list.addByOrder(hero2);
        list.addByOrder(hero3);
        list.addByOrder(hero4);
        list.list();
        //测试修改节点信息的代码
        HeroNode newHero = new HeroNode(2, "周瑜");
        list.update(newHero);
        System.out.println("修改后的节点信息:");
        list.list();
        //测试删除节点的代码
        System.out.println("删除后信息");
        list.del(1);
        list.del(2);
        list.del(3);
        list.del(4);
        list.list();
        System.out.println("***********以上为单链表测试******************");
        DoubleLinkedList list1 = new DoubleLinkedList();
        list1.add(hero1);
        list1.add(hero4);
        list1.add(hero3);
        list1.add(hero2);
        list1.list();

        list1.update(new HeroNode(2,"周瑜"));
        list1.list();

        list1.del(1);
        list1.list();
    }
}

在双向链表中也可以通过编号顺序进行添加展示:

    /**
     * 按顺序添加新英雄信息
     * @param heroNode 传入一个新英雄信息
     */
    public void addByOrder(HeroNode heroNode){
        //因为头节点不能动,所以只能通过一个临时变量也是指向头节点堆中,进行遍历添加
        HeroNode temp  =head;
        boolean flag = false; //flag标记添加的编号是否存在,默认不存在
        //遍历链表,找到最后一个节点
        while (true){
            if (temp.next==null){ //说明temp已经在链表的最后了
                break;
            }
            if (temp.next.no> heroNode.no){ //说明位置已经找到,就在temp的后面插入
                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;//将newHeroNode的的next置空
            temp.next= heroNode;
            heroNode.pre=temp;
        }
    }

posted @ 2022-11-15 20:05  郭培鑫同学  阅读(37)  评论(0编辑  收藏  举报