链表的增删改查、链表与顺序表的优缺点

本文介绍了链表的概念,包括无头单向链表的增删改查操作,并详细对比了链表与顺序表的优缺点。链表允许在任意位置高效地进行插入和删除,但不支持随机访问;而顺序表则提供空间连续性,适合随机访问,但在中间或前面进行插入删除效率较低。

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

链表

链表的概念

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。
链表的结构有8种。
其中分为单向/双向、带头/不带头、循环/非循环八种。

无头单向链表的增删改查

创建了一个类,用来对链表的增删改查。

//无头单向链表插入
class Node{//创建节点
    public int value;//节点中的数据
    public Node next;//节点下一个数据的地址
    public Node(int value){
        this.value = value;
    }
}
public class SingleLinkedList {
    public Node head = null;
    //头插
    public void addFirst(int data){
        Node node = new Node(data);
        node.next = head;
        this.head = node;
    }
    //尾插
    public void addLast(int data){
        Node cur = this.head;
        Node node = new Node(data);
        if(this.head == null){
            this.head = node;
        }else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    public Node searchIndexSbuOne(int index) {
        Node cur = this.head;
        int count = 0;
        while (count != index - 1) {
            count++;
            cur = cur.next;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标,空链表添加数据
    public void addIndex(int index,int data){
        Node cur = this.head;
        Node node = new Node(data);
        if(index < 0 || index > this.size()){
            System.out.println("数据插入的位置不合法!");
            return;
        }
        if(index == 0){
           addFirst(data);
           return;
        }
        if(index == size()){
            addLast(data);
            return;
        }


        Node ret = searchIndexSbuOne(index);
        node.next = ret.next;
        ret.next = node;

    }
    //查找是否包含关键字key是否在单链表中
    public boolean contains(int key){
        Node cur = this.head;
        Node node = new Node(key);
        while(cur != null){
            if(cur.value == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public Node searchPrev(int key){
        Node cur = this.head;
        while(cur.next != null){
            if(cur.next.value == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head.value == key){
            this.head = this.head.next;
            return;
        }
        Node prev = searchPrev(key);
        if(prev == null){
            System.out.println("没有要删除的值!");
            return;
        }
        Node del = prev.next;
        prev.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = this.head.next;
        Node prev = this.head;
        while(cur != null){
            if(cur.value == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur =cur.next;
            }
        }
        if(prev.value == key){
            prev = prev.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int length = 0;
        Node cur = this.head;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        return length;
    }
    //打印链表数据
    public void display(){
        Node cur = this.head;
        if(cur == null){
            System.out.println();
        }
        while(cur != null){
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //清空链表
    public void clear(){
        Node cur = this.head;
        while(cur != null){
            Node curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        System.out.println("链表被清空");
    }
}

顺序表和链表的区别和联系

顺序表

优点:空间连续、支持随机访问
缺点:(1)中间或前面部分的插入删除时间复杂度O(N);(2)增容的代价比较大。

链表

优点:(1)任意位置插入删除时间复杂度为O(1);(2)不会浪费空间,用一个开辟一个空间。
缺点:以节点为存储单位,不支持随机访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值