File tree Expand file tree Collapse file tree 3 files changed +49
-4
lines changed
group08/108621969/com/coding/basic Expand file tree Collapse file tree 3 files changed +49
-4
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ private boolean isFull() {
63
63
return false ;
64
64
}
65
65
66
- private boolean isEmpty () {
66
+ public boolean isEmpty () {
67
67
if (size == 0 ) {
68
68
return true ;
69
69
}
Original file line number Diff line number Diff line change 4
4
* Created by zhangjiatao on 2017/2/25.
5
5
*/
6
6
public class Queue {
7
+ private ArrayList elementData = new ArrayList ();
8
+
7
9
public void enQueue (Object o ) {
10
+ elementData .add (0 , o );
8
11
}
9
12
10
13
public Object deQueue () {
11
- return null ;
14
+ return elementData . remove ( elementData . size () - 1 ) ;
12
15
}
13
16
14
17
public boolean isEmpty () {
15
- return false ;
18
+ return elementData . isEmpty () ;
16
19
}
17
20
18
21
public int size () {
19
- return -1 ;
22
+ return elementData .size ();
23
+ }
24
+
25
+ public static void main (String [] args ) {
26
+ Queue queue = new Queue ();
27
+ queue .enQueue (1 );
28
+ queue .enQueue (2 );
29
+ queue .enQueue (3 );
30
+ System .out .println (queue .deQueue ());
31
+ System .out .println (queue .toString ());
20
32
}
21
33
}
Original file line number Diff line number Diff line change 1
1
package com .coding .basic ;
2
2
3
+ import java .util .ArrayList ;
4
+
5
+
3
6
/**
4
7
* Created by zhangjiatao on 2017/2/25.
5
8
*/
6
9
public class Stack {
10
+ private ArrayList elementData = new ArrayList ();
11
+
12
+ public void push (Object o ) {
13
+ elementData .add (elementData .size (), o );
14
+ }
15
+
16
+ public Object pop () {
17
+ return elementData .remove (elementData .size () - 1 );
18
+ }
19
+
20
+ public Object peek () {
21
+ return elementData .get (elementData .size () - 1 );
22
+ }
23
+
24
+ public boolean isEmpty () {
25
+ return elementData .isEmpty ();
26
+ }
27
+
28
+ public int size () {
29
+ return elementData .size ();
30
+ }
31
+
32
+ public static void main (String [] args ) {
33
+ Stack stack = new Stack ();
34
+ stack .push (1 );
35
+ stack .push (2 );
36
+ stack .push (3 );
37
+ System .out .println (stack .pop ());
38
+ System .out .println (stack .toString ());
39
+ }
7
40
}
You can’t perform that action at this time.
0 commit comments