java实现双向链表。
package 双向链表;
public class LinkedList<E> {
private class Node<E>{
private Node<E> previ; //上一个节点
private E data; //当前节点存储的数据
private Node<E> next; //下一个节点
//constructor
public Node(Node<E> previ,E data,Node<E> next) {
this.previ = previ;
this.data = data;
this.next = next;
}
public Node(Node<E> previ,E data) {
this(previ,data,null);
}
public Node(E data,Node<E> next) {
this(null,data,next);
}
//setter and getter
public void setPrevi(Node<E> previ) {
this.previ = previ;
}
public void setData(E data) {
this.data = data;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node getPrevi() {
return previ;
}
public E getData() {
return data;
}
public Node getNext() {
return next;
}
@Override
public String toString() {
return data.toString();
}
}
private Node<E> first; //头节点(头指针)
private int size; //有效节点数量(已存储数据的节点)
private Node<E> last; //尾节点(尾指针)
//constructor
public LinkedList() { //构造一个空的双向链表
this.first = null;
this.size = 0;
this.last = null;
}
//获取链表节点数量
public int size() {
return size;
}
//判断链表是否为空?
public boolean isEmpty() {
return size() == 0;
}
//获取索引的指针位置
public Node getPointer(int index) {
Node<E> tmp;
if(checkIndex(index)) {
if(index == 0) {
return first;
}else if(index == size - 1){
return last;
}else if(index == size) {
return null;
}else {
if(index <= size / 2) {
for(tmp = first;index > 0;tmp = tmp.getNext()) {
index--;
}
return tmp;
}else {
if(size % 2 != 0) {
for(tmp = last;index < size - 1;tmp = tmp.getPrevi()) {
index++;
}
return tmp;
}else {
for(tmp = last;index < size;tmp = tmp.getPrevi()) {
return tmp;
}
}
}
}
}
return null;
}
//检测索引区间
public boolean checkIndex(int index) {
if(index < 0 || index > size) {
return false;
}
return true;
}
//insert addFirst addLast
public boolean insert(int index,E data) {
if(checkIndex(index)) {
if(index == 0) return addFirst(data);
if(index == size) return addLast(data);
Node<E> tmp = getPointer(index);
if(tmp != null) {
Node<E> n = new Node<>(tmp.getPrevi(),data,tmp);
tmp.getPrevi().setNext(n);;
tmp.setPrevi(n);
size++;
return true;
}
}
return false;
}
public boolean addFirst(E data) {
if(first == null) {
first = new Node<>(data,null);
size++;
return true;
}else {
return addLast(data);
}
}
public boolean addLast(E data) {
if(last != null) {
last = new Node<>(last,data);
size++;
return true;
}else {
if(isEmpty()) {
return addFirst(data);
}else {
last = new Node<>(first,data);
size++;
return true;
}
}
}
//remove removeFirst removeLast
public boolean remove(int index) {
if(checkIndex(index)) {
if(index == 0) return removeFirst();
if(index == size - 1) return removeLast();
if(index == size) {
return false;
}else {
Node<E> tmp = getPointer(index - 1);
tmp.setNext(tmp.getNext().getNext());
size--;
return true;
}
}
return false;
}
public boolean removeFirst() {
if(first != null) {
first = first.getNext();
size--;
return true;
}else {
return false;
}
}
public boolean removeLast() {
if(last != null) {
last = last.getPrevi();
size--;
return true;
}else {
return false;
}
}
//update updateFirst updateLast
public boolean update(int index,E newData) {
if(checkIndex(index)) {
if(index == 0) return updateFirst(newData);
if(index == size - 1) return updateLast(newData);
Node<E> tmp = getPointer(index);
if(tmp != null) {
tmp.setData(newData);
return true;
}
}
return false;
}
public boolean updateFirst(E newData) {
if(first != null) {
first.setData(newData);
return true;
}
return false;
}
public boolean updateLast(E newData) {
if(last != null) {
last.setData(newData);
return true;
}
return false;
}
//select selectFirst selectLast
public E select(int index) {
if(checkIndex(index)) {
if(index == 0) return selectFirst();
if(index == size - 1) return selectLast();
Node<E> tmp = getPointer(index);
if(tmp != null) {
return tmp.getData();
}
}
return null;
}
public E selectFirst() {
if(first != null) return first.getData();
return null;
}
public E selectLast() {
if(last != null) return last.getData();
return null;
}
}