class linkedList {
public int val;
public linkedList prev;
public linkedList next;
public linkedList(int val) {
this.val = val;
}
}
public class DoublelinkedList {
public linkedList head;
public linkedList last;
//return the length of the DoublelinkedList
public int size() {
linkedList cur = this.head;
int count = 0;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//Print the value of every node in the DoublelinkedList
public void display() {
linkedList cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//Check whether the DoublelinkedList contains the given value
public boolean containsValue(int val) {
linkedList cur = this.head;
while (cur != null) {
if (cur.val == val) {
return true;
}
cur = cur.next;
}
return false;
}
//Push a node at the first of the DoublelinkedList
public void push_font(int val) {
linkedList node = new linkedList(val);
if (head == null) {
this.head = node;
this.last = node;
} else {
this.head.prev = node;
node.next = this.head;
this.head = node;
}
}
//Push a node at the last of the DoublelinkedList
public void push_back(int val) {
linkedList node = new linkedList(val);
if (this.last == null) {
this.head = node;
this.last = node;
} else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
//Pop the last node of the DoublelinkedList
public void pop_back() {
if (this.head == null) {
System.out.println("链表为空!无法删除!");
} else {
int lastValue = this.last.val;
if (this.head == this.last) {
this.head = null;
this.last = null;
} else {
linkedList tmp = this.last.prev;
this.last.prev.next = null;
this.last.prev = null;
this.last = tmp;
}
}
}
//Pop the first node of the DoublelinkedList
public void pop_font() {
if (this.head == null) {
System.out.println("链表为空!无法删除!");
} else {
int firstValue = this.head.val;
if (this.head == this.last) {
this.head = null;
this.last = null;
} else {
linkedList tmp = this.head.next;
this.head.next.prev = null;
this.head.next = null;
this.head = tmp;
}
}
}
//Return the value of the node at the specific position
public int searchByIndex(int index) {
if (this.head == null) {
System.out.println("链表为空!");
return -1;
}
if (index < 0 || index >= this.size()) {
System.out.println("index 位置不合法!");
return -1;
} else {
linkedList cur = this.head;
while (index > 0) {
cur = cur.next;
index--;
}
return cur.val;
}
}
//Insert a node at the DoublelinkedList
public void Insert(int index, int val) {
if (index < 0 || index > this.size()) {
System.out.println("index 位置不合法!");
return;
}
linkedList node = new linkedList(val);
linkedList cur = this.head;
if (index == 0) {
this.push_font(val);
} else if (index == this.size() - 1) {
this.push_back(val);
} else {
while (index > 0) {
cur = cur.next;
index--;
}
node.next = cur.next;
cur.next.prev = node;
node.prev = cur;
cur.next = node;
}
}
//Remove the first node with the specific value of the DoublelinkedList
public void removeByValue(int val) {
linkedList cur = this.head;
while (cur != null) {
if (cur.val == val) {
if (cur == head) {
this.head = this.head.next;
if (this.head != null) {
this.head.prev = null;
} else {
this.last = null;
}
return;
} else {
cur.prev.next = cur.next;
if (cur == this.last) {
this.last = cur.prev;
} else {
cur.next.prev = cur.prev;
}
return;
}
} else {
cur = cur.next;
}
}
}
//Remove all nodes with the specific value of the DoublelinkedList
public void removeAllByValue(int val) {
linkedList cur = this.head;
while (cur != null) {
if (cur.val == val) {
if (cur == head) {
this.head = this.head.next;
if (this.head != null) {
this.head.prev = null;
} else {
this.last = null;
}
cur = cur.next;
} else {
cur.prev.next = cur.next;
if (cur == this.last) {
this.last = cur.prev;
this.last.next = null;
} else {
cur.next.prev = cur.prev;
}
cur = cur.next;
}
} else {
cur = cur.next;
}
}
}
//Clear the DoublelinkedList
public void clear() {
linkedList curNext = this.head;
while (head != null) {
curNext = curNext.next;
this.head.next = null;
this.head.prev = null;
this.head = curNext;
}
}
}