Skip to content

Commit 306b41b

Browse files
author
luoziyihao
committed
ok queue, Stack
1 parent cf6ea46 commit 306b41b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package com.coding.basic;
22

3+
import java.util.Arrays;
4+
35
public class Queue {
4-
5-
public void enQueue(Object o){
6+
7+
private LinkedList elementData = new LinkedList();
8+
9+
public void enQueue(Object o){
10+
elementData.add(o);
611
}
712

813
public Object deQueue(){
9-
return null;
14+
return elementData.remove(0);
1015
}
1116

1217
public boolean isEmpty(){
13-
return false;
18+
return size() == 0;
1419
}
1520

1621
public int size(){
17-
return -1;
22+
return elementData.size();
1823
}
1924
}

group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33
public class Stack {
44
private ArrayList elementData = new ArrayList();
55

6-
public void push(Object o){
6+
public void push(Object o){
7+
elementData.add(o);
78
}
89

910
public Object pop(){
10-
return null;
11+
if (isEmpty()) {
12+
throw new IllegalStateException("the stack is empty");
13+
}
14+
return elementData.remove(elementData.size() - 1);
1115
}
1216

1317
public Object peek(){
14-
return null;
18+
if (isEmpty()) {
19+
throw new IllegalStateException("the stack is empty");
20+
}
21+
return elementData.get(elementData.size() - 1);
1522
}
23+
1624
public boolean isEmpty(){
17-
return false;
25+
return size() == 0;
1826
}
1927
public int size(){
20-
return -1;
28+
return elementData.size();
2129
}
2230
}

0 commit comments

Comments
 (0)