• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java实现线性表的顺序存储

java 搞代码 4年前 (2022-01-09) 36次浏览 已收录 0个评论

本文实例为大家分享了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 in<p style="color:transparent">本文来源gao!daima.com搞$代!码网</p>t 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();
  }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java实现线性表的顺序存储
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址