• 1.线性顺序表


    线性结构概念:

    1.除了第一个和最后一个元素,每个元素都有一个前驱和一个后继元素

    2.第一个元素没有前驱

    3.最后一个元素没有后继

    操作:

    1.元素个数

    2.插入

    3.删除

    4.查找

    5.判断是否为空

    /**
    * 线性表接口
    */
    public interface List<E> {

    public int getSize();

    public boolean isEmpty();

    //插入元素
    public void add(E e);

    //对于位置添加元素
    public void add(int index,E e);

    public void delete(int index);

    public E get(int index);

    }

    计算机存储结构:顺序存储和离散存储

    顺序结构的线性表是顺序表

    顺序表实现类:

    public class SequenceList<E> implements List<E> {

    private final int DEFAULT_SIZE = 10;

    int maxSize;

    int currentSize;//當前長度

    private E[] emelents;//元素

    public SequenceList() {
    init(DEFAULT_SIZE);
    }

    public SequenceList(int size) {
    init(size);
    }

    private void init(int size) {
    this.maxSize = size;
    currentSize = 0;
    emelents = (E[])new Object[size];
    }


    @Override
    public int getSize() {
    return currentSize;
    }

    @Override
    public boolean isEmpty() {
    return currentSize == 0;
    }


    @Override
    public void add(int index, E e) {
    if(index<0||index>currentSize){
    throw new RuntimeException("參數錯誤");
    }
    for (int i = currentSize; i > index; i--) {//移動後面的元素
    emelents[i] = emelents[i-1];
    }
    emelents[index] = e;
    currentSize++;
    }

    @Override
    public void delete(int index) {
    if(isEmpty()){
    throw new RuntimeException("無法刪除");
    }else {
    if(index<0||index>currentSize-1){
    throw new RuntimeException("非法");
    }
    for (int i = index; i < currentSize ; i++) {
    emelents[i] = emelents[i+1];
    }
    currentSize--;
    }
    }

    @Override
    public E get(int index) {
    if(index<0||index>currentSize){
    throw new RuntimeException("");
    }
    return emelents[index];
    }
    }

    分析:插入和删除需要移动大量的元素,O(n)
    优点:支出随机访问,底层数组,内存连续,空间利用率高
    确定:大小固定,插入删除需要移动大量的数据


  • 相关阅读:
    linux将home目录扩容到根目录
    Daily Build
    H公司数据同步的总结
    VB2010新特性之——标识语言版本的新命令行选项/langversion (Visual Basic)
    Linux安装Jemalloc
    Lnmp切换PHP版本
    Server2008通过bat命令自动定时备份MySQL数据库
    IIS 安装AspNetCoreModule托管模块
    JavaScript 学习笔记——Math属性及其方法
    js完美多物体运动框架(缓冲运动)
  • 原文地址:https://www.cnblogs.com/xu-xiang/p/10231827.html
Copyright © 2020-2023  润新知