Java - 顺序表和链表

1. 线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
在这里插入图片描述

2. 顺序表

2.1 概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
静态顺序表:使用定长数组存储。
动态顺序表:使用动态开辟的数组存储。
静态顺序表适用于确定知道需要存多少数据的场景.
静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用.
相比之下动态顺序表更灵活, 根据需要动态的分配空间大小.

2.2 接口实现

我们来实现一个动态顺序表. 以下是需要支持的接口.

public class SeqList {
    
    
    // 打印顺序表
    public void display() {
    
      }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
    
     }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
    
     return true; }
    // 查找某个元素对应的位置
    public int search(int toFind) {
    
     return -1; }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
    
     return -1; }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
    
      }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
    
      }
    // 获取顺序表长度
    public int size() {
    
     return 0; }
    // 清空顺序表
    public void clear() {
    
      }
}

顺序表实现的时候:
this.elem[usedSize] = null; 如果数组当中是引用数据类型,需要置为null。
例如:

//删除第一次出现的关键字key
public void remove(int toRemove) {
    
    
    int pos = search(toRemove);
    for (int i = pos; i < this.elem.length- 1; i++) {
    
    
        this.elem[i] = this.elem[i+1];
    }
    this.usedSize--;
    //this.elem[usedSize] = null; 如果数组当中是引用数据类型。
}
// 清空顺序表
public void clear() {
    
    
    this.usedSize = 0;
    /*for (int i = 0; i < usedSize; i++) {
        this.elem[i] = null;
    }
    */

}

2.3 顺序表的实现

MyArrayList.java文件

import java.util.Arrays;

public class MyArrayList {
    
    

    public int[] elem;
    public int usedSize;//有效的数据个数

    public MyArrayList() {
    
    
        this.elem = new int[10];
    }

    // 打印顺序表
    public void display() {
    
    
        for (int i = 0; i < this.usedSize; i++) {
    
    
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    // 获取顺序表的有效数据长度
    public int size() {
    
    
        return this.usedSize;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
    
    
        //第一步:判断pos是否在合理区间
        if (pos < 0 || pos > usedSize) {
    
    
            System.out.println("pos不在合理区间内");
            return;
        }
        //第二步:判断顺序表初始长度是否已经满了
        if(isFull()) {
    
    
            System.out.println("顺序表初始长度已满,已经扩容为原长度的二倍");
            this.elem = Arrays.copyOf(this.elem,elem.length*2);
        }
        //第三步:新增元素
        this.elem[pos] = data;
        //第四步:已使用的长度加一
        this.usedSize++;
    }

    public boolean isFull() {
    
    
        return this.elem.length == this.usedSize;
    }


    // 判定是否包含某个元素
    public boolean contains(int toFind) {
    
    
        for (int i = 0; i < this.elem.length; i++) {
    
    
            if (this.elem[i] == toFind) {
    
    
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int search(int toFind) {
    
    
        for (int i = 0; i < this.elem.length; i++) {
    
    
            if (this.elem[i] == toFind) {
    
    
                return i;
            }
        }
        return -1;
    }

    public boolean isEmpty() {
    
    
        return this.usedSize == 0;
    }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
    
    
        //第一步:pos的位置不能越界
        if (pos < 0 || pos >= usedSize) {
    
    
            System.out.println("pos位置越界了");
            return -1;
        }
        //第二步:原顺序表不能为空
        if(isEmpty()) {
    
    
            System.out.println("顺序表为空!");
            return -1;
        }

        //第三步:获取pos位置的元素
        return this.elem[pos];
    }


    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
    
    
        //第一步:pos的位置不能越界
        if (pos < 0 || pos >= usedSize) {
    
    
            System.out.println("pos位置越界了");
            return;
        }
        //第二步:判断顺序表是否为空
        if (isEmpty()) {
    
    
            System.out.println("顺序表为空");
            return;
        }
        //pos位置设置元素为value
        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
    
    
        int pos = search(toRemove);
        for (int i = pos; i < this.elem.length- 1; i++) {
    
    
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
        //this.elem[usedSize] = null; 如果数组当中是引用数据类型。
    }
    // 清空顺序表
    public void clear() {
    
    
        this.usedSize = 0;
        /*for (int i = 0; i < usedSize; i++) {
            this.elem[i] = null;
        }
        */

    }
}

Test.java文件

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myList = new MyArrayList();
//        System.out.println(myList.isEmpty());
        myList.setPos(4,10);
        myList.display();
//        System.out.println(myList.getPos(0));
        myList.add(0,2);
//        System.out.println(myList.getPos(0));
        myList.add(1,8);
        myList.add(2,6);
        myList.add(3,9);
        myList.add(4,4);
//        System.out.println(myList.isEmpty());
        myList.display();
        System.out.println(myList.size());
        System.out.println(myList.contains(1));
        System.out.println(myList.search(9));
        System.out.println(myList.getPos(0));
        myList.setPos(4,10);
        myList.display();
        myList.remove(6);
        myList.display();
        myList.clear();
        myList.display();

    }
}

3. 链表

3.1 链表的概念及结构

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
在这里插入图片描述
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

  • 单向、双向
  • 带头、不带头
  • 循环、非循环(循环时,尾节点指向第一个数据节点(带头时,不是指向傀儡节点))
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    虽然有这么多的链表的结构,但是我们重点掌握两种:
  • 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
  • 无头双向非循环链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。
    在这里插入图片描述

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

顺序表
优点:空间连续、支持随机访问
缺点:1.中间或前面部分的插入删除时间复杂度O(N) 2.增容的代价比较大。
链表
优点:1.任意位置插入删除时间复杂度为O(1) 2.没有增容问题,插入一个开辟一个空间。
缺点:以节点为单位存储,不支持随机访问

3.2 链表的实现

// 1、无头单向非循环链表实现
public class SingleLinkedList {
    
    
  //头插法
  public void addFirst(int data);
  //尾插法
  public void addLast(int data);
  //任意位置插入,第一个数据节点为0号下标
  public boolean addIndex(int index,int data);
  //查找是否包含关键字key是否在单链表当中
  public boolean contains(int key);
  //删除第一次出现关键字为key的节点
  public void remove(int key);
  //删除所有值为key的节点
  public void removeAllKey(int key);
  //得到单链表的长度
  public int size();
  public void display();
  public void clear();
}
// 2、无头双向链表实现
public class DoubleLinkedList {
    
    
  //头插法
  public void addFirst(int data);
  //尾插法
  public void addLast(int data);
  //任意位置插入,第一个数据节点为0号下标
  public boolean addIndex(int index,int data);
  //查找是否包含关键字key是否在单链表当中
  public boolean contains(int key);
  //删除第一次出现关键字为key的节点
  public void remove(int key);
  //删除所有值为key的节点
  public void removeAllKey(int key);
  //得到单链表的长度
  public int size();
  public void display();
  public void clear();
}

猜你喜欢

转载自blog.csdn.net/qq_43398758/article/details/121065211
今日推荐