update at 2021-04-04 16:31:55 by ehlxr
This commit is contained in:
		| @@ -0,0 +1,171 @@ | ||||
| /* | ||||
|  * The MIT License (MIT) | ||||
|  * | ||||
|  * Copyright © 2020 xrv <xrg@live.com> | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
|  | ||||
| package io.github.ehlxr.datastructure.linkedlist; | ||||
|  | ||||
| /** | ||||
|  * @author ehlxr | ||||
|  * @since 2021-04-04 14:19. | ||||
|  */ | ||||
| public class SingleLinkedListDemo { | ||||
|     public static void main(String[] args) { | ||||
|         HeroNode hero1 = new HeroNode(1, "宋江", "及时雨"); | ||||
|         HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟"); | ||||
|         HeroNode hero3 = new HeroNode(3, "吴用", "智多星"); | ||||
|         HeroNode hero4 = new HeroNode(4, "林冲", "豹子头"); | ||||
|  | ||||
|         //创建要给链表 | ||||
|         SingleLinkedList singleLinkedList = new SingleLinkedList(); | ||||
|  | ||||
|         //加入 | ||||
|         // singleLinkedList.add(hero1); | ||||
|         // singleLinkedList.add(hero4); | ||||
|         // singleLinkedList.add(hero2); | ||||
|         // singleLinkedList.add(hero3); | ||||
|  | ||||
|         singleLinkedList.addByOrder(hero1); | ||||
|         singleLinkedList.addByOrder(hero4); | ||||
|         singleLinkedList.addByOrder(hero2); | ||||
|         singleLinkedList.addByOrder(hero3); | ||||
|  | ||||
|         singleLinkedList.list(); | ||||
|  | ||||
|         HeroNode newHeroNode = new HeroNode(3, "小吴", "智多星.."); | ||||
|         singleLinkedList.update(newHeroNode); | ||||
|         System.out.println("修改后的链表"); | ||||
|         singleLinkedList.list(); | ||||
|  | ||||
|         singleLinkedList.del(3); | ||||
|         System.out.println("删除 no 为 3 的节点"); | ||||
|         singleLinkedList.list(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| class SingleLinkedList { | ||||
|     private final HeroNode head = new HeroNode(0, "", ""); | ||||
|  | ||||
|     public void list() { | ||||
|         HeroNode temp = head.next; | ||||
|         while (temp != null) { | ||||
|             System.out.println(temp); | ||||
|             temp = temp.next; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void add(HeroNode heroNode) { | ||||
|         HeroNode temp = head; | ||||
|         while (temp.next != null) { | ||||
|             temp = temp.next; | ||||
|         } | ||||
|         temp.next = heroNode; | ||||
|     } | ||||
|  | ||||
|     public void addByOrder(HeroNode heroNode) { | ||||
|         HeroNode temp = head; | ||||
|         while (true) { | ||||
|             if (temp.next == null) { | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             // 找到需要插入位置的前一个节点 | ||||
|             if (temp.next.no > heroNode.no) { | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             temp = temp.next; | ||||
|         } | ||||
|  | ||||
|         heroNode.next = temp.next; | ||||
|         temp.next = heroNode; | ||||
|     } | ||||
|  | ||||
|     public void update(HeroNode newHeroNode) { | ||||
|         HeroNode temp = head.next; | ||||
|         if (temp == null) { | ||||
|             System.out.println("链表为空!"); | ||||
|         } | ||||
|  | ||||
|         while (true) { | ||||
|             if (temp == null) { | ||||
|                 System.out.printf("没找到 %d 的节点\n", newHeroNode.no); | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             if (temp.no == newHeroNode.no) { | ||||
|                 temp.name = newHeroNode.name; | ||||
|                 temp.nickname = newHeroNode.nickname; | ||||
|  | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             temp = temp.next; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void del(int no) { | ||||
|         HeroNode temp = head; | ||||
|         if (temp.next == null) { | ||||
|             System.out.println("链表为空!"); | ||||
|         } | ||||
|  | ||||
|         while (true) { | ||||
|             if (temp.next == null) { | ||||
|                 System.out.printf("没找到要删除吃 %d 节点\n", no); | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             // 找到待删除节点的前一个节点 | ||||
|             if (temp.next.no == no) { | ||||
|                 temp.next = temp.next.next; | ||||
|  | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             temp = temp.next; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| // 定义HeroNode , 每个HeroNode 对象就是一个节点 | ||||
| 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; | ||||
|     } | ||||
|  | ||||
|     // 为了显示方法,我们重新 toString | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]"; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -22,7 +22,7 @@ | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| package io.github.ehlxr.datastructure; | ||||
| package io.github.ehlxr.datastructure.queue; | ||||
| 
 | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
| @@ -22,7 +22,7 @@ | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| package io.github.ehlxr.datastructure; | ||||
| package io.github.ehlxr.datastructure.queue; | ||||
| 
 | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
		Reference in New Issue
	
	Block a user