java 的数据结构之路——顺序表ArrayList(附带Java、C语言 版本的代码)

线性表

线性表顾名思义就是将数据元素排列成一条线一样的表,严格来说,线性表是具有相同特性的元素的一个有序的序列。其特征有三,其一是所有的数据元素的类型相同,线性表示有有限个数据元素组成,并且数据元素与位置相关。本博客首先介绍线性表中的顺序存储结构——顺序表

线性表的顺序存储结构——顺序表

线性表的顺序存储结构是把线性表中的所有元素按照其逻辑顺序依次的存储到内存中的一块连续的存储储空间中的。线性表的顺序存储结构称作为顺序表。那么顺序表是如何实现的呢?

顺序表的实现代码(以存储Int类型的元素为例)

首先我们需要进行一个My_ArrayList的类的创建,用来存放我们的顺序表,在这个类中,我们需要有两个成员变量,一个是一个Int类型(因为是以Int类型为例子)的数组,另一个需要把这个数组的长度单独的定义出来,以便于日后的使用。我们还需要定义一个final类型的变量,来规定这个顺序表的初始的大小。

private int[] array;//用来存放数据的数组
private int usedSized;//数组的长度
private static final int ARRAYSTARTED = 5;数组的初始大小

 在成员变量定义好之后,运用构造方法来对成员变量进行初始化。

public My_ArrayList(){
        this.usedSized = 0;
        this.array = new int[ARRAYSTARTED];
    }

在写之前还要进行异常类的处理,这里为了方便阅读进行了简化,为了突出数据结构部分 

public class RentimeExtion extends Exception{
    public RentimeExtion() {
    }
    public RentimeExtion(String message){
        super(message);
    }
}

 

 现在我们要手动手动的实现ArrayList的各种方法,功能方法可以概括为以下的几个方面,增删查改清。


ArrayList  增  

在增操作中,包含着两个add重载方法,首先是第一个,第一个方法是add(int data),给顺序表传入一个参数,把这个参数默认的放置到顺序表的末尾。有这么几点值得注意的是,1、增加元素有可能使得我们的下标越界,这是我们就需要动态的扩容。这也就需要我们进行数组是否满了的判断2、我们最好把当前的数组的有效长度进行一个封装,下次引用直接引用函数。3、在进行完增加操作后,需要我们手动的把数组的有效长度增加一个。

 首先是获得数组有效长度的方法size():

//获得顺序表的长度
    public int size(){
        return this.usedSized;
    }

add(int data)&&isFull():

//添加顺序表(默认在最后的位置)
    public void add(int data){
        //是不是满了
        if(isFull()){
            //满了要扩容
            System.out.println("扩容");
            this.array = Arrays.copyOf(this.array, 2 * array.length);
        }
        //最后位置加上
        this.array[this.usedSized] = data;
        //长度增加
        this.usedSized ++;
    }
    //是否满了?
    public boolean isFull(){
        return this.usedSized >= array.length;
    }

在增操作中,还有一个add的重载方法,add(int pos, int data),这个方法也有几点需要注意,1、我们仍需要判断这个数组存储的元素是否已满,如果已满需要扩容。2、并且还需要判断pos的合法性。3、一切满足之后进行插入操作。4将数组的有效长度加一。

public void add(int pos, int data) throws RentimeExtion {
        //首先传入的位置必须是有意义的
        if(pos < 0 || pos > size()){
            //异常处理,pos超出了范围
            throw new RentimeExtion("pos超出了范围");
        }
        //判断扩容
        if(isFull()){
            this.array = Arrays.copyOf(array, 2* this.array.length);
        }
        //然后该位置之后的元素必须全部向后移动
        for(int i = size(); i >= pos; i--){
            array[i + 1] = array[i];
        }
        //该位置等于传入的数据
        array[pos] = data;
        //长度加一
        this.usedSized ++;
    }

 ArrayList 删 

Java的删除操作需要传入想要删除的值,就会把这个第一次出现的值进行删除。

这一步也有几点要注意的,1、就是如果顺序表里是空的,肯定是不行的。 2、如果该数不在顺序表中也是不行的。 3、删除完成后有效长度减一

//顺序表的删除
    public void remove(int key) throws Exception{
        //查看顺序表是不是空的
        if(isEmpty()) {
            //异常,是空的
            throw new RentimeExtion("空");
        }
        int pos = indexOf(key);
        if(pos == -1){
            System.out.println("没有这个数字");
            return;
        }
        //指定位置删除
        for(int i = pos; i < size() - 1; i++){
            this.array[i] = this.array[i + 1];
        }
        //长度减一
        this.usedSized--;
    }

 ArrayList的查

