【数据结构和算法分析】单链表的基本实现

本文介绍单链表的基本实现方法,包括插入、删除节点等操作,并分析了单链表的时间效率问题。通过Java代码示例展示了如何构建和操作单链表。

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

单链表的基本实现

链表中的数据是以节点来表示的,每个节点的构成:元素 + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个节点的地址数据。 


效率问题:单链表在效率上最大的问题在于,如果要插入一个结点到链表的末端或者删除末端的一个结点,则需要遍历整个链表,时间复杂度是O(N)。平均来说,要访问一个结点,时间复杂度也有O(N/2)。这是链表本身的性质所造成的,没办法解决。不过我们可以采用双链表和循环链表来改善这种情况。


其他说明:java 的 util 中的链表实现有两种方式,ArrayList由数组实现,LinkdeList由结点实现,两者各有千秋,在此,MyList是由结点实现的链表


public class MyList {
    
    /*
     * 单向链表的基本实现
     * 包括插入结点,删除结点,查询某一位置结点的值,修改结点的值,反回结点的位置等基本功能
     */
    Node headNode;   //头结点
    Node tailNode;   //尾结点
    int length;    //链表长度   

    /*
     * 初始化链表,由头结点创建
     * 仅有单一  Node 创建,不考虑在 Node 中 new Node
     * @param  Node node 头结点
     */
    public MyList(Node node){    
        this.headNode = node;
        this.tailNode = node;
        length++;
    } 
    /*
     * 在链表结尾加一个结点
     * @param Node node 加在结尾的结点
     */
    public void insert(Node node){
        insert(node, length);
    }
    /*
     * 在链表中加一个结点
     * @param Node node,加在链表中的结点   , int index 结点所加 的位置
     */
    public void insert(Node node,int index){
        if (index < 0 || index > length) {
            throw new IndexOutOfBoundsException();
        }
        if (index == 0) {
            node.next = this.headNode;
            headNode = node;
        }else if (index == length) {
            this.tailNode.next = node;
            tailNode = node;
        }else {
            Node otherNode = headNode;    
            while (index > 1) {
                otherNode = otherNode.next;
                index--;
            }
            node.next = otherNode.next;
            otherNode.next = node;
        }
        length++;
    }
    /*
     * 删除链表中的一个结点
     * @param int index 所需删除的结点的位置
     */
    public void delete(int index){
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        }
        if (index == 0 && length != 1) {
            this.headNode = headNode.next;
            length--;
        }else if(index == 0 && length == 1){
            this.headNode = this.tailNode = null;
            length = 0;
        }else {
            Node otherNode = this.headNode;
            while (index > 1) {
                otherNode = otherNode.next;
                index--;
            }
            otherNode.next = otherNode.next.next;
            length--;
        }
    }
    /*
     * 查看某一处结点的值
     * @param int index 所需查看的结点的位置
     */
    public Object indexOf(int index){
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        }
        Node otherNode = this.headNode;
        while (index > 0) {
            otherNode = otherNode.next;
            index--;
        }
        return otherNode.data;
    }
    /*
     * 设置某一处结点的值
     * @param int index 结点的位置 ,Object data 修改后的结点的值
     */
    
    public void setData(int index,Object data){
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        }
        Node otherNode = this.headNode;
        while (index > 0) {
            otherNode = otherNode.next;
            index--;
        }
        otherNode.data = data;
    }
    /*
     * 依次打印所以结点
     */
    public void print(){
        Node otherNode = this.headNode;
        while(otherNode.hasnext()){
            System.out.print(otherNode.data+" ");
            otherNode = otherNode.next;
        }
        System.out.println(otherNode.data);
    }
}


class Node{
    
    /*
     * 节点类,包括结点的值 data 和指向下一结点的指针 next
     */
    
    Object data;
    Node next;
    
    public Node(){
        data = null;
        next = null;
    }
    
    public Node(Object data){
        this.data = data;
        this.next = null;
    }
    
    public Node(Object data,Node nextnode){
        this.data = data;
        this.next = nextnode;
    }
    
    /*
     * 判断有无下一结点
     * @return boolean 若true,则该结点有下一结点
     */
    public boolean hasnext(){
        if (this.next != null) {
            return true;
        }else {
            return false;
        }
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值