数据结构之顺序表Java实现

元素默认为char类型,代码中有详细注释,可编译运行

接口:

package linearList;

public interface ILiist {
	public void clear();// 置空表

	public boolean isEmpty();// 是否为空

	public int length();// 数据元素个数

	public char get(int i) throws Exception;// 返回第i个数据元素的值

	public void insert(int i, char x) throws Exception;// 在第i个数据元素之前插入一个值为x的数据元素

	public void remove(int i) throws Exception;//删除并返回第i个元素
	
	public int indexOf(char x);//返回首次出现指定的数据元素的位序号,
	                             //若线性表不包含此数据元素,则返回-1
	
	public void display();//输出
}

具体实现:

package linearList;

public class SqList implements ILiist{
	
	private char[] listElem;//线性表存储空间
	private int curLen;//线性表当前长度
	
	//顺序表构造函数,构造一个存储空间容量位maxSize的线性表
	public SqList(int maxSize){
		curLen=0;//置顺序表的当前长度为0
		listElem=new char[ maxSize];//为顺序表分配maxSize个存储单元
	}
	
	//将一个已经存在的线性表置成空表
	public void clear(){
		curLen=0;//置顺序表当前的长度为0
	}
	
	//判断线性表中的数据元素个数是否为0
	public boolean isEmpty(){
		return curLen==0;//为0则返回true,否则返回false
	}
	
	//求线性表中数据元素的个数并返回其值
	public int length(){
		return curLen;//返回顺序表的当前长度
	}
	
	//读取到线性表中的第i个元素并由函数返回其值,
	//其中i的取值范围为:0<=i<=length()-1
	//若i不在此范围内则抛出异常
	public char get(int i)throws Exception{
		if(i<0||i>curLen-1){//i小于0或者i大于表长减一
			throw new Exception("第"+i+"个元素不存在");//抛出异常
		}
		return listElem[i];//返回第i个数据元素
	}
	
	// 在第i个数据元素之前插入一个值为x的数据元素
	public void insert(int i, char c) throws Exception{
		if(curLen==listElem.length){//判断顺序表是否已满
			throw new Exception("顺序表已满");//抛出异常
		}
		if(i<0||i>curLen){//如果i不合法
			throw new Exception("插入位置不合法");//抛出异常
		}
		for(int j=curLen;j>i;j--){
			listElem[j]=listElem[j-1];//插入位置及其之后的所有数据元素后移一位
									  //注意是j=j-1,因为curLen是表的长度
		}
		listElem[i]=c;//插入x
		curLen++;//表长加1
	}

	//删除并返回第i个元素
	public void remove (int i)throws Exception{
		if(i<0||i>curLen-1){//i不合法
			throw new Exception("删除位置不合法");//抛出异常
		}
		for(int j=i;j<curLen;j++){
			listElem[j]=listElem[j+1];//被删除元素之后的所有数据元素左移一个存储位置
		}
		curLen--;//表长减1
	}
	
	//返回首次出现指定的数据元素的位序号,若线性表不包含此数据元素,则返回-1
	public int indexOf(char x){
		int j=0;//j指示顺序表中待比较的数据元素,其初始值指示指示顺序表中第0个数据元素
		while(j<curLen&&!(listElem[j]==(x))){//依次比较
			j++;
		}
		if(j<curLen){//判断j的位置是否在顺序表中
			return j;//返回值为x的位置的数据元素在顺序表中的位置
		}
		else
		return -1;//值为x的数据元素在顺序表中不存在
	}
	
	//输出
	public void display(){
			for(int j=0;j<curLen;j++){
				System.out.print(listElem[j]+" ");
			}
			System.out.println();
	}
}
示例:

package exapmle;

import linearList.SqList;

public class Example2_1 {
	public static void main(String[] args)throws Exception{
		SqList L=new SqList(10);
		L.insert(0, 'a');
		L.insert(1, 'b');
		L.display();
	}
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值