1
1
package com .coding .basic ;
2
2
3
+ import com .coding .basic .LinkedList .Node ;
4
+
3
5
public class LinkedList implements List {
4
6
5
7
private Node head ;
8
+ private int size ;
6
9
7
10
public void add (Object o ){
8
-
11
+ if (head .data == null ) {
12
+ head .data = o ;
13
+ head .next = null ;
14
+ size ++;
15
+ return ;
16
+ }
17
+ Node node = new Node (o );
18
+ Node curr = head ;
19
+ while (curr .next != null ) {
20
+ curr = curr .next ;
21
+ }
22
+ curr .next = node ;
23
+ size ++;
9
24
}
10
25
public void add (int index , Object o ){
11
-
26
+ if (index < 0 || index > size ) {
27
+ System .out .println (index + "无效指数" );
28
+ return ;
29
+ }
30
+ if (head .data == null ) {
31
+ if (index == 0 ) {
32
+ head .data = o ;
33
+ head .next = null ;
34
+ size ++;
35
+ return ;
36
+ } else {
37
+ System .out .println ("无效指数" );
38
+ return ;
39
+ }
40
+ }
41
+ Node node = new Node (o );
42
+ Node curr = head ;
43
+ for (int i = 0 ; i < index - 1 ; i ++) {
44
+ curr = curr .next ;
45
+ }
46
+ Node temp = curr .next ;
47
+ curr .next = node ;
48
+ node .next = temp ;
49
+ size ++;
12
50
}
13
51
public Object get (int index ){
14
- return null ;
52
+ if (index < 0 || index > size ) {
53
+ System .out .println (index + " is invalid index!" );
54
+ return null ;
55
+ }
56
+ Node result = head ;
57
+ for (int i = 0 ; i < index ; i ++) {
58
+ result = result .next ;
59
+ }
60
+ return result ;
15
61
}
16
62
public Object remove (int index ){
17
- return null ;
63
+ if (index < 0 || index > size ) {
64
+ System .out .println (index + " is invalid index!" );
65
+ return null ;
66
+ }
67
+ Node curr = head ;
68
+ for (int i = 0 ; i < index - 1 ; i ++) {
69
+ curr = curr .next ;
70
+ }
71
+ Node result = curr .next ;
72
+ curr .next = curr .next .next ;
73
+ size --;
74
+ return result ;
18
75
}
19
76
20
77
public int size (){
21
- return - 1 ;
78
+ return this . size ;
22
79
}
23
80
24
81
public void addFirst (Object o ){
25
-
82
+ if (head .data == null ) {
83
+ head .data = o ;
84
+ head .next = null ;
85
+ size ++;
86
+ return ;
87
+ }
88
+ Node temp = head ;
89
+ head = new Node (o );
90
+ head .next = temp ;
91
+ size ++;
26
92
}
27
93
public void addLast (Object o ){
28
-
94
+ if (head .data == null ) {
95
+ head .data = o ;
96
+ head .next = null ;
97
+ size ++;
98
+ return ;
99
+ }
100
+ Node node = new Node (o );
101
+ Node curr = head ;
102
+ while (curr .next != null ) {
103
+ curr = curr .next ;
104
+ }
105
+ curr .next = node ;
106
+ size ++;
29
107
}
30
108
public Object removeFirst (){
31
- return null ;
109
+ if (head .data == null ) {
110
+ return null ;
111
+ }
112
+ Node result = head ;
113
+ head = head .next ;
114
+ size --;
115
+ return result ;
32
116
}
33
117
public Object removeLast (){
34
- return null ;
118
+ if (head .data == null ) {
119
+ return null ;
120
+ }
121
+ Node curr = head ;
122
+ for (int i = 0 ; i < size - 1 ; i ++) {
123
+ curr = curr .next ;
124
+ }
125
+ Node result = curr .next ;
126
+ curr .next = null ;
127
+ size --;
128
+ return result ;
35
129
}
36
130
public Iterator iterator (){
37
131
return null ;
@@ -41,6 +135,12 @@ public Iterator iterator(){
41
135
private static class Node {
42
136
Object data ;
43
137
Node next ;
138
+ public Node (Object o ) {
139
+ data = o ;
140
+ next = null ;
141
+ }
142
+
143
+
44
144
45
145
}
46
146
}
0 commit comments