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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
| import java.util.Stack;
public class SingleLinkedListDemo { public static void main(String[] args) { SingleLinkedList singleLinkedList = new SingleLinkedList(); HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨"); HeroNode heroNode2 = new HeroNode(3, "卢俊义", "玉麒麟"); HeroNode heroNode3 = new HeroNode(5, "吴用", "智多星"); HeroNode heroNode4 = new HeroNode(7, "公孙胜", "入云龙");
SingleLinkedList singleLinkedList1 = new SingleLinkedList(); HeroNode heroNode5 = new HeroNode(2, "唐僧", "唐三藏"); HeroNode heroNode6 = new HeroNode(4, "孙悟空", "齐天大圣"); HeroNode heroNode7 = new HeroNode(6, "猪八戒", "天蓬元帅"); HeroNode heroNode8 = new HeroNode(8, "沙僧", "卷帘大将");
singleLinkedList.addHeroNodeByOrder(heroNode4); singleLinkedList.addHeroNodeByOrder(heroNode3); singleLinkedList.addHeroNodeByOrder(heroNode2); singleLinkedList.addHeroNodeByOrder(heroNode1); singleLinkedList.list();
singleLinkedList1.addHeroNodeByOrder(heroNode8); singleLinkedList1.addHeroNodeByOrder(heroNode7); singleLinkedList1.addHeroNodeByOrder(heroNode6); singleLinkedList1.addHeroNodeByOrder(heroNode5); singleLinkedList1.list(); System.out.println("================合并两个有序的单链表,合并之后的链表依然有序================"); HeroNode mergeTwoLinkedList = SingleLinkedList.mergeTwoLinkedList(singleLinkedList.getHead(), singleLinkedList1.getHead()); singleLinkedList1.list(mergeTwoLinkedList);
} }
class SingleLinkedList {
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() { return head; }
public void addHeroNode(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; }
public void addHeroNodeByOrder(HeroNode heroNode) { HeroNode temp = head; boolean flag = false; while (true) { if (temp.next == null) { break; } else if (temp.next.no > heroNode.no) { break; } else if (temp.next.no == heroNode.no) { flag = true; break; } temp = temp.next; }
if (flag) { System.out.printf("已有存在%d编号节点,不能插入\n", heroNode.no); } else { heroNode.next = temp.next; temp.next = heroNode; } }
public void updateHeroNode(HeroNode newHeroNode) { HeroNode temp = head; boolean flag = false; while (true) { if (temp.next == null) { break; } if (temp.no == newHeroNode.no) { flag = true; break; } temp = temp.next; }
if (temp.next == null) { System.out.println("链表为空"); }
if (flag) { temp.name = newHeroNode.name; temp.nickName = newHeroNode.nickName; } else { System.out.printf("没有编号%d节点\n", newHeroNode.no); } }
public void delHeroNode(int no) { HeroNode temp = head; boolean flag = false; while (true) { if (temp.next == null) { break; }
if (temp.next.no == no) { flag = true; break; } temp = temp.next; }
if (flag) { temp.next = temp.next.next; } else { System.out.printf("要删除的%d节点不存在\n", no); } }
public void list() {
HeroNode temp = head; if (temp.next == null) { System.out.println("链表为空"); return; }
while (true) { if (temp.next == null) { break; } temp = temp.next; System.out.println(temp); } }
public void list(HeroNode head) { HeroNode temp = head; if (temp.next == null) { System.out.println("链表为空"); return; }
while (true) { if (temp.next == null) { break; } temp = temp.next; System.out.println(temp); } }
public static int getLinkedLength(HeroNode head) { if (head.next == null) { return 0; }
HeroNode cur = head.next; int length = 0; while (cur != null) { length++; cur = cur.next; } return length; }
public static HeroNode getLastIndexHeroNode(HeroNode head, int index) { if (head.next == null) { return null; }
int size = getLinkedLength(head);
if (index <= 0 || index > size) { return null; }
HeroNode cur = head.next; for (int i = 0; i < size - index; i++) { cur = cur.next; } return cur; }
public static void reverseLinkedList(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode reverseHead = new HeroNode(0, "", ""); HeroNode cur = head.next; HeroNode next = null; while (cur != null) { next = cur.next; cur.next = reverseHead.next; reverseHead.next = cur; cur = next; } head.next = reverseHead.next; }
public static void printReverseLinkedList(HeroNode head) { if (head.next == null) { System.out.println("队列为空"); return; } Stack<HeroNode> heroNodeStack = new Stack<>(); HeroNode cur = head.next; while (cur != null) { heroNodeStack.push(cur); cur = cur.next; }
cur = head.next; while (cur != null) { System.out.println(heroNodeStack.pop()); cur = cur.next; } }
public static HeroNode mergeTwoLinkedList(HeroNode head1, HeroNode head2) { if (head1.next == null) { return head2; }
if (head2.next == null) { return head1; }
HeroNode mergeHead = new HeroNode(0, "", "");
HeroNode cur1 = head1.next; HeroNode cur2 = head2.next; HeroNode mergeCur = mergeHead; while (cur1 != null && cur2 != null) { if (cur1.no <= cur2.no) { mergeCur.next = cur1; mergeCur = mergeCur.next; cur1 = cur1.next; } else { mergeCur.next = cur2; mergeCur = mergeCur.next; cur2 = cur2.next; } } if (cur1 == null) { mergeCur.next = cur2; } else { mergeCur.next = cur1; } return mergeHead; } }
class HeroNode { public int no; public String name; public String nickName; public HeroNode next;
public HeroNode(int no, String name, String nickName) { this.no = no; this.name = name; this.nickName = nickName; }
@Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; } }
|