-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhw2.java
More file actions
287 lines (223 loc) · 8.91 KB
/
Copy pathhw2.java
File metadata and controls
287 lines (223 loc) · 8.91 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.util.Random;
public class hw2 {
private static class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
public class DoublyLinkedList {
// 定义双向链表节点
public Node head;
public Node trailer;
public DoublyLinkedList(int l,int r){
head = new Node(l);
trailer = new Node(r);
}
public static Node findMiddle(Node header, Node trailer) {
Node slow = header.next;
Node fast = header.next;
while (fast != trailer && fast.next != trailer) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
public class CircularlyLinkedList<E> {
// 嵌套的节点类,与 SinglyLinkedList 类的节点类相同
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E element, Node<E> next) {
this.element = element;
this.next = next;
}
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
private Node<E> tail = null; // 尾部节点,不存储头部节点
private int size = 0; // 链表中的节点数
public CircularlyLinkedList() { } // 构造一个初始为空的链表
// 访问方法
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public E first() {
// 返回(但不移除)第一个元素
if (isEmpty()) return null;
return tail.getNext().getElement(); // 头部在尾部的后面
}
public E last() {
// 返回(但不移除)最后一个元素
if (isEmpty()) return null;
return tail.getElement();
}
// 更新方法
public void rotate() {
// 将第一个元素旋转到链表末尾
if (tail != null) // 如果为空,不做任何操作
tail = tail.getNext(); // 旧头部变为新尾部
}
public void addFirst(E e) {
// 在链表前端添加元素
if (size == 0) {
tail = new Node<>(e, null);
tail.setNext(tail); // 形成循环链接到自身
} else {
Node<E> newest = new Node<>(e, tail.getNext());
tail.setNext(newest);
}
size++;
}
public void addLast(E e) {
// 在链表末尾添加元素
addFirst(e); // 在链表前端插入新元素
tail = tail.getNext(); // 现在新元素变为尾部
}
public E removeFirst() {
// 移除并返回第一个元素
if (isEmpty()) return null; // 没有要移除的内容
Node<E> head = tail.getNext();
if (head == tail) tail = null; // 必须是最后一个节点
else tail.setNext(head.getNext()); // 从链表中移除"头部"
size--;
return head.getElement();
}
public boolean equals(Object obj) {
// 检查是否是CircularlyLinkedList类型的对象
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
// 将对象转换为CircularlyLinkedList
CircularlyLinkedList<?> otherList = (CircularlyLinkedList<?>) obj;
// 检查两个链表的大小是否相等
if (size != otherList.size) return false;
// 检查链表中对应位置的元素是否相等
Node<E> currentThis = tail.getNext();
Node<E> currentOther = (Node<E>) otherList.tail.getNext();
for (int i = 0; i < size; i++) {
if (!currentThis.getElement().equals(currentOther.getElement())) {
return false;
}
currentThis = currentThis.getNext();
currentOther = currentOther.getNext();
}
return true;
}
public CircularlyLinkedList<E> clone() {
try {
// 调用父类的 clone 方法创建一个新的 CircularlyLinkedList 对象
CircularlyLinkedList<E> clonedList = (CircularlyLinkedList<E>) super.clone();
if (size > 0) {
// 克隆头部节点
clonedList.tail = new Node<>(tail.getElement(), null);
clonedList.tail.setNext(clonedList.tail);
// 克隆链表中的其他节点
Node<E> current = tail.getNext();
Node<E> clonedCurrent = clonedList.tail.getNext();
while (current != tail) {
Node<E> newNode = new Node<>(current.getElement(), null);
clonedCurrent.setNext(newNode);
clonedCurrent = newNode;
current = current.getNext();
}
// 将克隆链表的尾部指向新添加的尾部节点
clonedList.tail = clonedCurrent;
clonedList.tail.setNext(clonedList.tail);
}
return clonedList;
} catch (CloneNotSupportedException e) {
// 捕获异常并处理
return null;
}
}
public static String encrypt(String message, int shift) {
StringBuilder encryptedMessage = new StringBuilder();
for (char ch : message.toCharArray()) {
if (Character.isUpperCase(ch)) {
// 大写字母的加密
char encryptedChar = (char) ((ch - 'A' + shift) % 26 + 'A');
encryptedMessage.append(encryptedChar);
} else if (Character.isLowerCase(ch)) {
// 小写字母的加密
char encryptedChar = (char) ((ch - 'a' + shift) % 26 + 'a');
encryptedMessage.append(encryptedChar);
} else {
// 非字母字符保持不变
encryptedMessage.append(ch);
}
}
return encryptedMessage.toString();
}
public static String decrypt(String encryptedMessage, int shift) {
// 解密即加密的逆过程,将移位值取反
return encrypt(encryptedMessage, -shift);
}
}
public static void shuffle(int[] A) {
Random rand = new Random();
for (int i = A.length - 1; i > 0; i--) {
// 生成一个介于 0 和 i(含)之间的随机索引
int j = rand.nextInt(i + 1);
// 将当前元素与随机选择的元素交换
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
public class DoublyLinkedListConcatenation {
// 定义双向链表节点
public static class Node {
Node prev;
int val;
Node next;
}
// 定义带有头尾哨兵节点的双向链表类
private static class DoublyLinkedList {
// 头部哨兵节点
Node header;
// 尾部哨兵节点
Node trailer;
public DoublyLinkedList(Node header, Node trailer) {
this.header=header;
this.trailer=trailer;
}
// 连接两个双向链表的方法
public static DoublyLinkedList concatenate(DoublyLinkedList L, DoublyLinkedList M) {
// 如果其中一个链表为空,返回另一个链表
if (L==null) {
return M;
} else if (M==null) {
return L;
}
// 获取 L 的尾部哨兵节点 L_trailer 和 M 的头部哨兵节点 M_header
Node L_trailer = L.trailer;
Node M_header = M.header;
// 将 L_trailer 的 next 指向 M_header.next,即将链表 L 的尾部连接到链表 M 的头部
L_trailer.next = M_header.next;
// 如果 M 不为空,则将 M 的尾部哨兵节点 M_trailer 的 next 指向 L_trailer.next
if (M!=null) {
Node M_trailer = M.trailer;
M_trailer.next = L_trailer.next;
}
// 返回合并后的链表
return new DoublyLinkedList(L.header, M.trailer);
}
// 判断链表是否为空的方法
}
}
}