双向链表模板
class DoubleLinkedList{
private Node head;
private Node tail;
private int size;
public DoubleLinkedList(){
head=new Node(0,0);
tail=new Node(0,0);
head.next=tail;
tail.pre=head;
size=0;
}
public void addFirst(Node node){
node.next=head.next;
node.pre=head;
head.next.pre=node;
head.next=node;
size++;
}
public void remove(Node node){
node.pre.next=node.next;
node.next.pre=node.pre;
size--;
}
public Node removeLast(){
if(tail.pre==head){
return null;
}
Node last=tail.pre;
remove(last);
return last;
}
public int size(){
return size;
}
public static class Node{
int key;
int value;
Node pre;
Node next;
public Node(int key,int value){
this.key=key;
this.value=value;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
编辑 (opens new window)
上次更新: 2024/02/22, 14:03:19