Java实现线性表的顺序存储

 更新时间:2020年10月29日 13:34:11   作者:I like study.  
这篇文章主要为大家详细介绍了Java实现线性表的顺序存储,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现线性表的顺序存储,供大家参考,具体内容如下

顺序表:用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表

package algorithm.datastructure.seqlist;

/*顺序表
*
* 用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表
*
*/
public class SeqList {

  private int length;//顺序表长度
  private int[] list;//数组,连续的存储空间

  //初始化,构造一个空的线性表
  public SeqList(int listLength) {
    list = new int[listLength];
  }
  //销毁表
  public void destroyList() {
    list = null;
    this.length = 0;
  }
  //将线性表置为空表
  public void clearList() {
    for (int i = 0; i < getLength(); i++) {
      list[i] = 0;
    }
  }

  //判断线性表是否未空表
  public Boolean isEmpty() {
    return getLength() == 0;
  }

  //获取线性表元素个数
  public int getLength() {
    return length;
  }

  //根据下标获取线性表元素
  public int getElem(int i) {
    if (i < 0 || i >= getLength()) {
      try {
        throw new Exception("线性表下标越界");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return list[i];
  }


  //返回某元素(第一个)的前驱
  public Integer priorElem(int element) {
    for (int i = 0; i < getLength(); i++) {
      if (element == list[i]) {
        if (i == 0) {
          return null;
        } else {
          return list[i - 1];
        }
      }
    }
    return null;
  }

  //获取某元素(第一个)的后继
  public Integer nextElem(int element) {
    for (int i = 0; i < getLength(); i++) {
      if (element == list[i]) {
        if (i == getLength() - 1) {
          return null;
        } else {
          return list[i + 1];
        }
      }
    }
    return null;
  }

  //扩容,这里设置容量变为原来两倍
  public void ensureCapacity(int capacity) {
    if (capacity >= list.length) {//扩容
      int tempList[] = new int[list.length * 2];
      for (int i = 0; i < list.length; i++) {
        tempList[i] = list[i];
      }
      list = tempList;
    }
  }

  //在指定位置插入元素
  public Boolean insertElement(int index, int element) {
    if (index < 0 || index >= list.length) {
      try {
        throw new Exception("下标错误");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (index == getLength()) {
      return insertTailElement(element);
    }
    for (int i = 0; i < getLength(); i++) {
      if (i == index) {
        ensureCapacity(getLength() + 1);
        //index位置后面的元素后移
        for (int j = getLength() - 1; j >= index; j--) {
          list[j + 1] = list[j];
        }
        list[index] = element;
        length++;
      }
    }
    return true;
  }

  //尾部插入元素
  public Boolean insertTailElement(int element) {
    ensureCapacity(length + 1);
    list[++length] = element;
    return true;
  }
  //删除尾部元素
  public int deleteTailElement() {
    if (getLength() == 0) {
      try {
        throw new Exception("下标错误");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    int tailElement = list[getLength() - 1];
    list[getLength() - 1] = 0;
    length--;
    return tailElement;
  }

  //删除元素
  public int deleteElement(int index) {
    if (index < 0 || index >= list.length) {
      try {
        throw new Exception("下标错误");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (index == getLength()) {
      return deleteTailElement();
    }
    for (int i = 0; i < getLength(); i++) {
      if (i == index) {
        int tailElement = list[index];
        //index位置后面的元素前移
        for (int j = index; j < getLength() - 1; j++) {
          list[j] = list[j + 1];
        }
        list[getLength() - 1] = 0;
        length--;
        return tailElement;
      }
    }
    return 0;
  }

  //遍历顺序表
  public void traverseList() {
    for (int i = 0; i < getLength(); i++) {
      System.out.println(list[i]);
    }
  }



  public static void main(String[] args) {
    //测试
    SeqList seqList = new SeqList(2);
    System.out.println(seqList.insertTailElement(1));
    System.out.println(seqList.insertTailElement(2));
    System.out.println(seqList.insertTailElement(3));
    System.out.println(seqList.insertTailElement(4));
    System.out.println(seqList.getElem(0));
    System.out.println(seqList.getElem(1));
    System.out.println(seqList.getElem(2));
    System.out.println(seqList.getElem(3));

    System.out.println(seqList.insertElement(0, 4));
    System.out.println(seqList.getElem(0));
    System.out.println(seqList.getElem(1));
    System.out.println(seqList.getElem(2));
    System.out.println(seqList.getElem(3));
    System.out.println(seqList.getElem(4));
    System.out.println(seqList.priorElem(3));
    System.out.println(seqList.priorElem(4));
    System.out.println(seqList.nextElem(4));
    System.out.println(seqList.nextElem(3));
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
//    System.out.println(seqList.deleteTailElement());
    System.out.println(seqList.deleteElement(0));
    System.out.println(seqList.deleteElement(1));
    seqList.traverseList();
  }
}

以上就是用Java简单实现的顺序表,在Java中,如果要实现功能更复杂,性能更高的顺序表,可参考ArrayList源码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • springboot+mybaties项目中扫描不到@mapper注解的解决方法

    springboot+mybaties项目中扫描不到@mapper注解的解决方法

    本文主要介绍了springboot+mybaties项目中扫描不到@mapper注解的解决方法,该报错表明扫描不到Mapper层,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • spring security CSRF防护的示例代码

    spring security CSRF防护的示例代码

    这篇文章主要介绍了spring security CSRF防护的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • IDEA关闭git管理,文件变成红色解决方案

    IDEA关闭git管理,文件变成红色解决方案

    在软件开发中,当一个文件夹内的Java项目启用Git版本控制,通常会导致该文件夹下所有项目同步开启Git,这种做法有助于保持项目的一致性和可追溯性,但也可能带来管理上的复杂性,如果需要解除某个项目的Git管理,可以通过IDE的设置选项进行调整
    2024-10-10
  • springboot集成mybatisplus的详细步骤

    springboot集成mybatisplus的详细步骤

    MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生,这篇文章主要介绍了springboot四步集成mybatisplus,需要的朋友可以参考下
    2022-10-10
  • 剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串

    剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • 使用Java读取Excel文件数据的方法详解

    使用Java读取Excel文件数据的方法详解

    通过编程方式读取Excel数据能实现数据导入、批量处理、数据比对和更新等任务的自动化,本文为大家介绍了三种Java读取Excel文件数据的方法,需要的可以参考下
    2024-01-01
  • mybatis-plus 扩展批量新增的实现

    mybatis-plus 扩展批量新增的实现

    本文主要介绍了mybatis-plus 扩展批量新增的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Java创建对象的四种方式详解

    Java创建对象的四种方式详解

    这篇文章主要介绍了Java创建对象的四种方式详解,如果我们不想利用默认构造器来创建java对象,而想利用指定的构造器来创建java对象,则需要利用Construtor对象,每个Construtor对应一个构造器,需要的朋友可以参考下
    2023-11-11
  • SpringBoot Validation快速实现数据校验的示例代码

    SpringBoot Validation快速实现数据校验的示例代码

    在实际开发中,肯定会经常遇到对参数字段进行校验的场景,通常我们只能写大量的if else来完成校验工作,而如果使用SpringBoot Validation则可以轻松的通过注解来完成,接下来小编给大家介绍下利用SpringBoot Validation快速实现数据校验的示例代码,需要的朋友参考下吧
    2022-06-06
  • IntelliJ IDEA 2020.3 重大特性(新功能一览)

    IntelliJ IDEA 2020.3 重大特性(新功能一览)

    这篇文章主要介绍了IntelliJ IDEA 2020.3 重大特性(新功能一览),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12

最新评论