数据结构与算法--1Java实现线性表的顺序存储

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_34122324/article/details/80093941

Java实现线性表的顺序存储




1.基本思路

1.基本数据的算法实现基本包括四个最重要的功能:增、删、改、查
2.在这个例子中,我是用数组来实现线性表的顺序存储。如果数组空间不够,那就要扩大数组。
3.线性表在数组内进行存储,主要参数有三个:数组arrs,数组长度maxSize,线性表List长度size


2.实际功能列表

实际功能列表

实际功能列表



3.代码块

3.1要点

3.1.1扩大数组:

首先要创建一个更大的数组,然后把arrs通过中介数组oldarrs传递到新的大数组newarrs里面去。

if ((size == maxSize)){
            //Expand the size
            int[] oldarrs;
            int[] newarrs;
            oldarrs = arrs;
            newarrs = new int[maxSize * 2];
            for (int j = 0; j < size; j++) {
                newarrs[j] = oldarrs[j];
            }
            maxSize = maxSize * 2;
            arrs = newarrs;
        }

3.1.2插入功能(增)

1.如果线性表的size已满(size == maxSize),就要扩大数组(详见3.1.1)
2.判断i是否超出范围((i < 1) || (i > (size+1)))
3.分两种情况进行插入:插入中部和插入尾部

public void add(int i, int e){
        if ( (i < 1) || (i > (size+1)) ){
            throw new IllegalArgumentException(" The i is out of bound!");
        }
        if ((size == maxSize)){
            //Expand the size
            int[] oldarrs;
            int[] newarrs;
            oldarrs = arrs;
            newarrs = new int[maxSize * 2];
            for (int j = 0; j < size; j++) {
                newarrs[j] = oldarrs[j];
            }
            maxSize = maxSize * 2;
            arrs = newarrs;
        }
        if (i <= size){
            for (int j = size - 1; j >= i - 1; j--) {
                arrs[j + 1] = arrs[j];
            }
        }

        arrs[i-1] = e;
        size++;
        display();
    }

3.1.3删除功能(删):

1.判断线性表是否为空(size == 0)
2.判断i是否超出范围(i < 1 || i > size)
3.分两种情况进行删除:删除中部和删除尾部

    public void remove(int i){
        if (size == 0){
            throw new IllegalArgumentException(" Sorry, the List is null!");
        }
        if (i < 1 || i > size){
            throw new IllegalArgumentException(" Sorry, the index is out of bound!");
        }
        if (i < size){
            for (int j = i; j < size; j++) {
                arrs[j - 1] = arrs[j];
            }
        }
        size--;
        display();
    }


3.2基本功能


public class LinearList {
    //Create a new array
    private int[] arrs;

    //The default size
    public static final int DEFAULT_SIZE = 10;

    //The array's size
    private int maxSize;

    //The List size
    private int size;

    /*
    Two method to initialize the List
    1.Default Size
    2.Using the appointed size
     */
    public LinearList(){
        this(DEFAULT_SIZE);
    }

    public LinearList(int size){
        maxSize = size;
        arrs = new int[maxSize];
    }

    //Determine if the List is empty
    public boolean isEmpty(){
        if (size == 0){
            System.out.println(" The List is empty!");
            return true;
        }else {
            return false;
        }
    }

    //Get the length of the List
    public int getLength(){
        return size;
    }

    //Get the appointed element
    public int getElement(int i){
        if ((size == 0) || (i < 1) || (i > size)){
            throw new IllegalArgumentException(" The i is out of bound!");
        }
        return arrs[i-1];
    }

    public void LocateElement(int e){
        for (int i = 0; i < size; i++) {
            if (arrs[i] == e){
                System.out.println(" Existing in the " + " index.");
                break;
            }
            if (size == i+1){
                System.out.println(" The element is not existent.");
            }
        }
    }

    /*
    Insert the element e into the ith filed of the List
    1.Determine if the index i is out of bound
    2.if the size is out of bound, expanding the List Size
    3.Divide two situation when you wanna insert element
        ->between the List
        ->into the end of the List
     */
    public void add(int i, int e){
        if ( (i < 1) || (i > (size+1)) ){
            throw new IllegalArgumentException(" The i is out of bound!");
        }
        if ((size == maxSize)){
            //Expand the size
            int[] oldarrs;
            int[] newarrs;
            oldarrs = arrs;
            newarrs = new int[maxSize * 2];
            for (int j = 0; j < size; j++) {
                newarrs[j] = oldarrs[j];
            }
            maxSize = maxSize * 2;
            arrs = newarrs;
        }
        if (i <= size){
            for (int j = size - 1; j >= i - 1; j--) {
                arrs[j + 1] = arrs[j];
            }
        }

        arrs[i-1] = e;
        size++;
        display();
    }

    /*
    Remove the element e from the index i of the List
    1.Determine if the index i is out of bound, Determine if the size is 0
    2.Remove the element from the List
    3.Divide two situation when you wanna remove element
        ->between the List
        ->on the end of the List
     */
    public void remove(int i){
        if (size == 0){
            throw new IllegalArgumentException(" Sorry, the List is null!");
        }
        if (i < 1 || i > size){
            throw new IllegalArgumentException(" Sorry, the index is out of bound!");
        }
        if (i < size){
            for (int j = i; j < size; j++) {
                arrs[j - 1] = arrs[j];
            }
        }
        size--;
        display();

    }

    //Clear the List
    public void removeAll() {
        if(arrs != null){
            for (int i = 0; i < size; i++) {
                arrs[i] = 0;
            }
        }
        size = 0;
        System.out.println(" Clear completed!");
        display();
    }


    //display the List
    public void display(){
        if (arrs != null){
            System.out.println("");
            for (int i = 0; i < size; i++) {
                System.out.print(" " + arrs[i]);
            }
        }
    }
}


3.3测试代码


public static void main(String[] args) {
        LinearList linearList = new LinearList(5);
        linearList.add(1,1);
        linearList.add(2,2);
        linearList.add(1,3);
        linearList.add(1,4);
        linearList.add(1,5);
        linearList.add(1,6);
        System.out.println();

        linearList.remove(1);
        linearList.remove(5);
        System.out.println();
        System.out.println();

        linearList.LocateElement(3);
        linearList.LocateElement(2);
        System.out.println();

        linearList.removeAll();
    }


3.4输出示例

输出示例



4.线性表顺序存储的优缺点

优点 缺点
无须为表示表中元素之间的逻辑关系而增加额外的存储空间 插入和删除操作需要移动大量的元素
可以快速地存取表中任一位置的元素 当线性表长度变化较大时,难以确定存储空间的容量
. 造成存储空间的碎片

猜你喜欢

转载自blog.csdn.net/baidu_34122324/article/details/80093941
今日推荐