1.線性順序表

線性結構概念:數組

1.除了第一個和最後一個元素,每一個元素都有一個前驅和一個後繼元素ide

2.第一個元素沒有前驅this

3.最後一個元素沒有後繼接口

 

操做:內存

1.元素個數get

2.插入it

3.刪除io

4.查找class

5.判斷是否爲空List

/**
* 線性表接口
*/
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)優勢:支出隨機訪問,底層數組,內存連續,空間利用率高肯定:大小固定,插入刪除須要移動大量的數據
相關文章
相關標籤/搜索