File tree Expand file tree Collapse file tree 5 files changed +215
-0
lines changed Expand file tree Collapse file tree 5 files changed +215
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
3
+ /**
4
+ * Created by Great on 2017/2/25.
5
+ */
6
+ public class ArrayList {
7
+ private final int DEFAULT_SIZE = 20 ;
8
+ int [] array = new int [DEFAULT_SIZE ];
9
+ int size = 0 ;
10
+ public void add (int e ) {
11
+ if (size == array .length ) {
12
+ array = Arrays .copyOf (array , array .length + DEFAULT_SIZE );
13
+ }
14
+ array [size ] = e ;
15
+ ++size ;
16
+ }
17
+
18
+ public Integer get (int i ) {
19
+ if (i < 0 || i >= size ) return null ;
20
+ return array [i ];
21
+ }
22
+
23
+ public int size () {
24
+ return size ;
25
+ }
26
+
27
+ public void remove (int i ) {
28
+ if (i < 0 || i >= size ) return ;
29
+ for (int j = i ; j < size ; j ++) {
30
+ array [j ] = array [j + 1 ];
31
+ }
32
+ --size ;
33
+ }
34
+ public boolean isEmpty () {
35
+ return size == 0 ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Created by Great on 2017/2/24.
3
+ */
4
+ public class LinkList {
5
+ private class Node {
6
+ Node next ;
7
+ int data ;
8
+ }
9
+ private Node rear ;
10
+ private Node top ;
11
+ private int size = 0 ;
12
+ public void add (int e ) {
13
+ if (rear == null ) {
14
+ rear = new Node ();
15
+ top = rear ;
16
+ }else {
17
+ rear .next = new Node ();
18
+ rear = rear .next ;
19
+ }
20
+ rear .data = e ;
21
+ ++size ;
22
+ }
23
+
24
+ public Integer getReciprocal (int count ) {
25
+ if (top == null || count < 1 ) return null ;
26
+ Node node = top ;
27
+ int size = 0 ;
28
+ while ( node .next != null ) {
29
+ node = node .next ;
30
+ size ++;
31
+ }
32
+ if (size +1 < count ) return null ;
33
+ return get (size +1 -count );
34
+ }
35
+
36
+ public Integer get (int i ) {
37
+ if (i < 0 || i >= size ) return null ;
38
+ return getNode (i ).data ;
39
+ }
40
+
41
+ public void remove (int i ) {
42
+ if (i < 0 || i >= size ) return ;
43
+ if (i == 0 ) {
44
+ top = top .next ;
45
+ return ;
46
+ }
47
+ if (i == size - 1 ) {
48
+ getNode (i - 1 ).next = null ;
49
+ }
50
+ Node front = getNode (i - 1 );
51
+ Node back = getNode (i + 1 );
52
+ front .next = back ;
53
+ --size ;
54
+ }
55
+
56
+ private Node getNode (int i ) {
57
+ if (i < 0 || i >= size ) return null ;
58
+ Node node = top ;
59
+ for (int j = 0 ; j < size ; j ++) {
60
+ if (j == i ) return node ;
61
+ node = node .next ;
62
+ }
63
+ return null ;
64
+ }
65
+
66
+ public int size () {
67
+ return size ;
68
+ }
69
+
70
+ public boolean isEmpty () {
71
+ return size == 0 ;
72
+ }
73
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
3
+ /**
4
+ * Created by Great on 2017/2/23.
5
+ */
6
+ public class Queue {
7
+ private class Node {
8
+ Node next ;
9
+ int data ;
10
+ }
11
+ private int size ;
12
+ private Node front ;
13
+ private Node rear ;
14
+ public void add (int e ) {
15
+ if (front == null ) {
16
+ front = new Node ();
17
+ rear = front ;
18
+ }else {
19
+ front .next = new Node ();
20
+ front = front .next ;
21
+ }
22
+ front .data = e ;
23
+ ++size ;
24
+ }
25
+
26
+ public Integer poll () {
27
+ if (rear == null ) return null ;
28
+ int data = rear .data ;
29
+ rear = rear .next ;
30
+ --size ;
31
+ return data ;
32
+ }
33
+
34
+ public boolean isEmpty () {
35
+ return size == 0 ;
36
+ }
37
+
38
+ public int size () {
39
+ return size ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+
3
+ /**
4
+ * Created by Great on 2017/2/23.
5
+ */
6
+ public class Stack {
7
+ private final int DEFAULT_SIZE = 20 ;
8
+ private int base ;
9
+ private int top ;
10
+ private int [] array = new int [DEFAULT_SIZE ];
11
+ public void push (Integer e ) {
12
+ if (top == array .length ){
13
+ array = Arrays .copyOf (array , array .length + DEFAULT_SIZE );
14
+ }
15
+ array [top ] = e ;
16
+ ++top ;
17
+ }
18
+ public Integer pop (){
19
+ if (top == 0 ) return null ;
20
+ top --;
21
+ return array [top ];
22
+ }
23
+ public int size () {
24
+ return top ;
25
+ }
26
+ public boolean isEmpty (){
27
+ return top == 0 ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .List ;
2
+
3
+ /**
4
+ * Created by Great on 2017/2/23.
5
+ */
6
+ public class Test {
7
+ public static void main (String [] args ) {
8
+ ArrayList arrayList = new ArrayList ();
9
+ LinkList linkList = new LinkList ();
10
+ Queue queue = new Queue ();
11
+ Stack stack = new Stack ();
12
+ for (int i = 0 ; i < 10 ; i ++) {
13
+ arrayList .add (i );
14
+ linkList .add (i );
15
+ queue .add (i );
16
+ stack .push (i );
17
+ }
18
+ System .out .println ("ArrayList:" );
19
+ for (int i = 0 ; i < arrayList .size (); i ++) {
20
+ System .out .println (arrayList .get (i ));
21
+ }
22
+ System .out .println ("LinkList:" );
23
+ for (int i = 0 ; i < linkList .size (); i ++) {
24
+ System .out .println (linkList .get (i ));
25
+ }
26
+ System .out .println ("Queue:" );
27
+ while (!queue .isEmpty ()) {
28
+ System .out .println (queue .poll ());
29
+ }
30
+ System .out .println ("Stack:" );
31
+ while (!stack .isEmpty ()) {
32
+ System .out .println (stack .pop ());
33
+ }
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments