线性结构定义
如果一个数据元素序列满足:
(1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素;
(2)第一个数据元素没有前驱数据元素;
(3)最后一个数据元素没有后继数据元素。
则称这样的数据结构为线性结构。
线性表抽象数据类型
线性表抽象数据类型主要包括两个方面:既数据集合和该数据集合上的操作集合。
数据集合可以表示为a0,a1,a2,...an-1,每个数据元素的数据类型可以是任意的类型。
操作集合包括如下:
1.求元素个数
2.插入
3.删除
4.查找
5.判断是否为空
5.判断是否为空
设计线性表抽象数据类型的Java接口
- <span style="font-size:18px">public interface List {
- // 获得线性表长度
- public int size();
- // 判断线性表是否为空
- public boolean isEmpty();
- // 插入元素
- public void add(int index, Object obj) throws Exception;
- // 删除元素
- public void delete(int index) throws Exception;
- // 获取指定位置的元素
- public Object get(int index) throws Exception;
- }</span>
- public class ArrayList implements List {
- //默认的顺序表的最大长度
- final int defaultSize = 10;
- //当前长度
- int currentSize;
- //最大长度
- int maxSize;
- //对象数组
- Object[] listArray;
- //构造方法,默认大小
- public ArrayList() {
- init(this.defaultSize);
- }
- //构造方法,设置大小
- public ArrayList(int length) {
- init(length);
- }
- //线性表的初始化方法
- private void init(int length) {
- maxSize = length;
- this.currentSize = 0;
- listArray = new Object[length];
- }
- //ArrayList的大小
- public int size() {
- return this.currentSize;
- }
- //ArrayList是否为空
- public boolean isEmpty() {
- return this.currentSize == 0;
- }
- //添加数据
- public void add(int index, Object obj) throws Exception {
- if (this.currentSize == this.maxSize) {
- throw new Exception("线性表已满,无法插入!");
- }
- if (index < 0 || index >= this.maxSize) {
- throw new Exception("参数错误!");
- }
- for (int j = this.currentSize; j > index; j--) {
- listArray[j] = listArray[j - 1];
- }
- listArray[index] = obj;
- this.currentSize++;
- }
- //删除数据
- public void delete(int index) throws Exception {
- if (this.isEmpty()) {
- throw new Exception("线性表为空,无法删除!");
- }
- if (index < 0 || index > this.maxSize - 1) {
- throw new Exception("参数错误!");
- }
- for (int j = index; j < this.currentSize - 1; j++) {
- listArray[j] = listArray[j + 1];
- }
- this.currentSize--;
- }
- //查找指定索引的数据
- public Object get(int index) throws Exception {
- if (index < 0 || index >= this.currentSize) {
- throw new Exception("参数错误!");
- }
- return listArray[index];
- }
- }
ArrayList插入和删除一个元素的时间复杂度为O(n)。
ArrayList支持随机访问,ArrayList读取一个元素的时间复杂度为O(1)。
ArrayList的优点是:支持随机访问,空间利用率高。
ArrayList的缺点是:大小固定,插入和删除元素需要移动大量的数据。
测试案例:
- //学生类
- public class Students {
- private String sid;// 学号
- private String name;// 姓名
- private String gender;// 性别
- private int age;// 年龄
- public Students()
- {
- super();
- }
- public Students(String sid,String name,String gender,int age)
- {
- this.sid = sid;
- this.name = name;
- this.gender = gender;
- this.age =age;
- }
- public String toString()
- {
- return "学号:"+this.getSid()+" 姓名:"+this.getName()+" 性别:"+this.getGender()+" 年龄:"+this.getAge();
- }
- public String getSid() {
- return sid;
- }
- public void setSid(String sid) {
- this.sid = sid;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- public class ArrayListTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- List list = new ArrayList();
- try {
- list.add(0, new Students("S0001", "张三", "男", 18));
- list.add(1, new Students("S0002", "李四", "男", 19));
- list.add(2, new Students("S0003", "王五", "女", 21));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("*************************");
- for (int i = 0; i < list.size(); i++) {
- try {
- System.out.println(list.get(i).toString());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- try {
- list.delete(1);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("##############################");
- for (int i = 0; i < list.size(); i++) {
- try {
- System.out.println(list.get(i).toString());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
测试结果:
*************************
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0002 姓名:李四 性别:男 年龄:19
学号:S0003 姓名:王五 性别:女 年龄:21
##############################
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0003 姓名:王五 性别:女 年龄:21
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0002 姓名:李四 性别:男 年龄:19
学号:S0003 姓名:王五 性别:女 年龄:21
##############################
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0003 姓名:王五 性别:女 年龄:21