public class DoubleLinkedList {
//头插法
public void addFirst(int data){ }
//尾插法
public void addLast(int data){ }
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data){ }
//查找是否包含关键字key是否在单链表当中
public boolean contain(int key){ }
//删除第一次出现关键字为key的节点
public void remove(int key){ }
//删除所有值为key的节点
public void removeAll(int key){ }
//得到单链表的长度
public int size(){ }
public void show(){ }
public void clear(){ }
}
class Node{
public int val;
public Node pre;
public Node next;
public Node(int val){
this.val=val;
}
}
public class DoubleLinkedList {
public Node head;
public Node last; //指向末尾
public void addFirst(int val) {
Node node = new Node(val);
if (this.head == null) {
this.head = node;
this.last = node;
} else {
node.next = this.head;
this.head.pre = node;
this.head = node;
}
}
public void addLast(int val) {
Node node = new Node(val);
if (this.head == null) {
this.head = node;
this.last = node;
} else {
this.last.next = node;
node.pre = this.last;
this.last = node;
}
}
public boolean contain(int key) {
Node cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
public void show() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public int size() {
Node cur = this.head;
int count = 0;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
public Node searchIndex(int index) {
Node cur = this.head;
if (index < 0 && index > size()) {
System.out.println("index位置不合法");
return null;
}
int count = 0;
while (index != 0) {
cur = cur.next;
index--;
}
return cur;
}
public void addIndex(int index, int val) {
if (index < 0 || index > size()) {
System.out.println("index不合法");
return;
} else if (index == 0) {
addFirst(val);
} else if (index == size()) {
addLast(val);
} else {
Node node = new Node(val);
Node cur = searchIndex(index);
node.next = cur;
cur.pre.next = node;
node.pre = cur.pre;
cur.pre = node;
}
}
public void remove(int key) {
Node cur = this.head;
while (cur != null) {
if (cur.val == key) {
if (this.head.val == key) { //删除头节点
this.head = this.head.next;
this.head.pre = null;
} else {
cur.pre.next = cur.next;
if (cur.next != null) {
cur.next.pre = cur.pre; //当前的cur不是最后一个节点
} else {
this.last = this.last.pre; //尾节点
}
}
return;
}
cur = cur.next;
}
}
public void removeAll(int key) {
Node cur = this.head;
while (cur != null) {
if (cur.val == key) {
if (this.head.val == key) {
if(this.head.next == null) {
this.head = null;
return;
}
this.head = this.head.next;//删除头节点
this.head.pre = null;
} else {
cur.pre.next = cur.next;
if (cur.next != null) {
cur.next.pre = cur.pre;
} else {
this.last = this.last.pre; //尾节点
}
}
}
cur = cur.next;
}
}
public void clear(){
this.last = null;
this.head = null;
}
}
public class TestDemo {
public static void main(String[] args) {
DoubleLinkedList list=new DoubleLinkedList();
list.addLast(1);
list.addFirst(2);
list.addFirst(3);
list.show();
list.addIndex(1,1);
list.addIndex(2,4);
list.show();
list.remove(4);
list.show();
list.removeAll(1);
list.show();
}
}