*ListNode 比单向多一个pre指针
*部分方法细节修改
package LeetcodeExercise;
public class DoubleLinkedListTest {
public static void main(String[] args) {
}
}
class DoubleLinkedList{
private ListNode2 head = new ListNode2(0," ");//head不能动
public ListNode2 getHead() {
return head;
}
//遍历
public void list(){
if(isEmpty()){
return;
}
ListNode2 temp = head.next;
while (true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
public boolean isEmpty(){
if(head.next == null){
System.out.println("空链表");
return true;
}
return false;
}
public void insertNodeTail(ListNode2 listNode){
ListNode2 temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = listNode;
listNode.pre = temp;//和单链表不同
}
public void updateNode(ListNode2 listNode){
if(isEmpty()){
return;
}
ListNode2 temp = head.next;
boolean flag = false;
while (true){
if(temp == null) {
break;
}
if(temp.no == listNode.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.value = listNode.value;
}else {
System.out.println("no listnode.no like this!");
}
}
public void deleteNode(int no){ //和单链表不同
if(isEmpty()){
return;
}
ListNode2 temp = head.next; //是需要删除的结点
boolean flag = false;
while (true){
if(temp == null) {
break;
}
if(temp.no == no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.pre.next = temp.next;
if(temp.next != null) {
temp.next.pre = temp.pre;
}
}else {
System.out.println("no listnode.no like this!");
}
}
}
class ListNode2{
int no;
String value;
ListNode2 next;
ListNode2 pre;
public ListNode2(int no, String value) {
this.no = no;
this.value = value;
}
@Override
public String toString() {
return "ListNode{" +
"no=" + no +
", value=" + value +
'}';
}
}