Java - 单链表

本文详细介绍了链表这一基础数据结构的特点与应用场景,包括其存储方式、节点组成及链表的基本操作如添加、删除等,并提供了Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链表是一种常见的基础数据结构,是一种有序的列表,但不会按照线性顺序存储数据,而是在每一个节点里存储下一个节点的指针(next)。链表适合插入、删除,不宜过长,否则会导致遍历性能下降。

  • 以节点方式存储;
  • 每个节点包含data域,next域:指向下一个节点;
  • 链表的各个节点不一定是连续存储的;

代码实现:

节点类

public class HeroNode {

    protected Integer no;
    protected String name;
    protected HeroNode next;

    public HeroNode(Integer no, String name) {
        this.no = no;
        this.name = name;
    }

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

 

SingleLinkedList

public class SingleLinkedList {

    private HeroNode root;
    private Integer size = 0;

    /**
     * 添加至链表头
     * @param hero
     */
    public void addFirst(HeroNode hero) {
        if (root == null) {
            root = hero;
        } else {
            hero.next = root;
            root = hero;
        }
        size++;
    }

    /**
     * 添加至链表尾
     * @param hero
     */
    public void addLast(HeroNode hero) {
        if (root == null) {
            root = hero;
        } else {
            HeroNode temp = root;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = hero;
        }
        size++;
    }

    /**
     * 按照某属性的顺序添加
     * @param hero
     */
    public void addByOrder(HeroNode hero) {
        if (root == null) {
            root = hero;

        } else {
            HeroNode tmp = root;
            // 新节点比头节点小
            if (hero.no < tmp.no) {
                root = hero;
                root.next = tmp;
                return;
            }
            // 找到next节点编号大于等于新节点的编号的节点,该节点与它的next节点之间就是新节点所在位置,
            // 等于时不添加,控制台进行提示
            while (tmp.next != null && tmp.next.no < hero.no) {
                tmp = tmp.next;
            }
            if (tmp.next == null) {
                tmp.next = hero;
                return;
            }

            if (tmp.next.no.equals(hero.no)) {
                System.out.println("编号为" + hero.no + "已存在");
                return;
            }
            hero.next = tmp.next;
            tmp.next = hero;
        }
        size++;
    }

    /**
     * 修改
     * @param hero
     */
    public void modify(HeroNode hero) {
        HeroNode tmp = root;
        while(tmp !=null) {
            if (tmp.no.equals(hero.no)) {
                tmp.name = hero.name;
                break;
            }
            tmp = tmp.next;
        }
    }

    /**
     * 根据编号获取节点
     * @param no
     * @return
     */
    public HeroNode query(int no) {
        HeroNode tmp = root;
        while (tmp != null) {
            if (tmp.no.equals(no)) {
                return tmp;
            }
            tmp = tmp.next;
        }
        return null;
    }

    /**
     * 根据编号删除节点
     * @param no
     */
    public void remove(int no) {
        if (root == null) {
            return;
        }
        //根节点
        if (no == root.no) {
            if (null != root.next) {
                root = root.next;
            } else {
                root = null;
            }
            size--;
            return;
        }

        // 非根节点
        HeroNode temp = root;
        while (temp.next != null) {
            if (no == temp.next.no) {
                break;
            }
            temp = temp.next;
        }
        // 节点不存在
        if (temp.next == null) {
            return;
        }
        temp.next = temp.next.next;
        size--;
    }

    /**
     * 打印
     */
    public void display() {
        if (root == null) {
            System.out.println("This SingleLinkedList is null.");
            return;
        }
        HeroNode temp = root;
        while(temp != null) {
            System.out.println(temp.toString());
            temp = temp.next;
        }
    }

    /**
     * 获取链表长度
     * @return
     */
    public Integer getSize() {
        return size;
    }
}
View Code

 

转载于:https://www.cnblogs.com/Latiny/p/10570143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值