模拟实现LinkedList双向链表,加深自己的理解
public class LinkedList<E> {
public static class Node<E>{
E value;
Node<E> prev;
Node<E> next;
public Node(E value) {
this.value = value;
}
}
Node<E> head;
Node<E> tail;
int size = 0;
public LinkedList(){
}
//头插法
public void addFirst(E e){
Node<E> node = new Node<>(e);
if (null == head){
tail = node;
}else{
head.prev = node;
node.next = head;
}
head = node;
size++;
}
//尾插法
public void addLast(E e){
Node<E> node = new Node<>(e);
if (null == head) {
head = node;
}else{
tail.next = node;
node.prev = tail;
}
tail = node;
size++;
}
//插入任意位置
public void add(E e,int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("下标越界");
if (size == index){
addLast(e);
}else if (0 == index){
addFirst(e);
}else{
Node<E> cur = null;
if (index <= (size >> 1)) {
cur = head;
while (0 < index--) {
cur = cur.next;
}
} else if (index > (size >> 1)) {
cur = tail;
while (size > ++index) {
cur = cur.prev;
}
}
Node<E> node = new Node<>(e);
node.prev = cur.prev;
node.next = cur;
node.prev.next = node;
cur.prev = node;
size++;
}
}
//头删
public void removeFirst(){
if (null == head){
return;
}else if (head == tail){
head = null;
tail = null;
}else{
head = head.next;
head.prev = null;
}
size--;
}
//尾删
private void removeLast() {
if (null == head){
return;
}else if (head == tail){
head = null;
tail = null;
}else{
tail = tail.prev;
tail.next = null;
}
size--;
}
//删除任意元素
public void remove(E e){
if (null == head){
return;
}
Node<E> cur = head;
//Node<E> next = cur.next;
while(null != cur){
if ((e == cur.value) && (null == cur.prev)){
removeFirst();
return;
}else if ((e == cur.value) && (null == cur.next)){
removeLast();
return;
}else if (e == cur.value){
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
return;
}
cur = cur.next;
}
}
//删除所有值为e的元素
public void removeAll(E e){
if (null == head){
return;
}
Node<E> cur = head;
//Node<E> next = cur.next;
while(null != cur){
if ((e == cur.value) && (null == cur.prev)){
removeFirst();
}else if ((e == cur.value) && (null == cur.next)){
removeLast();
}else if (e == cur.value){
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
cur = cur.next;
}
}
//查找是否包含某一关键字
public boolean contains(E e){
if (null == head){
return false;
}
Node<E> cur = head;
while(null != cur){
if(e == cur.value)
return true;
cur = cur.next;
}
return false;
}
//查找第一次遇到某一元素的位置
public int indexOf(E e){
if (null == head){
return -1;
}
Node<E> cur = head;
int index = 0;
while(null != cur){
if (e == cur.value)
return index;
cur = cur.next;
index++;
}
return -1;
}
public int size(){
return size;
}
public boolean isEmpty(){
return null == head;
}
@Override
public String toString() {
String str = "{";
if (null == head){
str += "}";
return str;
}
Node<E> cur = head;
while(null != cur.next){
str += cur.value;
str += "⇄";
cur = cur.next;
}
str += cur.value;
str += "}";
return str;
}
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.addFirst(1);
linkedList.addFirst(2);
linkedList.addFirst(3);
linkedList.addFirst(4);
System.out.println("依次头插1,2,3,4:");
System.out.println(linkedList);
System.out.println("依次尾插5,6,7,8:");
linkedList.addLast(5);
linkedList.addLast(6);
linkedList.addLast(7);
linkedList.addLast(8);
System.out.println(linkedList);
System.out.println("在位置0插入10:");
linkedList.add(10,0);
System.out.println(linkedList);
System.out.println("在位置5插入11:");
linkedList.add(11,5);
System.out.println(linkedList);
System.out.println("在位置10插入12:");
linkedList.add(12,10);
System.out.println(linkedList);
System.out.println("删除头结点:");
linkedList.removeFirst();
System.out.println(linkedList);
System.out.println("删除尾结点:");
linkedList.removeLast();
System.out.println(linkedList);
System.out.print("是否包含5:");
System.out.println(linkedList.contains(5));
System.out.print("元素2的位置:");
System.out.println(linkedList.indexOf(3));
System.out.println("删除元素5:");
linkedList.remove(5);
System.out.println(linkedList);
linkedList.addFirst(1);
linkedList.addFirst(1);
linkedList.addLast(1);
System.out.println(linkedList);
System.out.println("删除所有元素1:");
linkedList.removeAll(1);
System.out.println(linkedList);
}
}