0
点赞
收藏
分享

微信扫一扫

【Java数据结构】顺序表

目录

1. 思路

2. 顺序表的创建

3. 实现顺序表功能

4. 源代码

MyArrayList.java

test.java


1. 思路

变量核心:一个elem数组,一个usedSize记录数组大小,一个capacity记录数组容量

顺序表在逻辑上和物理上都是连续的。

2. 顺序表的创建

public class MyArrayList {
    public int[] elem;  //elem=null
    public int usedSize;
    public int capacity = 10;

    public MyArrayList() {
        this.elem = new int[capacity];
    }
}

new创建一个MyAraryList时,其在内存中的情况如下:

3. 实现顺序表功能

需要支持的接口如下:

    // 判断是否为空
    public boolean isEmpty() {    }
    // 判断是否表满
    public boolean isFull() {    }

    // 打印顺序表
    public void display() {    }
    // 在 pos 位置新增元素
    public void insert(int pos, int data) {    }
    // 判定是否包含某个元素
    public boolean isExist(int value) {    }

    // 查找某个元素的位置
    public int search(int value) {    }
    // 获取 pos 位置的元素
    public int getValue(int pos) {    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {   }

    // 删除pos位置的元素
    public void delete(int pos) {
    // 删除第一次出现的关键字key
    public void remove(int toRemove) {   }

    // 获取顺序表长度
    public int length() {    }
    // 清空顺序表
    public void clear() {   }

4. 源代码

MyArrayList.java

一定要注意:在插入元素时判断表满的情况,满的话二倍扩容!

import java.util.Arrays;
public class MyArrayList {
    public int[] elem;
    public int usedSize;
    public int capacity = 10;
    public MyArrayList (){
        this.elem = new int[capacity];
    }
    //判断是否为空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    //判断是否表满
    public boolean isFull() {
        return this.usedSize==this.capacity;
    }
    //打印顺序表
    public void display() {
        if (this.isEmpty()) {
            throw new RuntimeException("表为空");
        }
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+ " ");
        }
        System.out.println();
    }
    //在pos处插入data
    public void insert(int pos, int data) {
        if (pos < 0 || pos > this.usedSize) {
            throw new RuntimeException("插入位置不合法");
        }
        if (isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem, 2*capacity);
            capacity *= 2;  //新的容量
        }
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    //判断是否包含某个元素
    public boolean isExist(int value) {
        if (isEmpty()) {
            throw new RuntimeException("表为空");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == value) {
                return true;
            }
        }
        return false;
    }
    //获取某个元素的位置
    public int search(int value) {
        if (isEmpty()) {
            throw new RuntimeException("表为空");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == value) {
                return i;
            }
        }
        throw new RuntimeException("没有找到该元素");
    }
    //获取pos位置的元素
    public int getValue(int pos) {
        if (pos < 0 || pos > this.usedSize-1) {
            throw new RuntimeException("查找位置不合法");
        }
        return this.elem[pos];
    }
    //给pos位置的元素设为value
    public void setPos(int pos, int value) {
        if (pos < 0 || pos > this.usedSize-1) {
            throw new RuntimeException("位置不合法");
        }
        this.elem[pos] = value;
    }
    //删除pos位置的元素
    public void delete(int pos) {
        if (pos < 0 || pos > this.usedSize-1) {
            throw new RuntimeException("删除位置不合法");
        }
        for (int i = pos; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }
    //删除第一次出现的关键字
    public void remove(int key) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == key) {
                for (int j = i; j < this.usedSize-1; j++) {
                    this.elem[j] = this.elem[j+i];
                }
                this.usedSize--;
                break;
            }
        }
    }
    //获取顺序表的长度
    public int length() {
        return this.usedSize;
    }
    //清空顺序表
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
    }
}

test.java

测试代码,根据需要使用

public class test {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.insert(0,1);
        myArrayList.insert(1,2);
        myArrayList.insert(2,3);
        myArrayList.insert(3,4);
        myArrayList.insert(4,5);
        myArrayList.display();
        myArrayList.delete(2);
        System.out.println(myArrayList.usedSize);
        myArrayList.display();
        System.out.println(myArrayList.search(5));
        myArrayList.insert(4,6);
        myArrayList.display();
        myArrayList.insert(4,99);
        myArrayList.display();
    }
}
/*
1 2 3 4 5 
4
1 2 4 5 
3
1 2 4 5 6 
1 2 4 5 99 6 
*/
举报

相关推荐

0 条评论