数据结构之——线性表之顺序表

来源:互联网 发布:同轴圆柱形电容器算法 编辑:程序博客网 时间:2024/06/18 05:47

顺序表

  1. 顺序表的定义:
    用一组地址连续的存储单元依次存放线性表中的各个元素的数据结构。
  2. 顺序表的地址表示
    Loc(ai)=Loc(a0)+i*c (0 < i < n-1)
    Loc( a0)表示的是基地址,c表示的是一个元素所占c个存储单元
  3. 顺序表的特点
    • 物理存储和逻辑存储相同
    • 存储密度高,存储密度=数据元素本身值所需的空间/该元素实际所占的空间
    • 便于随机存储
    • 不便于插入和删除操作

算法设计

插入算法图解,当插入一个数据时,所有的数据都要向后移动。我们要先移动数据,空出插入的位置,然后把插入的值赋予这个空位。
这里写图片描述

//插入     public void insert(int index,Object element) {           //currSize是当前数组的实际长度,capacity为数据的容量           if (currSize==capacity) {                throw new IndexOutOfBoundsException("数组已满");           }           if (index<0||index>currSize) {                throw new  IndexOutOfBoundsException("当前位置不合适");           }                    //cursize未加一,刚好是增加了一个元素后的下标值           for(int i=currSize;i>index;i--){                elements[i] = elements[i-1];           }           elements[index]=element;           currSize++;     }

删除算法图解,删除一个元素,删除位置之后的元素向前移动
这里写图片描述

//删除     public void delete(int index) {           if (index<0||index>currSize) {                throw new IndexOutOfBoundsException("删除位置不符合");           }           for(int i=index;i<currSize-1;i++){                elements[i]=elements[i+1];           }           currSize--;     }

完整代码:

//顺序表public class MyList  {     //声明数组     public Object[] elements;     //当前数组的长度     public int currSize;     //数组长度     public int capacity;      public MyList(int capacity) {            this.capacity = capacity;            elements = new   Object [capacity];//初始化数组的长度      }     public void clear() {           currSize=0;     }     public boolean isEmpty() {           return currSize==0;     }     //获得当前长度     public int getLength(){           return currSize;     }    //获取某个位置的元素          public Object get(int index) {           if (index<0||index>capacity) {                throw new IndexOutOfBoundsException("数组越界");           }           return elements[index];     }     //插入     public void insert(int index,Object element) {           if (currSize==capacity) {                throw new IndexOutOfBoundsException("数组已满");           }           if (index<0||index>currSize) {                throw new  IndexOutOfBoundsException("当前位置不合适");           }           for(int i=currSize;i>index;i--){                elements[i] = elements[i-1];           }           elements[index]=element;           currSize++;     }     //删除     public void delete(int index) {           if (index<0||index>currSize) {                throw new IndexOutOfBoundsException("删除位置不符合");           }           for(int i=index;i<currSize-1;i++){                elements[i]=elements[i+1];           }           currSize--;     }     public boolean indexof(Object element) {           for(int i=0;i<currSize;i++){                if (elements[i].equals(element)) {                     return true;                }           }           return false;     }     public void display() {           for (int i = 0; i < currSize; i++) {                System.out.print(elements[i]+" ");           }           System.out.println();     }     //测试代码     public static void main(String[] args) {           MyList list = new MyList(5);           list.insert(0, 20);           list.insert(1, 15);           list.insert(2, 9);           list.insert(2, 44);           list.display();            list.insert(1, 3);           list.display();           System.out.println(list.indexof(5));           list.delete(2);           list.display();           list.insert(2, 4);           list.display();           System.out.println(list.get(2));     }}

结果为:

20 15 44 920 3 15 44 9false20 3 44 920 3 4 44 94
原创粉丝点击