ArraylList中的查操作分为三个部分,一个是给指定的元素返回下标,一个是给定下标返回元素值,还有一个是给定元素看其是否存在,我们就可以写出三个简单的函数,其中需要注意的点我们已经说过,这里不再赘述 

//查找是否包含某个元素
    public boolean contains(int toFind){
        for(int i = 0; i < size(); i++){
            if(toFind == this.array[i]){
                return true;
            }
        }
        return false;
    }
    //查找某个元素对应的位置
    public int indexOf(int toFind){
        for(int i = 0; i < size(); i++){
            if(toFind == this.array[i]){
                return  i;
            }
        }
        return -1;
    }
    //查找位置下面的元素
    public int get(int pos) throws Exception{
        //判断是否是空
        if(isEmpty()){
            ///异常
            throw new RentimeExtion("空");
        }
        //首先判断pos合法
        if(pos < 0 || pos >= size()){
            //异常
            throw new RentimeExtion("pos超出了范围");
        }
         return this.array[pos];
    }

 ArrayList的改

将指定的位置的元素进行修改,改为传入的元素。应注意传入的下标是否合法,顺序表是否为空

//把下标是pos的元素改成data
    public void set(int pos, int data) throws Exception{
        if(isEmpty()){
            //异常
            throw new RentimeExtion("空");
        }
        if(pos < 0 || pos >= size()){
            //异常
            throw new RentimeExtion("pos超出了范围");
        }
        this.array[pos] = data;
    }

 ArrayList的清

清除操作就是把顺序表清空,分为引用类型的清空方法和基本类型的清空方法 

//顺序表清空
    public void clear(){
        /*
        如果是引用类型的数据因这样清空:
        for(int i = 0; i < size(); i++){
            this.array[i] = null;
        }
        */
        System.out.println("clear");
        this.usedSized = 0;
    }

 以下是ArrayList数据结构的手写代码(java)

import java.util.Arrays;

class My_ArrayList{
    private int[] array;
    private int usedSized;
    private static final int ARRAYSTARTED = 5;

    public My_ArrayList(){
        this.usedSized = 0;
        this.array = new int[ARRAYSTARTED];
    }
    //显示顺序表
    public void display_Liset(){
        for(int i = 0; i < size(); i++){
            System.out.print(array[i]);
            System.out.print(" ");
        }
        System.out.println();
    }
    //判断顺序表是否为空
    public boolean isEmpty(){
        if(this.size() == 0){
            return true;
        }else{
            return false;
        }
    }
    //获得顺序表的长度
    public int size(){
        return this.usedSized;
    }
        //添加顺序表(默认在最后的位置)
        public void add(int data){
        //是不是满了
        if(isFull()){
            //满了要扩容
            System.out.println("扩容");
            this.array = Arrays.copyOf(this.array, 2 * array.length);
        }
        //最后位置加上
        this.array[this.usedSized] = data;
        //长度增加
        this.usedSized ++;
    }
    //是否满了?
    public boolean isFull(){
        return this.usedSized >= array.length;
    }
    //在指定位置添加顺序表
    public void add(int pos, int data) throws RentimeExtion {
        //首先传入的位置必须是有意义的
        if(pos < 0 || pos > size()){
            //异常处理,pos超出了范围
            throw new RentimeExtion("pos超出了范围");
        }
        //判断扩容
        if(isFull()){
            this.array = Arrays.copyOf(array, 2* this.array.length);
        }
        //然后该位置之后的元素必须全部向后移动
        for(int i = size(); i >= pos; i--){
            array[i + 1] = array[i];
        }
        //该位置等于传入的数据
        array[pos] = data;
        //长度加一
        this.usedSized ++;
    }
    //顺序表的删除
    public void remove(int key) throws Exception{
        //查看顺序表是不是空的
        if(isEmpty()) {
            //异常,是空的
            throw new RentimeExtion("空");
        }
        int pos = indexOf(key);
        if(pos == -1){
            System.out.println("没有这个数字");
            return;
        }
        //指定位置删除
        for(int i = pos; i < size() - 1; i++){
            this.array[i] = this.array[i + 1];
        }
        //长度减一
        this.usedSized--;
    }
    //查找是否包含某个元素
    public boolean contains(int toFind){
        for(int i = 0; i < size(); i++){
            if(toFind == this.array[i]){
                return true;
            }
        }
        return false;
    }
    //查找某个元素对应的位置
    public int indexOf(int toFind){
        for(int i = 0; i < size(); i++){
            if(toFind == this.array[i]){
                return  i;
            }
        }
        return -1;
    }
    //查找位置下面的元素
    public int get(int pos) throws Exception{
        //判断是否是空
        if(isEmpty()){
            ///异常
            throw new RentimeExtion("空");
        }
        //首先判断pos合法
        if(pos < 0 || pos >= size()){
            //异常
            throw new RentimeExtion("pos超出了范围");
        }
         return this.array[pos];
    }
    //把下标是pos的元素改成data
    public void set(int pos, int data) throws Exception{
        if(isEmpty()){
            //异常
            throw new RentimeExtion("空");
        }
        if(pos < 0 || pos >= size()){
            //异常
            throw new RentimeExtion("pos超出了范围");
        }
        this.array[pos] = data;
    }
    //顺序表清空
    public void clear(){
        /*
        如果是引用类型的数据因这样清空:
        for(int i = 0; i < size(); i++){
            this.array[i] = null;
        }
        */
        System.out.println("clear");
        this.usedSized = 0;
    }
}

