-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleLinkedList.java
215 lines (168 loc) · 4.12 KB
/
SingleLinkedList.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.coding.basic.linkedList;
import java.util.Collection;
/**
* 尾插式单链表实现
* 笔记详见:http://www.cnblogs.com/lyu0709/p/6770309.html
*
* @author Administrator
*
*/
public class SingleLinkedList<T> {
private Node<T> head; // 头结点:指向第一个结点的位置
private Node<T> tail; // 尾结点
private int size;
public SingleLinkedList(){
this.head = new Node<T>();
this.tail = new Node<T>();
head.next = tail.next = null;
}
/**
* 从链表尾部开始接集合中的数据
* @param c
* @return
*/
public boolean addAll(Collection<? extends T> c) {
return addAll(size, c);
}
/**
*
* @param index 指定Collection中插入的第一个元素的位置
* @param c
* @return
*/
public boolean addAll(int index, Collection<? extends T> c){
checkIndex(index);
Object[] objs = c.toArray();
int len = objs.length;
if(len==0){
return false;
}
Node<T> successor = (index==size?tail:entry(index));
Node<T> predecessor = (index==0?head:entry(index-1));
for(int i=0; i<len; i++){
Node temp = new Node<T>((T)objs[i], null); //生成目标结点
predecessor.next = temp;
predecessor = temp;
}
predecessor.next = successor;
size += len;
return true;
}
private void checkIndex(int index) {
if(index < 0 || index > size){
throw new IndexOutOfBoundsException();
}
}
private Node<T> entry(int index) {
checkIndex(index);
Node<T> node = head.next;
for(int i=0; i<index; i++){
node = node.next;
}
return node;
}
public boolean add(T o){
return add(size, o);
}
private Node<T> addafter(T o, Node<T> node) {
Node<T> next = node.next;
Node<T> newNode = new Node<T>(o, next);
node.next = newNode;
return newNode;
}
public boolean add(int index , T o){
checkIndex(index);
Node<T> node = new Node<>(o, null);
Node<T> prev;
if(index == 0){
prev = head;
}else{
prev = entry(index - 1);
}
Node<T> next = prev.next;
node.next = next;
prev.next = node;
if(index == size){
this.tail = node;
}
size++;
return true;
}
public T get(int index){
checkIndex(index);
return entry(index).data;
}
public T remove(int index){
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);
}
Node<T> prev = entry(index - 1);
Node<T> target = prev.next;
Node<T> next = target.next;
prev.next = next;
target.next = null;
T t = target.data;
target.data = null;
if(index == size - 1){
this.tail = prev;
}
size--;
return t;
}
public int size(){
return this.size;
}
public void addFirst(T o){
add(0, o);
}
public void addLast(T o){
add(size, o);
}
public T removeFirst(){
return remove(0);
}
public T removeLast(){
return remove(size-1);
}
private static class Node<T>{
T data;
Node<T> next;
Node(T t, Node next){
this.data = t;
this.next = next;
}
Node(){
}
}
/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 从当前链表中中删除在list中出现的元素
* @param list
*/
public void subtract(LinkedList list){
}
/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){
}
/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
* @param min
* @param max
*/
public void removeRange(int min, int max){
}
/**
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
* @param list
*/
public LinkedList intersection(LinkedList list){
return null;
}
public static void main(String[] args){
}
}