-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLList.java
159 lines (137 loc) · 3.94 KB
/
DLList.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
public class DLList {
private class IntNode{
int item;
IntNode prev;
IntNode next;
public IntNode(int x, IntNode p, IntNode n){
item = x;
prev = p;
next = n;
}
}
private IntNode sentinel;
// private IntNode first;
private int size;
public DLList() {
sentinel = new IntNode(922, sentinel, sentinel);
sentinel.prev = sentinel;
sentinel.next = sentinel;
size = 0;
}
public DLList(DLList d) {
DLList d_copy = new DLList();
int i = 0;
while (i < d.size) {
d_copy.addLast(d.get(i));
i++;
}
this.sentinel = d_copy.sentinel;
this.size = d_copy.size;
}
// public DLList(int x) {
// sentinel = new IntNode(922, sentinel, sentinel);
// sentinel.prev = sentinel;
// sentinel.next = sentinel;
// first = new IntNode(x, sentinel, sentinel.next);
// size = 1;
// }
public void addFrist(int x) {
sentinel.next = new IntNode(x, sentinel, sentinel.next);
sentinel.next.next.prev = sentinel.next;
size ++;
}
public void addLast(int x) {
sentinel.prev = new IntNode(x, sentinel.prev, sentinel);
sentinel.prev.prev.next = sentinel.prev;
size ++;
}
public boolean isEmpty() {
if(this.size() == 0)
return true;
return false;
}
public int size(){
return size;
}
public void printDeque() {
IntNode p = sentinel.next;
int n = size;
while (n > 1){
System.out.print(p.item + " ");
p = p.next;
n --;
}
System.out.println(p.item);
}
public int removeFirst() {
int i = sentinel.next.item;
sentinel.next.next.prev = sentinel;
sentinel.next = sentinel.next.next;
size --;
return i;
}
public int removeLast() {
int i = sentinel.prev.item;
sentinel.prev.prev.next = sentinel;
sentinel.prev = sentinel.prev.prev;
size --;
return i;
}
public int get(int x) {
int n = 0;
IntNode p = sentinel.next;
if(x >= size)
return -1;
while (x != n) {
n ++;
p = p.next;
}
return p.item;
}
/** 使用递归实现是需要考虑辅助方法,想了比较久。 */
public int getRecursive(int x) {
if(x > size - 1)
return -999999;
return getR(sentinel.next, x);
}
public int getR(IntNode i, int x) {
if(x == 0)
return i.item;
return getR(i.next, -- x);
}
public static void main(String[] args) {
DLList D = new DLList();
D.addFrist(9);
D.addFrist(1);
D.addFrist(3);
D.addLast(0);
D.addLast(1);
D.addLast(0);
D.addLast(2);
D.addLast(0);
D.addLast(2);
D.addLast(9);
System.out.println(D.size);
D.printDeque();
System.out.println(D.removeFirst());
D.printDeque();
System.out.println(D.removeLast());
D.printDeque();
System.out.println(D.get(1));
System.out.println(D.get(2));
DLList D1 = new DLList(D);
D1.printDeque();
System.out.println(D1.size);
D.addFrist(3);
D.printDeque();
D1.printDeque();
System.out.println(D1.getRecursive(0));
System.out.println(D1.getRecursive(1));
System.out.println(D1.getRecursive(2));
System.out.println(D1.getRecursive(3));
System.out.println(D1.getRecursive(4));
System.out.println(D1.getRecursive(5));
System.out.println(D1.getRecursive(6));
System.out.println(D1.getRecursive(7));
}
}