软件编程
位置:首页>> 软件编程>> java编程>> Java 精炼解读数据结构的链表的概念与实现

Java 精炼解读数据结构的链表的概念与实现

作者:K媾?  发布时间:2022-03-02 05:17:11 

标签:Java,数据结构,链表

前言:

顺序表的问题及思考

1. 顺序表中间/头部的插入删除,时间复杂度为O(N)

2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。

3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续 插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。

思考: 如何解决以上问题呢?下面给出了链表的结构来看看。

一、什么是链表

链表的概念

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

链表的结构

链表结构分为8种:

Java 精炼解读数据结构的链表的概念与实现

这里我们只讲最下面两种,因为在工作中、业务里、考试题、刷到的链表题、面试题里面都是用到这两种链表。 

链表如何存储数据

链表是由一个一个节点组成的。(这里我们以单链表为例)

什么叫做节点?

节点分为两个域 ,假设一个叫做val域,一个叫做next域。

val:数据域

next:下一个节点的地址

Java 精炼解读数据结构的链表的概念与实现

链表的实现  


//ListNode代表一个节点

class ListNode{
   public int val;
   public ListNode next;

//构造方法
   public ListNode(int val){
       this.val = val;
   }
}

//MyLinkedList 代表这是一个链表

public class MyLinkedList {
   public ListNode head;//链表的头引用,所以定义在链表里,head是链表的头,不是节点的头,节点只有两个属性,一个属性是val值,一个属性是next值,所以不能定义在ListNode类里面
   ListNode listNode = new ListNode(2);//节点实例化,val域赋值为2
}

穷举法创建链表


/MyLinkedList 代表这是一个链表
public class MyLinkedList {
   public ListNode head;//链表的头引用,所以定义在链表里
   public  void createList(){
       ListNode listNode0 = new ListNode(11);
       ListNode listNode1 = new ListNode(26);
       ListNode listNode2 = new ListNode(23);
       ListNode listNode3 = new ListNode(45);
       ListNode listNode4 = new ListNode(56);
       listNode0.next = listNode1;
       listNode1.next = listNode2;
       listNode2.next = listNode3;
       listNode3.next = listNode4;
       this.head = listNode0;
   }

打印链表


//打印链表
   public  void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
       System.out.println();
   }

打印结果:

Java 精炼解读数据结构的链表的概念与实现

查找是否包含关键字key是否在单链表当中 


//查找是否包含关键字key是否在单链表当中
   public boolean contains(int key) {
       ListNode cur = this.head;
       while (cur != null) {
           if (cur.val == key) {
               return true;
           }
           cur = cur.next;
       }
       return false;
   }

打印结果:

Java 精炼解读数据结构的链表的概念与实现

得到单链表的长度:


   //得到单链表的长度
   public int size(){
       ListNode cur = this.head;
       int count = 0;
       while(cur != null){
           count++;
           cur = cur.next;
       }
       return count;
   }

 打印结果:

Java 精炼解读数据结构的链表的概念与实现

头插法


//头插法

public void addFirst(int data) {
       ListNode node = new ListNode(data);
       if (this.head == null) {
           this.head = node;
       } else {
           node.next = this.head;
           head = node;
       }
   }

打印结果:

Java 精炼解读数据结构的链表的概念与实现

尾插法


//尾插法

public void addLast(int data){
       ListNode node = new ListNode(data);
       ListNode cur = this.head;
       if(this.head == null){
           this.head = node;
       }else {
           while(cur.next != null){
               cur = cur.next;
           }
           cur.next = node;

}
   }

打印结果:

Java 精炼解读数据结构的链表的概念与实现

任意位置插入,第一个数据节点为0号下标


public ListNode findIndex(int index){
       ListNode cur = this.head;
       while(index -1 != 0){
           cur = cur.next;
           index--;
       }
       return cur;

}
   //任意位置插入,第一个数据节点为0号下标
   public void addIndex(int index,int data){
       if(index < 0 || index > size()){
           System.out.println("位置不合法");
           return;
       }
           if(index == 0){
               addFirst(data);
               return;
           }
           if(index == size()){
               addLast(data);
               return;
           }

ListNode cur = findIndex(index);
           ListNode node = new ListNode(data);
           node.next = cur.next;
           cur.next = node;

}

打印结果:

Java 精炼解读数据结构的链表的概念与实现

删除第一次出现关键字为key的节点


//删除第一次出现关键字为key的节点
   public void remove(int key){
       if(this.head == null){
           System.out.println("没有你要删除的节");
           return;
       }
      if (this.head.val == key){
          this.head = this.head.next;
          return;
       }
      ListNode cur = this.head;
      while (cur.next != null){
          if(cur.next.val == key){
              cur.next = cur.next.next;
              return;
          }
          cur = cur.next;
      }
      if(cur.next == null){
          System.out.println("没有该节点");
          return;
      }

}

打印结果:

Java 精炼解读数据结构的链表的概念与实现

删除所有值为key的节点


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

}

打印结果:

Java 精炼解读数据结构的链表的概念与实现

总结:

本文简单介绍了数据结构的链表,如何创建链表,链表上如何操作数据。通过简单例题的方式加深对顺序表的理解。上述就是今天的内容,有任何疑问的话可以随时私信我,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!

来源:https://blog.csdn.net/m0_64397675/article/details/123401757

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com