算法-->查找 (链表)
来源:互联网 发布:数据之魅 编辑:程序博客网 时间:2024/05/29 17:08
package 链表中的查找算法;import java.util.Scanner;public class LinkFind { public static void main(String[] args) { Type node, head = null; String key; Scanner sc = new Scanner(System.in); System.out.print("链表测试!\n 先输入链表中的 数据\n"); do { System.out.print("输入添加的节点:(学号,姓名 年龄)"); Data data = new Data(); data.key = sc.next(); if (data.key.equals("0")) { break; } else { Type t = new Type(); data.name = sc.next(); data.age = sc.nextInt(); head = t.Add(head, data); } } while (true); head.All(head);// 显示所有节点 System.out.print("\n演示在链表中查找,输入查找关键字:"); key = sc.next(); node = head.find(head, key); if (node != null) { Data nodeData = node.Notedata;// 获取 节点的数据 System.out .println("关键字" + key + "对应的节点为:(" + nodeData.key + " " + nodeData.name + " " + nodeData.age + ")"); } else { System.out.print("在链表中未找到关键字为" + key + " 的节点!\n"); } }}class Data { String key; String name; int age;}class Type { Data Notedata = new Data(); Type nextNode; // 追加节点 Type Add(Type head, Data Notedata) { Type node, htemp; node = new Type(); node.Notedata = Notedata;// 保存数据 node.nextNode = null;// 设置节点指针为空,即是表尾 if (head == null) { head = node; return head; } htemp = head; while (htemp.nextNode != null) { htemp = htemp.nextNode; } htemp.nextNode = node; return head; } // 查找节点 Type find(Type head, String key) { Type htemp;// 保存链表头指针 htemp = head; // 如果节点有效 while (htemp != null) { // 若关键子和传入的关键 子相同 if (htemp.Notedata.key.equals(key)) { return htemp; } // 处理下一个节点 htemp = htemp.nextNode; } return null; } // 计算 链表的长度 int length(Type head) { Type htemp; int len = 0; htemp = head; while (htemp != null) { len++; htemp = htemp.nextNode;// 处理下一个节点 } return len;// 返回节点数量 } // 遍历链表 void All(Type head) { Type htemp; Data Notedata; htemp = head; System.out.print("当前链表共有" + length(head) + "个节点,链表所有数据如下:\n"); while (htemp != null) { Notedata = htemp.Notedata;// 获取节点数据 System.out.println("节点(" + Notedata.key + "" + Notedata.name + "" + Notedata.age + ")"); htemp = htemp.nextNode; } }}
阅读全文
0 0
- 算法-->查找 (链表)
- 二分查找算法(折半查找算法)
- 二分查找算法(折半查找算法)
- 折半查找算法(二分查找算法)
- 查找算法(2)--二分查找算法
- 算法(之查找)
- 查找算法(Java)
- 二分查找算法(快速查找)
- java查找算法(一)--顺序查找
- 搜索算法----二分查找(折半查找)
- 查找算法(一)顺序查找
- 算法:二分查找(折半查找)
- 查找算法(基于二叉排序树的查找)
- 实现折半查找算法(二分查找)
- 查找算法(3)--二叉树查找
- 查找算法系列(三):字典查找
- 查找算法(一):顺序查找
- 查找算法(1)折半查找法
- UVa 1664 (Conquer a New Region) 并查集
- Ububtu16.04+GTX1070 环境配置
- Linux学习(一)
- windows——消息——队列化消息 非队列化消息
- Linux C编程学习笔记(5):文件属性操作和目录操作,ls的实现
- 算法-->查找 (链表)
- 中国剩余定理模板
- linkurious.js实现Louvain社区发现算法
- 初入编程世界
- android openGl纹理的使用
- javeBean与xml之间的相互转换
- 如何进行随机抽样?
- DOM4J创建 xml文件
- Android 缓存