您好,登錄后才能下訂單哦!
LinkedList
使用場景:
如果僅在兩端操作數據,用LinkedList
數據量較小時(100以內),頻繁增刪數據用LinkedList
雙向鏈表:兩端效率高,越往中間效率越低
package 集合.list.LinkedList;
public class MyLinkedList {
//默認長度為0
private int size = 0;
Node head = null;
Node tail = null;
public MyLinkedList() {
}
//添加元素的方法
public void add(Object obj) {
//創建Node
Node node = new Node(obj, null, null);
//先判斷之前的尾部節點
if (size == 0) {
this.head = node;
} else {
Node temp;
temp = this.tail;
temp.next = node;
node.prev = temp;
}
this.tail = node;
size++;
}
//插入元素
public void add(int index, Object obj) {
//先判斷索引是否合法
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
//判斷插入位置
if (index == size) {
this.add(obj);
return;
}
//創建Node
Node node = new Node(obj, null, null);
Node temp = null;
//如果插入是頭部
if (index == 0) {
temp = this.head;
this.head = node;
node.next = temp;
temp.prev = node;
} else {
// 如果插入中間位置
Node insertNode = indexOf(index);
Node prev = insertNode.prev;
node.prev = prev;
node.next = insertNode;
prev.next = node;
insertNode.prev = node;
}
size++;
}
//刪除指定索引元素
public void remove(int index) {
Node n;
if (index == 0) {
n = head.next;
head = n;
n.prev = null;
} else if (index == size - 1) {
n = tail.prev;
tail = n;
n.next = null;
} else {
Node node = indexOf(index);
Node prev = node.prev;
Node next = node.next;
prev.next = next;
next.prev = prev;
// node.prev = null;
//node.next = null;
}
size--;
}
//刪除第一個出現的元素
public void remove(Object obj) {
int index = indexOf(obj);
if (index != -1) {
remove(index);
}
}
//查找元素返回索引
public int indexOf(Object obj) {
//獲取頭節點
Node node = head;
for (int i = 0; i < size; i++) {
if (node.value == obj || node.value != null && node.value.equals(obj)) {
return i;
}
node = node.next;
}
return -1;
}
//查找指定索引的元素
public Node indexOf(int index) {
if (index == 0) {
return head;
}
if (index == size - 1) {
return tail;
}
Node n;
if (index <= size / 2) {
//前一半
n = head;
for (int i = 1; i < index; i++) {
n = n.next;
}
} else {
n = tail;
for (int i = size - 2; i >= index; i--) {
n = n.prev;
}
}
return n;
}
//判斷是否包含元素
public boolean cotains(Object obj) {
return indexOf(obj) != -1;
}
//獲取長度
public int getSize() {
return size;
}
//判斷是否為空
public boolean isEmpty() {
return size == 0;
}
//清空鏈表
public void clear() {
//設置頭尾節點為null,size=0
this.tail = null;
this.head = null;
size = 0;
}
//通過索引修改數據
public void set(int index, Object obj) {
//先判斷索引是否合法
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
indexOf(index).value = obj;
}
//獲取子鏈表
public MyLinkedList sublist(int fromIndex, int toIndex) {
//判斷越界
//先判斷索引是否合法
if (toIndex > size || fromIndex < 0) {
throw new IndexOutOfBoundsException();
}
MyLinkedList sublist = new MyLinkedList();
//先獲取fromIndex處的節點
Node node = indexOf(fromIndex);
for (int i = fromIndex; i < toIndex; i++) {
//將對于節點處的值加入到子鏈表中
sublist.add(node.value);
//每次循環需要改變節點
node = node.next;
}
return sublist;
}
//重寫toString
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
//獲取節點
Node node = head;
for (int i = 0; i < size; i++) {
if (i != size - 1) {
sb.append(node.value).append(",");
} else {
sb.append(node.value);
}
node = node.next;
}
sb.append("]");
return sb.toString();
}
//定義節點內部靜態類
private static class Node {
//節點的數據
Object value;
//節點的下一個地址
Node next;
//上一個地址
Node prev;
Node(Object value, Node prev, Node next) {
this.value = value;
this.next = next;
this.prev = prev;
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。