线性表的存储分为:顺序存储和链式存储
顺序存储的一种实现:ArrayList,数据量大的情况下,查找的效率高,删除和新增的效率低
链式存储的实现:LinkedList,数据量大的情况下,查找的效率低,删除和新增的效率低
下面给出简单的实现:
ArrayList:
package ds.list;
/**
* @author : cuantianhou 2019/12/19
*/
public class MockArrayList<T extends Comparable<T>> {
private Object[] elements;
private int size;
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
private int capacity;
private static final int MAX_CAPACITY = 1 << 30;
/**
* 无参构造函数
*/
public MockArrayList(){
elements = new Object[DEFAULT_INITIAL_CAPACITY];
capacity = DEFAULT_INITIAL_CAPACITY;
size = 0;
}
/**
* 有参构造函数
* @param initialCapacity
*/
public MockArrayList(int initialCapacity){
elements = new Object[initialCapacity];
capacity = initialCapacity;
size = 0;
}
/**
* 添加元素
* @param e
*/
public void add(T e){
ensureNewCapacity(size+1);
elements[size++] = e;
}
/**
* 指定位置添加元素
* @param index
* @param e
*/
public void add(int index,T e){
if (index < 0 || index > size){
throw new IndexOutOfBoundsException(index+"");
}
ensureNewCapacity(size+1);
System.arraycopy(elements,index,elements,index+1,size - index);
elements[index] = e;
size++;
}
/**
* 添加列表
* @param arrayList
*/
public void addAll(MockArrayList arrayList){
if (arrayList == null) {
return;
}
addAll(arrayList.elements);
}
public void addAll(Object[] arrayList){
if (arrayList == null)
return;
int len = arrayList.length;
ensureNewCapacity(size+len);
System.arraycopy(arrayList,0,elements,size,len);
size += len;
}
/**
* 自动扩容,每次扩容都扩大到原来的1.5倍
* @param newCapacity
*/
private void ensureNewCapacity(int newCapacity){
if (size <= 0) return;
if (newCapacity > MAX_CAPACITY){
System.out.println("数组大小已达到最大值,无法再插入数据");
return;
}
if (newCapacity <= capacity)
return;
int nc = capacity + capacity >> 1;
if (nc < newCapacity)
nc = newCapacity;
Object[] ne = new Object[nc];
System.arraycopy(elements,0,ne,0,size);
capacity = nc;
elements = ne;
}
/**
* 获取列表大小
* @return
*/
public int size(){
return size;
}
}
上述代码的精髓就是: System.arraycopy(elements,0,ne,0,size);
LinkedList:基于双链表实现的
package ds.list;
/**
* @author : cuantianhou 2019/12/19
*/
public class MockLinkedList<T extends Comparable<T> >{
Node<T> head;
Node<T> tail;
int capacity ;
public MockLinkedList(){
capacity = 0;
}
/**
* 头插法
*/
private void linkHead(T data){
Node node = new Node(data,null,head);
if(head == null){
tail = node;
}else{
head.pre = node;
}
head = node;
}
/**
* 尾插法
*/
private void linkTail(T data){
Node node = new Node(data,tail,null);
if(tail == null){
head = node;
}else{
tail.next = node;
}
tail = node;
}
/**
* 删除头部
*/
private void unLinkHead(){
Node next = head.next;
Node temp = head;
head = next;
temp.next = null;
head.pre = null;
}
/**
* 删除尾部
*/
public void unLinkTail(){
Node pre = tail.pre;
Node temp = tail;
tail = pre;
temp.pre = null;
tail.next = null;
}
/**
* 删除节点信息
*/
public boolean unLink(Node node){
if(node == null) {
return false;
}
if(node == head){
unLinkHead();
capacity--;
return true;
}
if(node == tail){
unLinkTail();
capacity--;
return true;
}
Node pre = node.pre;
Node next = node.next;
pre.next = next;
next.pre = pre;
node.pre = null;
node.next = null;
capacity--;
return true;
}
/**
* 获取节点信息 索引
*/
private Node getNode(int index){
if(index < 0 || index > capacity){
throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
}
int mid = (capacity-1)>>1;
Node resultNode;
/**
* 遍历后面的数据
*/
if(index >= mid) {
resultNode = tail;
for (int i = capacity - 1; i > index; i--) {
resultNode = resultNode.pre;
}
return resultNode;
}
/**
* 遍历前面的数据
*/
resultNode = head;
for (int i = 0; i < index; i++) {
resultNode = resultNode.next;
}
return resultNode;
}
/**
* 获取节点 元素
* @param data
* @return
*/
private Node getNode(T data){
Node node = head;
while(node != null){
if(node.data.compareTo(data) == 0){
return node;
}
node = node.next;
}
return null;
}
/**
* 判断是否包含某一个元素
* @param data
* @return
*/
public boolean contains(T data){
return getNode(data) == null? Boolean.FALSE : Boolean.TRUE;
}
/**
* 获取头结点
* @return
*/
public Node getHead(){
return head;
}
/**
* 获取尾结点
* @return
*/
public Node getTail(){
return tail;
}
/**
* 删除元素
* @param e
* @return
*/
public T remove(T e){
Node<T> node = getNode(e);
return unLink(node) ? node.data:null;
}
/**
* 尾插法
* @param e
* @return
*/
public boolean addTail( T e){
linkTail(e);
capacity++;
return true;
}
/**
* 头插法
* @param e
* @return
*/
public boolean addHead(T e){
linkHead(e);
capacity++;
return true;
}
/**
* 插入方法
* @param e 元素
* @return
*/
public boolean add(T e){
return addTail(e);
}
/**
*
* 重写ToString方法
*/
@Override
public String toString(){
if(head == null){
return "[]";
}
StringBuilder stringBuilder = new StringBuilder("[");
Node node = head;
while(node != null){
stringBuilder.append(node.data).append(", ");
node = node.next;
}
return stringBuilder.substring(0,stringBuilder.length()-2)+"]";
}
/**
* 节点信息
*/
private final static class Node<T extends Comparable<T>>{
/**
* 数据
*/
T data;
/**
* 前驱
*/
Node pre;
/**
* 后继
*/
Node next;
/**
* 构造函数
* @param data 数据
* @param pre 前驱
* @param next 后继
*/
public Node(T data,Node pre, Node next){
this.data = data;
this.pre = pre;
this.next = next;
}
}
}