-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleLinkedList.java
More file actions
164 lines (150 loc) · 4.18 KB
/
Copy pathSingleLinkedList.java
File metadata and controls
164 lines (150 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package Array;
import Node.Node;
public class SingleLinkedList<T> implements _LinkedList<T> {
public Node<T> head;
//public Node<T> end;
public SingleLinkedList(Node<T> head){
this.head = head;
}
@Override
public boolean isEmpty() {
boolean result = false;
if (this.head == null)
result = true;
return result;
}
@Override
public int length() {
int n = 0;
Node<T> temp = head;
while (temp !=null){
n ++;
temp = temp.next;
}
return n;
}
@Override
public T get(int index) {
if (this.head != null && index > 0){
int count = 0;
Node<T> temp = this.head;
while (temp != null && count < index){
temp = temp.next;
count ++;
}
if (temp != null) {
return temp.data;
}
}
return null;
}
@Override
public T set(int index, T data) {
if (this.head != null && index > 0 ) {
int count = 0;
Node<T> temp = this.head;
//find target node
while (temp != null && count < index){
temp = temp.next;
count ++;
}
if (temp != null){
T old = temp.data;
temp.data = data;
return old;
}
}
return null;
}
@Override
public boolean add(int index, T data) {
if (data == null){
return false;
}
/*add at begin.*/
/**
* Node<T> p = new Node<T>(data,null)
* p.next = this.head;
* this.head = p;
* */
if (this.head == null || index <= 1) {
this.head = new Node<T>(data,this.head);
} else {
/*add in middle*/
/**
* Node<T> tNode = new Node<>(data,null);
* tNode.next = front.next;//先赋node尾巴到p
* front.next = tNode;//再赋node头链到front
*/
/*add at last*/
/**
* Node<T> tNode = new Node<>(data,null);
* tNode.next = null;
* front.next = tNode;
*/
int count = 0;
Node<T> front = this.head;
//找到插入index前的那个node
while (front.next != null && count < index-1){
front = front.next;
count ++;
}
front.next = new Node<T>(data,front.next);
}
return false;
}
@Override
public boolean add(T data) {
return false;
}
@Override
public T remove(int index) {
T old = null;
if (this.head != null && index >= 0) {
/*remove at begin.*/
/**
* 让第二个当头
* head = head.next
**/
if (index == 0) {
old = this.head.data;
this.head = this.head.next;
} else {
/*remove at middle or last.*/
/**
* 找到index前一个node。把删除targetNode = node.Next
* node.Next = targetNode.next;即可。
* targetNode = null;这步是为了free gc Node
**/
Node<T> front = this.head;
int count = 0;
while (this.head.next != null && count < index -1) {
front = front.next;
count ++;
}
//get the index one
Node<T> result = front.next;
if (result != null) {
old = result.data;
//front.next = front.next.next
front.next = result.next;
//GC? 不写的话 result 永远没有被指向
result = null;
}
}
}
return old;
}
@Override
public boolean removeAll(T data) {
return false;
}
@Override
public void clear() {
this.head = null;
}
@Override
public boolean contains(T data) {
return false;
}
}