自己實現簡單的linkedlist 沒有叠代器
阿新 • • 發佈:2018-08-18
節點 prev 簡單的 成員 自己 包含 clear lec 自己實現
package demo;
import java.util.Collection;
public class MyLinkedList<E> {
// 屬性
// 構造方法
public MyLinkedList() {
}
public MyLinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
// 構建雙向鏈表,首先要構建節點
// 建立成員內部類節點,
private static class Node<E> {
E item; // 存儲數據
Node<E> next;// 下一個節點
Node<E> prev; // 前一個節點
public Node(Node<E> next, E item, Node<E> prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
}
// 一個數組域存放數組,一個prev指向前一個節點的next 一個next指向下一個節點的prev.
// 第一個節點
private Node<E> first;
// 最後一個節點
private Node<E> last;
// 數組個數
private int size;
// 將指定元素追加到此列表的末尾
public boolean add(E e) {
addLast(e);
return true;
}
// 將節點連接到此列表的末尾
void addLast(E e) {
Node<E> l = last; // 首先找到最後一個元素
Node<E> node = new Node<E>(null, e, l);// 根據傳入的e創建一個新的節點next指向null
// item存傳入的e prev指向l;
last = node; // 將剛剛新創建的節點node變為最後一個節點
if (l == null) { // 如果最後一個節點null
first = node; // 第一個節點就為node
} else {
l.next = node; // 將前一個節點的next指向下一個節點
}
size++; // size+1
}
public void addfirst(E e) {
linkfirst(e);
}
// 在此列表中的指定位置插入指定的元素
public void add(int index, E e) {
checkRange(index);// 首先判斷範圍
if (index == size) { // 如果插入的在最後一個就調用插入最後一個的方法
addLast(e);
} else { // 否則取到index位置對應的node元素,然後替換
Node<E> l = index(index);
addBeforeNode(e, l);
}
}
// 在指定元素前插入元素
private void addBeforeNode(E e, Node<E> l) {
Node<E> preNode = l.prev;
Node<E> newnode = new Node<>(l, e, preNode);
if (preNode == null) {
first = newnode;
} else {
preNode.next = newnode;
}
l.prev = newnode;
size++;
}
// 查找index出的元素
private Node<E> index(int index) {// 采用二分法查找
if (index < (size / 2)) { // 首先判斷index在前一半還是後一半的範圍,然後找到index出的元素
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
void linkfirst(E e) {
Node<E> l = first; // 首先找到第一個一個元素
Node<E> node = new Node<E>(l, e, null);// 根據傳入的e創建一個新的節點next指向null
// item存傳入的e prev指向l;
first = node; // 將剛剛新創建的節點node變為最後一個節點
if (l == null) { // 如果第一個節點null
first = node; // 第一個節點就為node
} else {
l.prev = node; // 將前一個節點的next指向下一個節點
}
size++; // size+1
}
// 刪除所有的元素
public void clear() {
for (Node<E> x = first; x != null;) {
Node<E> next = x.next;
x.item = null;
x.prev = null;
x.next = null;
x = next;
}
first = last = null;
size = 0;
}
// 判斷是否越界
private void checkRange(int index) {
if (index < 0 && index > size)
throw new IndexOutOfBoundsException("越界了");
}
// 返回列表中指定位置的元素
public E get(int index) {
checkRange(index);
return index(index).item;
}
//
private void addAll(Collection<? extends E> c) {
// TODO Auto-generated method stub
}
// 返回此列表中指定元素的第一次出現的索引,如果此列表不包含元素,則返回-1。
public int indexOf(Object o) {
Node<E> n = first;
int c = 0;
while (n != null) {
if (o != null) {
if (o.equals(n.item))
return c;
} else {
if (n.item == null) {
return c;
}
}
c++;
n = n.next;
}
return -1;
}
//刪除指定節點
public E remove (int index){
checkRange(index);
return unlink(index);
}
//刪除指定對象
public boolean remove(Object o){
int index = indexOf(o);
if(index<0){
return false;
}
unlink(index);
return true;
}
//刪除index對應的節點的連接
private E unlink(int index){
Node<E> l = index(index);
E item = l.item;
Node<E> preNode = l.prev;
Node<E> nextNode = l.next;
if(preNode==null){
first = nextNode;
}else{
preNode.next=nextNode;
l.next=null;
}
if(nextNode==null){
last = preNode;
}else{
nextNode.prev = preNode;
l.prev=null;
}
size--;
l.item=null;
return item;
}
}
LinkedList是基於雙向鏈表實現的,
LinkedList和ArrayList的區別:
ArrayList基於數組實現,因此具有數組的特點:有序.元素可重復.插入慢.查找快
LinkedList 基於雙向鏈表實現, 因此具有鏈表特帶你 插入快、 查找慢的特性;
自己實現簡單的linkedlist 沒有叠代器