Skip to content

Commit 2466967

Browse files
committed
添加队列和栈的越界检查
1 parent 061e00c commit 2466967

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.github.zhanglifeng.coding2017.basic;
22

3+
import java.util.EmptyStackException;
4+
35
public class Queue {
46
private LinkedList elementData = new LinkedList();
57
public void enQueue(Object o){
68
elementData.add(o);
79
}
810

911
public Object deQueue(){
12+
if (elementData.size() == 0) {
13+
throw new EmptyStackException();
14+
}
1015
return elementData.removeFirst();
1116
}
1217

group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.zhanglifeng.coding2017.basic;
22

3+
import java.util.EmptyStackException;
4+
35
public class Stack {
46
private ArrayList elementData = new ArrayList();
57

@@ -8,10 +10,16 @@ public void push(Object o){
810
}
911

1012
public Object pop(){
13+
if (elementData.size() == 0) {
14+
throw new EmptyStackException();
15+
}
1116
return elementData.remove(elementData.size() - 1);
1217
}
1318

1419
public Object peek(){
20+
if (elementData.size() == 0) {
21+
throw new EmptyStackException();
22+
}
1523
return elementData.get(elementData.size() - 1);
1624
}
1725
public boolean isEmpty(){

0 commit comments

Comments
 (0)