File tree Expand file tree Collapse file tree 2 files changed +23
-10
lines changed
group17/1204187480/code/homework/basic/src/main/java/com/coding/basic Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Original file line number Diff line number Diff line change 1
1
package com .coding .basic ;
2
2
3
+ import java .util .Arrays ;
4
+
3
5
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 );
6
11
}
7
12
8
13
public Object deQueue (){
9
- return null ;
14
+ return elementData . remove ( 0 ) ;
10
15
}
11
16
12
17
public boolean isEmpty (){
13
- return false ;
18
+ return size () == 0 ;
14
19
}
15
20
16
21
public int size (){
17
- return - 1 ;
22
+ return elementData . size () ;
18
23
}
19
24
}
Original file line number Diff line number Diff line change 3
3
public class Stack {
4
4
private ArrayList elementData = new ArrayList ();
5
5
6
- public void push (Object o ){
6
+ public void push (Object o ){
7
+ elementData .add (o );
7
8
}
8
9
9
10
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 );
11
15
}
12
16
13
17
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 );
15
22
}
23
+
16
24
public boolean isEmpty (){
17
- return false ;
25
+ return size () == 0 ;
18
26
}
19
27
public int size (){
20
- return - 1 ;
28
+ return elementData . size () ;
21
29
}
22
30
}
You can’t perform that action at this time.
0 commit comments