基于数组实现的线性表
/**
* 线性表的顺序存储.
* @author mac
* */
public class ArrayLinearList {
private int DEFAULT_LENGTH = 10;
private Object[] items;
private int length;
/**
* 1.初始化
* 2.插入元素
* 3.删除元素
* 4.获取元素
*
* 5.判断是否为空
* 6.清空
* 7.获取当前长度
* */
public ArrayLinearList(){
items = new Object[DEFAULT_LENGTH];
this.length = 0;
}
public ArrayLinearList(int capacity) {
if(capacity < 1)
throw new IllegalArgumentException("initialize capacity error.");
items = new Object[capacity];
this.length = 0;
}
/**
* 在指定位置插入元素.
* */
public boolean insert(int position, Object item) {
if(position < 1 || position > items.length) {
throw new IllegalArgumentException("invalid position.");
}
if(this.length == items.length) {
throw new RuntimeException("linear list is full.");
}
for(int i = length - 1; i >= position - 1;i --) {
items[i + 1] = items[i];
}
items[position - 1] = item;
this.length ++;
return true;
}
/**
* 在尾部插入元素
* */
public boolean insert(Object item) {
return insert(this.length + 1, item);
}
/**
* 删除指定位置上的元素
* */
public Object delete(int position) {
if(position < 1 || position > items.length) {
throw new IllegalArgumentException();
}
Object obj = items[position - 1];
for(int i = position - 1;i < length - 1;i ++) {
items[i] = items[i + 1];
}
this.length --;
return obj;
}
/**
* 删除指定元素
* */
public Object delete(Object item) {
int idx = idx(item);
return delete(idx + 1);
}
/**
* 获取指定位置上的元素
* */
public Object get(int position) {
if(position < 1 || position >= items.length){
throw new IllegalArgumentException();
}
return items[position - 1];
}
/**
* 判断当前链表是否为空
* */
public boolean isEmpty(){
return this.length == 0;
}
/**
* 获取当前链表的长度
* */
public int size() {
return this.length;
}
/**
* 获取指定元素的索引位置
* */
public int idx(Object item) {
for(int i = 0;i < this.length;i ++) {
if(items[i] == item) {
return i;
}
}
return -1;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0;i < this.length;i ++) {
sb.append(items[i] + " ");
}
return sb.toString();
}
public static void main(String[] args) {
ArrayLinearList list = new ArrayLinearList();
list.insert("a");
list.insert("b");
System.out.println(list);
list.insert(2,"c");
System.out.println(list);
list.insert(3, "d");
System.out.println(list);
list.delete(2);
System.out.println(list);
}
}
优点
- 不需要额外的存储空间存储元素之间的关系
- 可以快速存取表中的任意一个元素
缺点