
java单链表实现+模拟水浒英雄排名
发布日期:2021-05-25 17:46:08
浏览次数:22
分类:精选文章
本文共 4556 字,大约阅读时间需要 15 分钟。
单链表实现水浒英雄排行榜管理系统
项目背景
为了实现一个简单但实用的水浒英雄排行榜管理系统,我们选择使用单链表数据结构。单链表的特点是单向,且只有一头节点(头节点不存储数据,只作为链表的标志),尾节点可以通过遍历找到。在实现过程中,我们需要支持入库、修改、删除以及查询操作。
技术选择与实现细节
1. 单链表结构设计
单链表的实现需要一个默认的HeroNode
头节点,其属性包括排名、姓名和外号。每个节点包含一个指向下一个节点的指针。
2. 节点管理方法
- 添加节点:根据排名将节点插入到指定位置,或直接添加到链表的尾部。
- 删除节点:通过遍历找到目标节点,并删除其指针。
- 修改节点:基于查找功能,先找到目标节点再更新其属性值。
- 查找节点:可以通过遍历找到节点信息,如直接返回节点或倒数第k个节点。
3. 实现代码示例
HeroNode类定义:
class HeroNode { public int no; // 防木 Consultant 编号 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; this.next = null; } public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + "', nickname='" + nickname + "'}"; }}
SingleLinkedList类实现:
class SingleLinkedList { private HeroNode head = new HeroNode(0, "", ""); // 头节点不存储数据 // 添加节点按顺序插入 public void add(HeroNode newNode) { HeroNode temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } // 按排名添加节点 public void addByOrder(HeroNode newNode) { HeroNode temp = head; while (temp != null && temp.no < newNode.no) { temp = temp.next; } if (temp != null && temp.no == newNode.no) { return; // 该排名已存在 } HeroNode prev = temp; newNode.next = temp; temp = prev; temp.next = newNode; } // 删除指定排名的节点 public void del(int no) { HeroNode temp = head; while (temp != null && temp.next != null && temp.next.no != no) { temp = temp.next; } if (temp != null) { HeroNode delNode = temp.next; temp.next = delNode.next; } } // 列表遍历 public void list() { HeroNode temp = head.next; // 遍历的起始点是第二个节点 while (temp != null) { System.out.println(temp); temp = temp.next; } } // 获取节点总数 public static int getNodeCount(HeroNode head) { HeroNode temp = head.next; int count = 0; while (temp != null) { count++; temp = temp.next; } return count; } // 查找指定排名的节点 public HeroNode findHero(int no) { HeroNode temp = head.next; while (temp != null && temp.no != no) { temp = temp.next; } return temp; } // 删除指定排名的节点 public void delByNo(int no) { del(no); } // 反转链表 public static void reverse(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode originalHead = head; HeroNode reverseHead = new HeroNode(0, "", ""); HeroNode temp = head.next; while (temp != null) { reverseHead.next = temp; temp = temp.next; reverseHead = reverseHead.next; } head.next = reverseHead.next; reverseHead.next = null; } // 逆序打印链表内容 public void reversePrint(HeroNode head) { Stackstack = new Stack<>(); HeroNode temp = head.next; while (temp != null) { stack.add(temp); temp = temp.next; } while (stack != null) { System.out.println(stack.pop()); } }}
测试示例
public static void main(String[] args) { SingleLinkedList singleLinkedList = new SingleLinkedList(); HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨"); HeroNode heroNode2 = new HeroNode(2, "吴用", "智多星"); HeroNode heroNode3 = new HeroNode(3, "林冲", "豹子头"); HeroNode heroNode4 = new HeroNode(4, "李逵", "黑旋风"); singleLinkedList.add(heroNode1); singleLinkedList.add(heroNode3); singleLinkedList.add(heroNode2); singleLinkedList.add(heroNode4); System.out.println("初始列表:"); singleLinkedList.list(); System.out.println("删除排名为1的英雄"); singleLinkedList.del(1); singleLinkedList.list(); System.out.println("查找排名为2的英雄"); HeroNode foundHero = singleLinkedList.findHero(2); System.out.println("结果:" + foundHero); // 反转链表 System.out.println("反转前:"); singleLinkedList.list(); reverse(singleLinkedList.getHead()); System.out.println("反转后:"); singleLinkedList.list(); // 逆序打印 System.out.println("逆序打印结果:"); singleLinkedList.reversePrint(singleLinkedList.getHead());}
尽管我们已经实现了一个基础的链表数据结构,具体应用可能需要根据实际需求进行优化和扩展。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年05月04日 21时01分51秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
多代理区块链框架客户端的操作
2019-03-13
一些技术博客
2019-03-13
第01问:MySQL 一次 insert 刷几次盘?
2019-03-13
libvirtd:内部错误:Failed to apply firewall rule
2019-03-13
优先级队列2
2019-03-13
TiKV 源码解析系列文章(十三)MVCC 数据读取
2019-03-13
Android 开发常用的工具类(更新ing)
2019-03-13
EasyUI的简单介绍
2019-03-13
初次安装webpack之后,提示安装webpack-cli
2019-03-13
Hbase压力测试
2019-03-14
C#中的类、方法和属性
2019-03-14
Python爬虫训练:爬取酷燃网视频数据
2019-03-14
Python数据分析入门(十九):绘制散点图
2019-03-14
Callable中call方法和Runnable中run方法的区别
2019-03-14
Linux yum提示Loaded plugins错误的解决方法
2019-03-14
Netty的体系结构及使用
2019-03-14
xshell解决文本粘贴格式错误
2019-03-14
网络+图片加载框架(英文版)
2019-03-14