数据结构顺序表的定义(Java)

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

定义顺序表的接口

public interface ILinarList<E> {
    public abstract boolean add(E item);          //添加元素
    public abstract boolean add(int i,E item);    //插入元素
    public abstract E remove(int i);              //删除元素
    public abstract int indexOf(E item);          //定位元素
    public abstract E get(int i);                 //取表元素 
    public abstract int size();                   //求线性表长度
    public abstract void clear();                 //清空线性表
    public abstract boolean isEmpty();            //判断线性表是否为空
    public abstract boolean isFull();             //判断线性表是否满


}

实现顺序表的接口

import java.lang.reflect.Array;

public class SeqList<E> implements ILinarList<E> {
    private int maxsize;   //顺序表的最大容量
    private E[] data;      //储存顺序表中数据元素的数组
    private int size;      //实际长度
    public SeqList(Class<E> type,int maxsize){
        this.maxsize=maxsize;
        data=(E[])Array.newInstance(type, maxsize);//不懂存疑
        size=0;
    }
    //添加功能
    public boolean add(E item) {
        if(!isFull()){//检查是否满了
            data[size++]=item;
            return true;
        }
        else
            return false;
    }
    public boolean add(int i, E item) {
        if(i<0||i>size){//检查是否在范围内,但是不能等于size,如果等于size也可以
            throw new IndexOutOfBoundsException("Index:"+i+",size:"+size);//跑出越界异常
        }
        if(!isFull()){
            for(int j=size;j>i;j--){//要从尾到头
                data[j]=data[j-1];
            }
            data[i]=item;
            size++;//每次改变长度size一定要操作
            return true;
        }
        else
            return false;
    }
    public E remove(int i) {
        rangeCheck(i);//与上面的检查不一样,这次可以等于size
        if(!isEmpty()){
            E item=data[i];//记录需要删除的元素
            for(int j=i;j<size-1;j++){//从头到尾,注意是size-1,如果仅仅小于size的话,当进行到size-1的时候,没有size的元素
                data[j]=data[j+1];
            }
            data[--size]=null;//要进行清除最后一个元素的操作
            return item;//删除成功返回被删除的元素,虽然不知到为什么要这样做
        }
        else
            return null;

    }
    public int indexOf(E item) {//需要测试item=null是会有什么结果
        if(!isEmpty()){
            for(int j=0;j<size;j++){
                if(item==data[j])
                    return j;
            }
        }
        return -1;//此处不可以加else,上面两个if可能会导致没有出口
    }
    public E get(int i) {
        rangeCheck(i);
        if(!isEmpty()){
            return data[i];
        }
        else
            return null;
    }
    public int size() {
        return size;
    }
    public void clear() {
        for(int j=0;j<size;j++)
            data[j]=null;
        size=0;
    }
    public boolean isEmpty() {
        return size==0;//返回一个Boolean
    }
    public boolean isFull() {
        return size==maxsize;
    }
    private void rangeCheck(int i) {
        if(i<0||i>=size){
            throw new IndexOutOfBoundsException("Index:"+i+",size:"+size);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/c_yejiajun/article/details/78383409