单链表的实现

之前的实现的顺序表有如下缺点

  1. 顺序表中间/头部的插入删除,时间复杂度为O(N)
  2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
  3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。

那么 如何解决以上问题呢?我们就需要用链表来解决了,我们就来聊一下链表。
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。概念肯能听起来有点绕,我们来举一个例子吧,链表类似于我们生活中的糖葫芦,它的一个个节点类似于一串糖葫芦上的一个个山楂,引用链接类似于糖葫芦把所有山楂连接起来的木签。可以对照下图:
在这里插入图片描述
知晓了链表的原理后,我们来想想怎么实现呢。我们可以定义一个Node类(大部分都是这样定义的),它包括两个成员变量data域(保存当前节点的数据)和next域(保存下一个引用的地址,类似于指针)

class Node{
    public int value;
    public Node next;
    public Node(){};
    public Node(int value){
        this.value=value;
    }
}

注意:链表的第一个节点和最后一个节点,分别称为链表的头节点和尾节点。尾节点的特征是其 next 引用为空(null)。链表中每个节点的 next 引用都相当于一个指针,指向另一个节点,借助这些 next 引用,我们可以从链表的头节点移动到尾节点。这个next我们一定要理解,不然凉凉。

链表数据结构中主要包含单向链表、双向链表及循环链表。今天我们先来简单看看单向链表。

单向链表

单向链表只有一个指针域,在整个节点中数据域用来存储数据元素,指针域用于指向下一个具有相同结构的节点。
在这里插入图片描述

  • 单向链表中,每个节点的数据域都是通过一个 Object 类的对象引用来指向数据元素的,与数组类似,单向链表中的节点也具有一个线性次序,即如果节点 a1 的 next 引用指向节点 a2,则 a1 就是 a2 的直接前驱,a2 是 a1 的直接后续。只能通过前驱节点找到后续节点,而无法从后续节点找到前驱节点。

特点:
  数据元素的存储对应的是不连续的存储空间,每个存储结点对应一个需要存储的数据元素。每个结点是由数据域和指针域组成。 元素之间的逻辑关系通过存储节点之间的链接关系反映出来。
  逻辑上相邻的节点物理上不必相邻。

单链表和顺序表的比较

可以简单理解单链表的优点就是顺序表的缺点。

  • 缺点
    1、比顺序存储结构的存储密度小 (每个节点都由数据域和指针域组成,所以相同空间内假设全存满的话顺序比链式存储更多)。
    2、查找结点时链式存储要比顺序存储慢(每个节点地址不连续、无规律,导致按照索引查询效率低下)。
  • 优点:
    1、插入、删除灵活 (不必移动节点,只要改变节点中的指针,但是需要先定位到元素上)。
    2、有元素才会分配结点空间,不会有闲置的结点。

源码如下:
实现了对单链表的增删查改以及简单的测试。

class Node{
    public int value;
    public Node next;
    public Node(){};
    public Node(int value){
        this.value=value;
    }
}


 class MyLinkedList {
    public Node head;
    public void  frontAdd(int value){
        Node node=new Node(value);
        if(this.head==null){
            this.head=node;
        }else{
            node.next=this.head;
            this.head=node;
        }

    }
    public void backAdd(int value)
    {
        Node node=new Node(value);
        if(this.head==null)
        {
            this.head=node;
        }else{
            Node cur=this.head;
            while(cur.next!=null)
            {
                cur=cur.next;
            }
            cur.next=node;
        }
    }
    public void dispaly(){
        Node cur=new Node();
        cur=this.head;
        while(cur!=null)
        {
            System.out.print(cur.value+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public int getSize(){
        int count=0;
        Node cur=this.head;
        while(cur!=null)
        {
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void insert(int pos,int value){
        if(pos<0||pos>getSize())
        {
            System.out.println("插入位置不合法");
            return ;
        }
        if(pos==0)
        {
            frontAdd(value);
            return ;
        }
        if(pos==getSize())
        {
            backAdd(value);
            return ;
        }
        Node node=new Node(value);
        Node cur=this.head;
        int index=pos-1;
        while(index!=0)
        {
            cur=cur.next;
            index--;
        }
        node.next=cur.next;
        cur.next=node;

    }
     public Node findLastTwoNode() {
    if(this.head==null||this.head.next==null)
    {
        return null;
    }
    Node cur=this.head;
    while(cur.next.next!=null)
    {
        cur=cur.next;
    }
         return cur;

    }

     public int getPosvalue(int pos){
        if(this.head==null)
        {
            return -1;
        }
        if(pos<0||pos>getSize()-1)
        {
            System.out.println("输入pos位置不合法!");
            return -1;
        }
        int count=0;
        Node cur=this.head;
        while(count!=pos)
         {
             cur=cur.next;
             count++;
         }
        return cur.value;
     }
     //链表中是否包含某个元素
     public boolean contain(int value){
        Node cur=this.head;
        while(cur!=null)
        {
            if(cur.value==value)
            {
                System.out.println("包含元素"+value);
                return true;
            }
            cur=cur.next;
        }
         System.out.println("不包含这个元素");
        return false;

     }

     //删除第一次出现关键字为key的节点
      public void remove(int key){
        if(this.head==null)
        {
            return ;
        }
        if(this.head.value==key)
        {
            this.head=this.head.next;
        }
        Node cur=this.head;
        while(cur!=null)
        {
            if(cur.next.value==key)
            {
                break;
            }
            cur=cur.next;
        }
        Node del=cur.next;
        cur.next=del.next;

      }
     // 删除所有值为key的节点
     public void removeAllKey(int key){
         if(head==null)
         {
             return ;
         }
         Node prev=head;
         Node cur=head.next;
         while(cur!=null)
         {
             if(cur.value==key)
             {
                 prev.next=cur.next;
                 cur=cur.next;
             }else{
                 prev=cur;
                 cur=cur.next;
             }
         }
         if(head.value==key)
         {
             head=head.next;
         }
         return ;

     }

     public static void main(String[] args) {
         MyLinkedList myLinkedList=new MyLinkedList();
         myLinkedList.frontAdd(0);
         myLinkedList.frontAdd(1);
         myLinkedList.backAdd(2);
         myLinkedList.backAdd(3);
         myLinkedList.backAdd(4);
         myLinkedList.backAdd(4);
         myLinkedList.backAdd(4);
         myLinkedList.insert(0,111);
         myLinkedList.insert(8,666);
         myLinkedList.dispaly();
         Node ret1=myLinkedList.findLastTwoNode();
         System.out.println(ret1.value);
         System.out.println(myLinkedList.getPosvalue(5));
         System.out.println(myLinkedList.getSize());
         myLinkedList.contain(11111);
         myLinkedList.contain(111);
         myLinkedList.remove(1);
         myLinkedList.removeAllKey(4);
         myLinkedList.dispaly();
     }
}

在这里插入图片描述
链表作为数据结构的基础必须要学好,加油吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值