使用带head头的单向链表实现—水浒英雄排行榜管理

    //定义HeroNode,每个HeroNode对象就是一个节点
    class HeroNode{
    public int nub;
    public String name;
    public HeroNode next;

    public HeroNode(int nub, String name) {
    this.nub = nub;
    this.name = name;
    }
    @Override
    public String toString() {
    return “HeroNode{“ +
    “nub=” + nub +
    “, name=’” + name + ‘\’’ +
    ‘}’; }
    }

    原理图:
    插入操作
    image.png
    删除操作
    image.png

    Java代码实现:

    1. public class SingleLinkedListDemo {
    2. public static void main(String[] args) {
    3. HeroNode hero1=new HeroNode(1,"aa");
    4. HeroNode hero2=new HeroNode(2,"bb");
    5. HeroNode hero3=new HeroNode(3,"cc");
    6. HeroNode hero4=new HeroNode(4,"dd");
    7. HeroNode hero5=new HeroNode(4,"嗨~");
    8. SingleLinkedList singleLinkedList = new SingleLinkedList();
    9. //加入
    10. /* singleLinkedList.add(hero1);
    11. singleLinkedList.add(hero2);
    12. singleLinkedList.add(hero3);
    13. singleLinkedList.add(hero4);*/
    14. singleLinkedList.addByNum(hero4);
    15. singleLinkedList.addByNum(hero2);
    16. singleLinkedList.addByNum(hero1);
    17. singleLinkedList.addByNum(hero3);
    18. //显示
    19. System.out.println("显示----------------");
    20. singleLinkedList.show();
    21. //查找
    22. System.out.println("查找----------------");
    23. singleLinkedList.findByNum(4);
    24. //修改
    25. System.out.println("修改----------------");
    26. singleLinkedList.updateByNum(hero5);
    27. //翻转
    28. System.out.println("翻转----------------");
    29. singleLinkedList.reverseList(singleLinkedList);
    30. singleLinkedList.show();
    31. /* //删除
    32. System.out.println("删除----------------");
    33. singleLinkedList.deleteByNum(2);*/
    34. }
    35. }
    36. //定义SingleLinkedList一个管理hero
    37. class SingleLinkedList{
    38. //初始化一个头结点
    39. private HeroNode head=new HeroNode(0,"");
    40. //添加单向链表
    41. public void add(HeroNode hero){
    42. //因为头结点不能动,我们需要一个辅助遍历 temp
    43. HeroNode temp=head;
    44. while (true){
    45. //找到链表的最后
    46. if(temp.next==null){
    47. break;
    48. }
    49. //如果没有到最后则后移temp
    50. temp=temp.next;
    51. }
    52. temp.next=hero;
    53. }
    54. //按照编号顺序添加
    55. public void addByNum(HeroNode hero){
    56. //因为头结点不能动,我们需要一个辅助遍历 temp
    57. HeroNode temp=head;
    58. boolean flag=false;//用于判断是否nub重复
    59. while (true){
    60. if(temp.next==null){
    61. break;
    62. }
    63. if(hero.nub<temp.next.nub){
    64. break;
    65. }else if(hero.nub==temp.next.nub){//nub相等
    66. flag=true;
    67. break;
    68. }
    69. temp=temp.next;
    70. }
    71. if(flag){
    72. System.out.println("您添加的编号"+temp.nub+"已重复");
    73. }else {
    74. hero.next=temp.next;
    75. temp.next=hero;
    76. }
    77. }
    78. //按编号查找节点
    79. public void findByNum(int num){
    80. if(head.next==null){
    81. System.out.println("链表为空");
    82. return;
    83. }
    84. //因为头结点不能动,我们需要一个辅助遍历 temp
    85. HeroNode temp=head;
    86. while (true){
    87. if(temp.next==null){
    88. break;
    89. }
    90. if(temp.nub==num){
    91. break;
    92. }
    93. temp=temp.next;
    94. }
    95. if(temp.next==null&&temp.nub!=num){
    96. System.out.println("没找到您查询的信息");
    97. }else {
    98. System.out.println("找到啦!信息为===="+temp.toString());
    99. }
    100. }
    101. //按编号修改节点
    102. public void updateByNum(HeroNode newHero){
    103. if(head.next==null){
    104. System.out.println("链表为空");
    105. return;
    106. }
    107. //因为头结点不能动,我们需要一个辅助遍历 temp
    108. HeroNode temp=head;
    109. while (true){
    110. if(temp.next==null){
    111. break;
    112. }
    113. if(temp.next.nub==newHero.nub){
    114. break;
    115. }
    116. temp=temp.next;
    117. }
    118. if(temp.next==null){
    119. System.out.println("没找到您所要修改的信息");
    120. }else {
    121. temp.next=newHero;
    122. System.out.println("修改成功!新的信息为:"+temp.next.toString());
    123. show();
    124. }
    125. }
    126. //删除节点
    127. public void deleteByNum(int num){
    128. if(head.next==null){
    129. System.out.println("链表为空");
    130. return;
    131. }
    132. HeroNode temp=head;
    133. while (true){
    134. if(temp.next==null){
    135. break;
    136. }
    137. if(temp.next.nub==num){
    138. break;
    139. }
    140. temp=temp.next;
    141. }
    142. temp.next=temp.next.next;
    143. show();
    144. }
    145. //显示数据
    146. public void show(){
    147. //判断链表是否为空
    148. if(head.next==null){
    149. System.out.println("链表为空");
    150. return;
    151. }
    152. //因为头结点不能动,我们需要一个辅助遍历 temp
    153. HeroNode temp=head;
    154. while (true){
    155. //找到链表的最后
    156. if(temp.next==null){
    157. break;
    158. }
    159. //如果没有到最后则后移temp
    160. temp=temp.next;
    161. System.out.println(temp);
    162. }
    163. }
    164. //翻转单链表
    165. public static void reverseList(SingleLinkedList List){//头插法
    166. if(List.head.next==null||List.head.next.next==null){
    167. return;//为空或只有一个节点
    168. }
    169. HeroNode cur=List.head.next;
    170. HeroNode next=null;//保存原链表的next
    171. HeroNode reverseHead=new HeroNode(0,"");
    172. while (cur!=null){
    173. next=cur.next;//先将cur的下一个节点保存
    174. cur.next=reverseHead.next;//将cur的next指向第一个有数据的节点
    175. reverseHead.next=cur;//将第一个有数据的节点换成cur
    176. cur=next;//下移
    177. }
    178. List.head=reverseHead;
    179. }
    180. }
    181. //定义HeroNode,每个HeroNode对象就是一个节点
    182. class HeroNode{
    183. public int nub;
    184. public String name;
    185. public HeroNode next;
    186. public HeroNode(int nub, String name) {
    187. this.nub = nub;
    188. this.name = name;
    189. }
    190. @Override
    191. public String toString() {
    192. return "HeroNode{" +
    193. "nub=" + nub +
    194. ", name='" + name + '\'' +
    195. '}';
    196. }
    197. }