线性表
定义
n个元素组成的有限的有序的一组数据,且都是相同的数据类型,个数n记为长度;
概念:
首节点,尾节点,空表,前驱,后继,直接前驱,直接后继-->顾名思义
代码实现
- 依靠顺序存储结构实现
- 依靠链式存储结构实现
两种存储结构比较
顺序储存
优点:
1.存储密度大,存储空间利用率高;
缺点:
1.插入删除时不方便,需要挪动其内存位置;
链式存储:
优缺点与上述相反,可从其原理分析,不赘述;
顺序结构实现
接口:
public interface List {
void clear();
Boolean isEmpty();
int length();
Object get(int index) throws Exception;
void insert(Object obj, int index) throws Exception;
void remove(int index) throws Exception;
int indexOf(Object obj);
}
接口实现:
public class LinearList implements List {
private Object[] listElem;
private int curLength;
public LinearList(int maxSize) {
this.curLength = 0;
this.listElem = new Object[maxSize];
}
@Override
public void clear() {
Arrays.fill(listElem,null);
curLength = 0;
}
@Override
public Boolean isEmpty() {
return curLength == 0;
}
@Override
public int length() {
return curLength;
}
@Override
public Object get(int index) throws Exception {
if (index < 0 || index > curLength - 1)
throw new Exception("out Of Inedx Exception");
return listElem[index];
}
@Override
public void insert(Object obj, int index) throws Exception {
if (curLength == listElem.length)
throw new Exception("full size , can't insert");
if (index < 0 || index > curLength)
throw new Exception("out of index exception");
for (int i = index; i < curLength - 1; i++)
listElem[i] = listElem[i - 1];
listElem[index] = obj;
curLength++;
}
@Override
public void remove(int index) throws Exception {
if (index < 0 || index > curLength)
throw new Exception("out of index exception");
for (int i = index; i < curLength - 1; i++)
listElem[i] = listElem[i + 1];
curLength--;
}
@Override
public int indexOf(Object obj) {
int j = 0;
while (j < curLength && !listElem[j].equals(obj))
j++;
if (j < curLength) return j;
else return -1;
}
}
ArrayList
此时代码完成了部分java中的ArrayList的功能,ArrayList即官方的线性表,需注意,为避免内存的浪费,上述线性表的长度没有初始长度,如有初始长度,需要考虑扩容。
看源码知
- 初始容量 10
private static final int DEFAULT_CAPACITY = 10;
- 跨容算法,1.5倍扩容;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
// ***重点行
int newCapacity = oldCapacity + (oldCapacity >> 1);
- 加入泛形;
链式实现
用任意的存储单元来存储线性表中的数据元素-->链式线性表
链表-->任意存储单元
及链表:
LinkedList
接口同上述List接口;
public class Link implements List {
private Node head;
private int size;
public Link() {
this.size = 0;
this.head = new Node(null);
}
private class Node {
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
}
}
@Override
public void clear() {
head = null;
size = 0;
}
@Override
public Boolean isEmpty() {
return size == 0;
}
@Override
public int length() {
return size;
}
@Override
public Object get(int index) throws Exception {
Node indexNode = head;
if (index < 0 || index > size - 1)
throw new Exception("out of index exception");
while (index != 0) {
indexNode = indexNode.next;
index--;
}
return indexNode.data;
}
@Override
public void insert(Object obj, int index) throws Exception {
if (index < 0 || index > size - 1)
throw new Exception("out of index exception");
Node insertNode = new Node(obj);
Node indexNode = head;
while (index != 0) {
indexNode = indexNode.next;
index--;
}
insertNode.next = indexNode.next;
indexNode.next = insertNode;
size++;
}
@Override
public void remove(int index) throws Exception {
if (index == 0)
head = head.next;
else {
Node indexNode = head;
while (index != 1) {
indexNode = indexNode.next;
index--;
}
indexNode.next = indexNode.next.next;
}
size = size - (size == 0 ? 0 : 1);
}
@Override
public int indexOf(Object obj) {
int j = 0;
Node indexNode = head;
while (j < size || !indexNode.data.equals(obj)) {
j++;
indexNode = indexNode.next;
}
if (j < size)
return j;
else
return -1;
}
}
及java中的LinkedList;