File tree Expand file tree Collapse file tree 4 files changed +118
-23
lines changed
group17/240094626/warm-up/src/com/coding/basic/impl Expand file tree Collapse file tree 4 files changed +118
-23
lines changed Original file line number Diff line number Diff line change 1
1
package com .coding .basic .impl ;
2
2
3
3
import java .util .Arrays ;
4
- import java .util .LinkedList ;
5
4
6
5
import com .coding .basic .Iterator ;
7
6
import com .coding .basic .List ;
@@ -37,7 +36,6 @@ public ArrayList(){
37
36
* 构造函数,初始化容量为length的空列表
38
37
*/
39
38
public ArrayList (int length ){
40
- super ();
41
39
if (length < 0 ){
42
40
throw new IllegalArgumentException ("初始容量参数非法:" +length );
43
41
}
@@ -71,7 +69,7 @@ private void grow(int length){
71
69
* @comment:检验下标参数是否超限
72
70
*/
73
71
private void check (int index ) {
74
- if ( index > data . length ){
72
+ if ( index >= size ){
75
73
throw new IndexOutOfBoundsException ("Index:" +index +",size:" +size );
76
74
}
77
75
}
@@ -163,24 +161,6 @@ public Object next() {
163
161
164
162
}
165
163
166
- // Test
167
- public static void main (String [] args ) {
168
- ArrayList list = new ArrayList (5 );
169
- System .out .println (list .size ());
170
- list .add ("1" );
171
- list .add (1 ,"hei" );
172
-
173
- System .out .println (list .toString ());
174
- Iterator i = list .iterator ();
175
- while (i .hasNext ()){
176
- System .out .println (i .next ());
177
- }
178
- // java.util.ArrayList<String> a = new java.util.ArrayList<String>();
179
- // Iterator iterator = (Iterator) a.iterator();
180
- }
181
-
182
-
183
-
184
164
}
185
165
186
166
Original file line number Diff line number Diff line change @@ -111,6 +111,26 @@ public Object remove(int index) {
111
111
public int size () {
112
112
return size ;
113
113
}
114
+ /**
115
+ * 环形链表结构,header.next就是第一个节点
116
+ * @param o
117
+ */
118
+ public void addFirst (Object o ){
119
+ addBefore (o , header .next );
120
+ }
121
+ /**
122
+ * 环形链表结构,header.pre就是最后一个节点
123
+ * @param o
124
+ */
125
+ public void addLast (Object o ){
126
+ addBefore (o , header );
127
+ }
128
+ public Object removeFirst (){
129
+ return remove (header .next );
130
+ }
131
+ public Object removeLast (){
132
+ return remove (header .pre );
133
+ }
114
134
115
135
public Iterator iterator (){
116
136
@@ -154,7 +174,7 @@ public boolean hasNext() {
154
174
155
175
@ Override
156
176
public Object next () {
157
- Node n = getNode (index );
177
+ Node n = getNode (index ++ );
158
178
return n .data ;
159
179
}
160
180
Original file line number Diff line number Diff line change
1
+ package com .coding .basic .impl ;
2
+
3
+ import com .coding .basic .Iterator ;
4
+
5
+ /**
6
+ * 队列简单实现
7
+ * @author 240094626
8
+ *
9
+ */
10
+ public class Queue {
11
+ /**队列元素容器对象*/
12
+ LinkedList elementData = new LinkedList ();
13
+
14
+ /**
15
+ * 入队列
16
+ * @param o
17
+ */
18
+ public void enQueue (Object o ){
19
+ elementData .add (o );
20
+ }
21
+
22
+ /**
23
+ * 出队列:先进先出,故取出链表首个节点
24
+ * @return
25
+ */
26
+ public Object deQueue (){
27
+ return elementData .removeFirst ();
28
+ }
29
+
30
+ public boolean isEmpty (){
31
+ if (elementData .size () > 0 ){
32
+ return false ;
33
+ }
34
+ return true ;
35
+ }
36
+
37
+ public int size (){
38
+ return elementData .size ();
39
+ }
40
+
41
+ public Iterator iterator (){
42
+ return new QueueIterator ();
43
+ }
44
+
45
+ private class QueueIterator implements Iterator {
46
+
47
+ int index ;
48
+
49
+ public QueueIterator () {
50
+ index = 0 ;
51
+ }
52
+
53
+ @ Override
54
+ public boolean hasNext () {
55
+ if (index < elementData .size ()){
56
+ return true ;
57
+ }
58
+ return false ;
59
+ }
60
+
61
+ @ Override
62
+ public Object next () {
63
+ return elementData .get (index ++);
64
+ }
65
+
66
+ }
67
+ }
Original file line number Diff line number Diff line change 1
1
package com .coding .basic .impl ;
2
2
3
+ import com .coding .basic .Iterator ;
4
+
3
5
/**
4
6
* 栈的简单实现
5
7
* @author 240094626
@@ -47,4 +49,30 @@ public boolean isEmpty(){
47
49
public int size (){
48
50
return elementData .size ();
49
51
}
50
- }
52
+
53
+ public Iterator iterator (){
54
+ return new StackIterator ();
55
+ }
56
+ private class StackIterator implements Iterator {
57
+ int index ;
58
+
59
+ public StackIterator () {
60
+ index = 0 ;
61
+ }
62
+
63
+ @ Override
64
+ public boolean hasNext () {
65
+ if (index < elementData .size ()){
66
+ return true ;
67
+ }
68
+ return false ;
69
+ }
70
+
71
+ @ Override
72
+ public Object next () {
73
+ return elementData .get (index );
74
+ }
75
+
76
+
77
+ }
78
+ }
You can’t perform that action at this time.
0 commit comments