import java.util.NoSuchElementException;
public class DoublyLinkedListImpl {
private Node head;// sentinel before first item
private Node tail;// sentinel after last item
private int size;// number of elements on list
public DoublyLinkedListImpl() {
size = 0;
}
/**
* this class keeps track of each element information
*
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
/**
* returns the size of the linked list
*
* @return
*/
public int size() {
return size;
}
/**
* return whether the list is empty or not
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}
/**
* adds element at the starting of the linked list
*
* @param element
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if (head != null) {
head.prev = tmp;
}// 跟原来的头节点交换位置
head = tmp;
if (tail == null) {
tail = tmp;
}// 首次添加节点的时候,头节点就是尾节点
size++;
System.out.println("adding: " + element);
}
/**
* adds element at the end of the linked list
*
* @param element
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if (tail != null) {
tail.next = tmp;
}// 跟原来的尾节点交换位置
tail = tmp;
if (head == null) {
head = tmp;
}// 首次添加节点的时候,头节点就是尾节点
size++;
System.out.println("adding: " + element);
}
/**
* get element at the specified location of the linked list
*
* @param loc
*/
public Node getElement(int loc) {
Node tmp = head;
int index = 0;
while (loc != index) {
tmp = tmp.next;
index++;
}
return tmp;
}
/**
* add element at the specified location of the linked list
*
* @param element
* @param loc
*/
public void insertAfterElement(E element, int loc) {
if (loc == size) {
addLast(element);
}
if (loc == 0) {
addFirst(element);
}
Node preEle = getElement(loc);
Node nextEle = preEle.next;
Node tmp = new Node(element, null, preEle);
// preEle=tmp.prev;//这一行没必要,因为创建类的时候已经指定preEle
preEle.next = tmp;
nextEle.prev = tmp;
tmp.next = nextEle;
size++;
System.out.println("inserting: " + element);
}
/**
* this method walks forward through the linked list
*/
public void iterateForward() {
System.out.println("iterating forward..");
Node tmp = head;
while (tmp != null) {
System.out.println(tmp.element);
tmp = tmp.next;
}
}
/**
* this method walks backward through the linked list
*/
public void iterateBackward() {
System.out.println("iterating backword..");
Node tmp = tail;
while (tmp != null) {
System.out.println(tmp.element);
tmp = tmp.prev;
}
}
/**
* this method removes element from the start of the linked list
*
* @return
*/
public E removeFirst() {
if (size == 0)
throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
}
/**
* this method removes element from the end of the linked list
*
* @return
*/
public E removeLast() {
if (size == 0)
throw new NoSuchElementException();
Node tmp = tail;
tail = tail.prev;
tail.next = null;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
}
/**
* removes element from the specified location of the linked list
*
* @return
*/
public E removeByIndex(int loc) {
Node tmp = null;
if (loc >= size || loc < 0) {
throw new NoSuchElementException();
} else if (loc == 0) {
removeFirst();
} else if (loc == size - 1) {
removeLast();
} else {
tmp = getElement(loc);
Node preEle = tmp.prev;
Node nextEle = tmp.next;
preEle.next = nextEle;
nextEle.prev = preEle;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
}
return null;
}
public static void main(String a[]) {
DoublyLinkedListImpl dll = new DoublyLinkedListImpl();
dll.addFirst(10);
dll.addFirst(34);
dll.addLast(56);
dll.addLast(364);
dll.insertAfterElement(88, 2);
dll.insertAfterElement(99, 3);
dll.iterateForward();
dll.removeByIndex(4);
dll.iterateBackward();
}
}