欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现线性表的顺序存储

程序员文章站 2022-07-02 13:57:45
本文实例为大家分享了java实现线性表的顺序存储,供大家参考,具体内容如下顺序表:用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表packag...

本文实例为大家分享了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源码。

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