数据结构(二)
1.线性表的顺序存储
-
数组(Array):实现顺序存储结构的基础;只能进行赋值、取值两种随机存取操作,不能进行插入、删除操作;数组容量不够,不能就地扩容
-
顺序表(SeqList):使用一维数组依次存放线性表A0到A(n-1)的数据元素;解决溢出的方法:申请另一个更大容量的数组并进行数组元素复制
-
随机存储结构:存取任何一个元素的时间复杂度为O(1)的数据结构;数组和顺序表皆是
2.线性表实现package List;
import Array.Triple; public class SeqList<T> extends Object{ protected Object[]element; //定义数组储存顺序表的数据元素 protected int n; //顺序表元素个数 public SeqList(int length) { //构造长度为length的空表 this.element=new Object[length]; //申请数组的存储空间 this.n=0; } public SeqList() { this(64); } public SeqList(T[]values) { //构造顺序表,由values数组提供元素 this(values.length); //创建容量为values.length的空表 for(int i=0;i<values.length;i++) { //复制数组元素 this.element[i]=values[i]; } this.n=element.length; } public boolean isEmpty() { //判断线性表是否为空 return this.n==0; } public int size() { return this.n; } public T get(int i) { //返回第i个元素,0<=i<n;若i越界,则返回null if(i>=0&&i<this.n) { return(T)this.element[i];