Skip to content

Commit e8416e4

Browse files
committed
'update'
1 parent 951e991 commit e8416e4

File tree

4 files changed

+118
-23
lines changed

4 files changed

+118
-23
lines changed

group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.coding.basic.impl;
22

33
import java.util.Arrays;
4-
import java.util.LinkedList;
54

65
import com.coding.basic.Iterator;
76
import com.coding.basic.List;
@@ -37,7 +36,6 @@ public ArrayList(){
3736
* 构造函数,初始化容量为length的空列表
3837
*/
3938
public ArrayList(int length){
40-
super();
4139
if(length < 0){
4240
throw new IllegalArgumentException("初始容量参数非法:"+length);
4341
}
@@ -71,7 +69,7 @@ private void grow(int length){
7169
* @comment:检验下标参数是否超限
7270
*/
7371
private void check(int index) {
74-
if( index > data.length){
72+
if( index >= size){
7573
throw new IndexOutOfBoundsException("Index:"+index+",size:"+size);
7674
}
7775
}
@@ -163,24 +161,6 @@ public Object next() {
163161

164162
}
165163

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-
184164
}
185165

186166

group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ public Object remove(int index) {
111111
public int size() {
112112
return size;
113113
}
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+
}
114134

115135
public Iterator iterator(){
116136

@@ -154,7 +174,7 @@ public boolean hasNext() {
154174

155175
@Override
156176
public Object next() {
157-
Node n = getNode(index);
177+
Node n = getNode(index++);
158178
return n.data;
159179
}
160180

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coding.basic.impl;
22

3+
import com.coding.basic.Iterator;
4+
35
/**
46
* 栈的简单实现
57
* @author 240094626
@@ -47,4 +49,30 @@ public boolean isEmpty(){
4749
public int size(){
4850
return elementData.size();
4951
}
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+
}

0 commit comments

Comments
 (0)