小言_互联网的博客

实现顺序表

350人阅读  评论(0)

总结:1.顺序表就是我们熟悉的数组,擅于随机访问,
2.给定位置,能够高效的获取/修改指定位置的值时间复杂度是O(1)
3.顺序表按值查找,插入,删除,时间复杂度O(N)
4.对于尾插和尾删,时间复杂度O(1)

//import java.util.Arrays;
public class SeqList {
    private int[] data = new int[10];
    private int size = 0;
    //打印顺序表
    public void display() {
        //System.out.println(Arrays.toString(data));
        System.out.print("[");
        for (int i = 0; i < size; i++) {
            System.out.print(data[i]);
            if(i != size - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }
    //在pos位置新增元素
    public void add(int pos,int elem) {
        if (pos < 0 || pos > size) {
            return;
        }
        if(this.size >= this.data.length) {
            realloc();//扩容
        }
        if(pos == size) {
            this.data[pos] = elem;
            size++;
            return;
        }
        for(int i = this.size;i > pos;i--) {
            this.data[i] = this.data[i-1];
        }
        this.data[pos] = elem;
        this.size++;
    }
    private void realloc() {
        int[] newData = new int[this.data.length * 2];
        for (int i = 0; i < this.data.length; i++) {
            newData[i] = this.data[i];
        }
        this.data = newData;
    }
    //判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (this.data[i] == toFind) {
                return true;
            }
        }
        return false;
    }
    //return this.search(toFind) != -1;
    //查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if(this.data[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    //获取pos位置的元素
    public int getPos(int pos) {
        return this.data[pos];
    }
    //给pos位置的元素设为elem
    public void setPos(int pos,int elem) {
        this.data[pos] = elem;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int pos = search(toRemove);
        if(pos == -1) {
            return;
        }
        if(pos == this.size - 1) {
            this.size--;
            return;
        }
        for (int i = pos; i < size - 1; i++) {
            this.data[i] = this.data[i + 1];
        }
        this.size--;
    }
    //获取顺序表的长度
    public int size () {
        return this.size;
    }
    //清空顺序表
    public void clear() {
        this.size = 0;
        this.data = new int[10];
    }
}

单元测试

public class Test {
    public static void main(String[] args) {
        testdisplay();
        testadd();
        testcontains();
        testsearch();
        testgetPos();
        testsetPos();
        testremove();
    }
    public static void testdisplay(){
        System.out.println("测试打印:");
        SeqList seqList = new SeqList();
        seqList.display();
    }
    public static void testadd() {
        System.out.println("测试 add 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.add(2,5);
        seqList.display();
    }
    public static void testcontains() {
        System.out.println("测试 contains 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.add(2,5);
        boolean result = seqList.contains(5);
        System.out.println("result预期是 true;实际是 " +result);
    }
    public static void testsearch() {
        System.out.println("测试 search 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.add(2,5);
        int result = seqList.search(5);
        System.out.println("result预期是 2;实际是 " + result);
    }
    public static void testgetPos() {
        System.out.println("测试 getPos 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        int result = seqList.getPos(3);
        System.out.println("result预期是 4;实际是 "+result);
    }
    public static void testsetPos() {
        System.out.println("测试 setPos 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.setPos(2,5);
        System.out.println("预期值:[1,2,5,4]");
        System.out.print("实际值:");
        seqList.display();
    }
    public static  void testremove() {
        System.out.println("测试 remove 方法:");
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.remove(2);
        System.out.println("预期值:[1,3,4]");
        System.out.print("实际值:");
        seqList.display();
    }
}

转载:https://blog.csdn.net/weixin_44945537/article/details/102074881
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场