数据结构之顺序表(java语言版)

数据结构之顺序表(java语言版)

顺序表是最简单的线性表,也就是数组。很多语言都把把它当做内置的基本数据类型,这里的数组没有对应数据结构的操作。

数组是顺序存储的结构,连续分配一段内存用于存储数据。在逻辑结构和物理结构上都是连续的。

数组

顺序表建立

在java内置的数组上建立顺序表。

public class Array{
	private Object[] data;//容器
	private int size;//实际长度
    private int cap;//容量

	
	public Array() {//默认构造方法
		size=0;
		cap=15;
		data=new Object[cap];
	}


	public Array(int cap) {
		size=0;
		this.cap=cap;//传入容器容量
		data=new Object[cap];
	}

}

基本操作

顺序表有容量,那么就需要判空和判满。

public boolean isEmpty() {//判空
		if(this.getSize()==0) {
			return true;
		}
		return false;
	}
	
public boolean isFull() {//判满
		if(this.getSize()==this.cap) {
			return true;
		}
		return false;
	}
public int getSize() {//得到实际长度
		return size;
	}

查询数据和插入都是通过索引,所以需要判断索引是否异常。

public void isError(int index){//索引异常
        if(index<0||index>=getSize()){
            System.out.println("\n索引异常,结束程序");
            System.exit(0);
        }
    }

查询元素

通过索引查询元素

public Object get(int index){//查询元素
        if(isError(index)){
            return null;
        }
        return this.data[index];
    }

删除元素

从头部删除,尾部删除,以及通过索引删除。

从尾部移除元素最简单,直接将实际长度减一即可。

public Object remove() {//移除
		return this.data[--size];
    }

从头部或者通过索引移除数据,则操作比较麻烦,被移除的数据位置需要由后面的数据顶上。

在这里插入图片描述

public Object del(int index){//删除元素
        if(isError(index)){
            return null;
        }
        if(index==size-1){
            return remove();
        }
        for(int i=index;i<getSize()-1;i++){
            this.data[i]=this.data[i+1];
        }
        size--;
        return this.data[index];
    }

public Object removeHead(){//从头部移除
        return del(0);
    }

增加元素

public boolean add(Object e) {//增加元素
		if(this.isFull()) {
			System.err.println("容器已满,无法添加\n程序结束");
			System.exit(0);
		}
		this.data[size]=e;
		this.size++;
		System.out.println("add success");
		return true;
    }

遍历

遍历就是访问数组中每个数据。

插入的逻辑和删除一样,移动数据,可以自己尝试实现。

public void display() {//遍历
		System.out.println("display: ");
		for(int i=0;i<this.getSize();i++) {
			System.out.print(" "+this.data[i]);
		}
		System.out.println("\nover...");
    }
    

插入

插入和删除逻辑是一样,移动数据。

 public boolean insert(int index,Object e){//插入

        if(index==getSize()){//先判断是否是追加,再判断索引是否越界 
            return add(e);
        }

        if(isError(index)){//索引越界判断
            return false;
        }
       
        for(int i=size-index;i>0;i--){//将每个数据向后移动一位
            this.data[index+i]=this.data[index+i-1];
        }
        this.data[index]=e;
        size++;
        return true;
    }

一个简单的顺序表或者说数组就完成了。

测试

public static void main(String[] args) {
        Array array=new Array();
        array.add("hello");
        array.add("ok");
        array.add("----");
        array.add("nice");
        array.add(3);

        array.display();

        array.remove();
        array.del(3);
        array.display();
        array.insert(2, "well");
        array.insert(1, "ooo");
        array.display();
        
    }

得到以下结果:

add success
add success
add success
add success
add success
display:
 hello ok ---- nice 3
 display  over...
display:
 hello ok ----
 display  over...
display:
 hello ooo ok well ----
 display  over...

数据结构与算法学习目录

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值