Skip to content

Commit b7d36b8

Browse files
author
robert
committed
代码完善
1 parent 31ae316 commit b7d36b8

File tree

15 files changed

+831
-454
lines changed

15 files changed

+831
-454
lines changed

.classpath

Lines changed: 0 additions & 20 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/bin/
22
/target/
3+
.project
4+
.settings/

.project

Lines changed: 0 additions & 23 deletions
This file was deleted.

.settings/org.eclipse.jdt.core.prefs

Lines changed: 0 additions & 13 deletions
This file was deleted.

.settings/org.eclipse.m2e.core.prefs

Lines changed: 0 additions & 4 deletions
This file was deleted.

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
<target />
3232
</configuration>
3333
</plugin>
34-
</plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<configuration>
38+
<source>7</source>
39+
<target>7</target>
40+
</configuration>
41+
</plugin>
42+
</plugins>
3543
</build>
3644

3745

src/com/algorithm/list/MyLinkedList.java

Lines changed: 143 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,144 +6,151 @@
66

77
/**
88
* 链表数据结构
9-
*
10-
* @author chao
119
*
1210
* @param <E>
11+
* @author chao
1312
*/
1413
public class MyLinkedList<E> implements Iterable<E> {
15-
public static class Node<T> {
16-
public Node() {
17-
18-
}
19-
20-
public Node(T ele) {
21-
this.ele = ele;
22-
}
23-
24-
public Node(T ele, Node next, Node pre) {
25-
this.ele = ele;
26-
this.next = next;
27-
this.pre = pre;
28-
}
29-
30-
T ele;
31-
Node next;
32-
Node pre;
33-
}
34-
35-
private Node<E> head;
36-
private Node<E> tail;
37-
private int size;
38-
private int modcount = 0;
39-
40-
public MyLinkedList() {
41-
clear();
42-
}
43-
44-
public void clear() {
45-
head = new Node<E>(null, null, null);
46-
tail = new Node<E>(null, head, null);
47-
head.next = tail;
48-
size = 0;
49-
modcount++;
50-
51-
}
52-
53-
@Override
54-
public Iterator<E> iterator() {
55-
return new LinkedListIterator();
56-
}
57-
58-
private class LinkedListIterator implements Iterator<E> {
59-
private Node<E> current = head.next;
60-
private boolean okToremove = false;
61-
private int expectedModcount = modcount;
62-
63-
@Override
64-
public boolean hasNext() {
65-
return current != tail;
66-
}
67-
68-
@Override
69-
public E next() {
70-
if (modcount != expectedModcount) {
71-
throw new ConcurrentModificationException();
72-
} else if (!hasNext()) {
73-
throw new NoSuchElementException();
74-
}
75-
current = current.next;
76-
okToremove = true;
77-
return (E) current.pre.ele;
78-
}
79-
80-
@Override
81-
public void remove() {
82-
if (!okToremove) {
83-
throw new IllegalStateException();
84-
} else if (modcount != expectedModcount) {
85-
throw new ConcurrentModificationException();
86-
}
87-
MyLinkedList.this.remove(current.pre);
88-
okToremove = false;
89-
expectedModcount++;
90-
}
91-
}
92-
93-
public boolean isempty() {
94-
return size == 0;
95-
}
96-
97-
public int size() {
98-
return size;
99-
}
100-
101-
private Node getNode(int index) {
102-
if (index < 0 || index >= size) {
103-
throw new IndexOutOfBoundsException();
104-
}
105-
Node<E> p = null;
106-
if (index < size / 2) {
107-
p = head.next;
108-
for (int i = 0; i < index; i++)
109-
p = p.next;
110-
} else {
111-
p = tail;
112-
for (int i = size; i > index; i--)
113-
p = p.pre;
114-
}
115-
return p;
116-
}
117-
118-
public E get(int index) {
119-
return (E) getNode(index).ele;
120-
}
121-
122-
private E remove(Node node) {
123-
node.next.pre = node.pre;
124-
node.pre.next = node.next;
125-
size--;
126-
modcount++;
127-
return (E) node.ele;
128-
}
129-
130-
public E remove(int index) {
131-
return remove(getNode(index));
132-
}
133-
134-
public void add(int index, E ele) {
135-
addBefore(getNode(index), ele);
136-
}
137-
138-
private void addBefore(Node node, E ele) {
139-
Node newnode = new Node<E>(ele, node.pre, node);
140-
node.pre.next = newnode;
141-
node.pre = newnode;
142-
size++;
143-
modcount++;
144-
}
145-
146-
public void add(E ele) {
147-
add(size, ele);
148-
}
14+
15+
public static class Node<T> {
16+
public Node() {
17+
18+
}
19+
20+
public Node(T ele) {
21+
this.ele = ele;
22+
}
23+
24+
public Node(T ele, Node<T> next, Node<T> pre) {
25+
this.ele = ele;
26+
this.next = next;
27+
this.pre = pre;
28+
}
29+
30+
T ele;
31+
Node<T> next;
32+
Node<T> pre;
33+
}
34+
35+
private Node<E> head;
36+
private Node<E> tail;
37+
private int size;
38+
private int modcount = 0;
39+
40+
public MyLinkedList() {
41+
clear();
42+
}
43+
44+
public Node getHead() {
45+
return head;
46+
}
47+
48+
public void clear() {
49+
head = new Node<>(null, null, null);
50+
tail = new Node<>(null, head, null);
51+
head.next = tail;
52+
size = 0;
53+
modcount++;
54+
55+
}
56+
57+
@Override
58+
public Iterator<E> iterator() {
59+
return new LinkedListIterator();
60+
}
61+
62+
private class LinkedListIterator implements Iterator<E> {
63+
private Node<E> current = head.next;
64+
private boolean okToremove = false;
65+
private int expectedModcount = modcount;
66+
67+
@Override
68+
public boolean hasNext() {
69+
return current != tail;
70+
}
71+
72+
@Override
73+
public E next() {
74+
if (modcount != expectedModcount) {
75+
throw new ConcurrentModificationException();
76+
} else if (!hasNext()) {
77+
throw new NoSuchElementException();
78+
}
79+
current = current.next;
80+
okToremove = true;
81+
return current.pre.ele;
82+
}
83+
84+
@Override
85+
public void remove() {
86+
if (!okToremove) {
87+
throw new IllegalStateException();
88+
} else if (modcount != expectedModcount) {
89+
throw new ConcurrentModificationException();
90+
}
91+
MyLinkedList.this.remove(current.pre);
92+
okToremove = false;
93+
expectedModcount++;
94+
}
95+
}
96+
97+
public boolean isEmpty() {
98+
return size == 0;
99+
}
100+
101+
public int size() {
102+
return size;
103+
}
104+
105+
private Node<E> getNode(int index) {
106+
if (index == 0) {
107+
return head;
108+
}
109+
if (index < 0 || index >= size) {
110+
throw new IndexOutOfBoundsException();
111+
}
112+
Node p = null;
113+
if (index < size / 2) {
114+
p = head.next;
115+
for (int i = 0; i < index; i++)
116+
p = p.next;
117+
} else {
118+
p = tail;
119+
for (int i = size; i > index; i--)
120+
p = p.pre;
121+
}
122+
return p;
123+
}
124+
125+
public E get(int index) {
126+
return (E) getNode(index).ele;
127+
}
128+
129+
private E remove(Node node) {
130+
node.next.pre = node.pre;
131+
node.pre.next = node.next;
132+
size--;
133+
modcount++;
134+
return (E) node.ele;
135+
}
136+
137+
public E remove(int index) {
138+
return remove(getNode(index));
139+
}
140+
141+
public void add(int index, E ele) {
142+
addBefore(getNode(index), ele);
143+
}
144+
145+
private void addBefore(Node node, E ele) {
146+
Node newnode = new Node<E>(ele, node.pre, node);
147+
node.pre.next = newnode;
148+
node.pre = newnode;
149+
size++;
150+
modcount++;
151+
}
152+
153+
public void add(E ele) {
154+
add(size, ele);
155+
}
149156
}

0 commit comments

Comments
 (0)