public class MyArrayList {
    public static void main(String[] args) throws Exception {
        My_ArrayList myArrayList = new My_ArrayList();
        myArrayList.add(12);
        myArrayList.add(23);
        myArrayList.add(21);
        myArrayList.add(34);
        myArrayList.add(22);
        //一次扩容
        myArrayList.add(44);
        myArrayList.add(12);
        myArrayList.add(23);
        myArrayList.add(21);
        myArrayList.add(34);
        //二次扩容
        myArrayList.add(22);
        myArrayList.add(44);
        myArrayList.display_Liset();
        try{
            myArrayList.add(100,23);
        }catch (RentimeExtion e){

        }

        myArrayList.display_Liset();
        myArrayList.remove(3);
        myArrayList.display_Liset();
        myArrayList.set(0,11);
        myArrayList.display_Liset();
        myArrayList.clear();
        myArrayList.display_Liset();
    }
}
public class RentimeExtion extends Exception{
    public RentimeExtion() {
    }
    public RentimeExtion(String message){
        super(message);
    }
}

(c)

        

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MaxSize 50

typedef char ElemType;
typedef int Integer;

typedef struct{
	ElemType data[MaxSize];
	int length;
}SqList;
//传入建立的结构体,传入想要进入结构体的数组,传入数组的大小
void CreateList(SqList *&L, ElemType arr[], int arr_length){
	L = (SqList*)malloc(sizeof(SqList));
	for(int i = 0; i < arr_length; i++){
		L->data[i] = arr[i];
	}
	L->length = arr_length;
}

//初始化链表,需要传入链表,
void InitList(SqList *&L){
	L = (SqList*)malloc(sizeof(SqList));
	L->length = 0;
}

//销毁链表
void DestoryList(SqList *&L){
	free(L);
}

bool ListEmpty(SqList *L){
	if(L->length == 0){
		return false;
	}else{
		return true;
	}
}

//输出线性表的长度
int ListLength(SqList *L){
	return L->length;
}

//输出线性表
void DisplayList(SqList *L){
	for(int i = 0; i < L->length; i++){
		printf("%c", L->data[i]);
	}
	printf("\n");
}

//返回线性表的第i个元素
bool GetElem(SqList *L, int pos, ElemType& elem){
	if(pos < 0 || pos > L->length){
		return false;
	}
	elem = L->data[pos];
	return true;
}

//输出元素a的位置:
int LocateElem(SqList *L, ElemType e){
	for(int i = 0; i < L->length; i++){
		if(e == L->data[i]){
			return i;
		}
	}
	return -1;
}

//在第i个位置上插入元素
bool Listinster(SqList *&L, int pos, ElemType e){
	if(pos < 0){
		return false;
	}
	for(int i = L->length; i > pos; i--){
		L->data[i] = L->data[i - 1];
	}
	L->data[pos] = e;
	L->length++;
	return true;
}

//删除第i个元素
bool ListDelete(SqList *&L, int pos){
	if(pos < 0 || pos >= L->length){
		return false;
	}
	for(int i = pos; i < L->length - 1; i++){
		L->data[i] = L->data[i + 1];
	}
	L->length --;
	return true;
}

int main(){
	ElemType arr[] = {"niyaojiehunre"};
	Integer arr_sizeof = sizeof(arr) / sizeof(arr[0]) - 1;
	SqList* L;
	InitList(L);
	Listinster(L,0,'c');
	Listinster(L,1,'a');
	DisplayList(L);
	DestoryList(L);
	CreateList(L,arr,arr_sizeof);
	
	Listinster(L,0,'a');
	ListDelete(L,1);
	DisplayList(L);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值