Linked List也是一種特殊的線性表, 其由一系列的節點組成, 節點的順序是通過節點元素中的指標連接次序來確定的. Linked List中的節點包含兩個部分, 一個是其自身需存放的資料, 另一個是指向下一個節點的參照(reference).
Linked List v.s. Array
package idv.carl.datastructures.list;
/**
* @author Carl Lu
*/
public class LinkedList {
private LinkedNode head;
private int size = 0;
public void insertHead(int id) {
LinkedNode newNode = new LinkedNode(id);
newNode.setNext(head);
head = newNode;
size++;
}
public void insertTail(int id) {
LinkedNode newNode = new LinkedNode(id);
if (head == null) {
head = newNode;
size++;
return;
}
LinkedNode tail = head;
while (tail.getNext() != null) {
tail = tail.getNext();
}
tail.setNext(newNode);
size++;
}
public LinkedNode removeHead() {
if (size == 0) {
return null;
}
LinkedNode tmp = head;
head = head.getNext();
size--;
return tmp;
}
public LinkedNode find(int id) {
LinkedNode node = head;
while (node.getId() != id) {
if (node.getNext() == null) {
return null;
} else {
node = node.getNext();
}
}
return node;
}
public LinkedNode remove(int id) {
if (size == 0) {
return null;
}
LinkedNode deleted = head;
LinkedNode previous = head;
// To search the deleted node and it's previous node
while (deleted.getId() != id) {
if (deleted.getNext() == null) {
return null;
} else {
previous = deleted;
deleted = deleted.getNext();
}
}
// Reset the relationship of nodes
if (deleted.equals(head)) {
head = head.getNext();
} else {
previous.setNext(deleted.getNext());
}
size--;
return deleted;
}
public void displayList() {
LinkedNode tmp = head;
while (tmp != null) {
System.out.println(tmp.toString());
tmp = tmp.getNext();
}
}
public int getSize() {
return size;
}
}