单链表的初步实现---(转置,删除,替换,查找,添加)
2016-08-22 23:39
525 查看
class Link{ //定义链表 private class Node{ //定义保持的节点 private Object data; //要保持的数据,可以用泛型替代 private Node next; //保持下一个节点信息 public Node(Object data){ this.data = data; } public void addNode(Node newNode) { if(this.next == null){ //如果下一个节点为空,则保持节点,如果不为空,则找到空为止 this.next = newNode; }else{ this.next.addNode(newNode); } } public Object getNode(int index) { if(Link.this.flag ++ == index){ //从第一个下标数据开始查询,如果和查询的索引为止一样,则返回当前数据,找不到则继续往后查询 return this.data; }else{ return this.next.getNode(index); } } public void setNode(int index,Object data) { if(Link.this.flag ++ == index){ // 和get方法是一样的,找到和索引相等位置的数据则修改数据 this.data = data; }else{ this.next.setNode(index,data); } } public boolean containsNode(Object data) { if(this.data.equals(data)){ // 从第一个数据开始比对,如果比对到,则返回数据,如果下一个节点还存在则继续比对,如果下个节点不存在则返回false,找到返回true return true; }else{ if(this.next!=null){ return this.next.containsNode(data); } } return false; } public void removeNode(Node node, Object data) { if(this.data.equals(data)){ //找到要删除的数据的节点,比如 1-->2-->3 2 节点找到,则把指向改为1-->3 node.next = this.next; }else{ this.next.removeNode(this, data); //没找到数据则继续往后查询,this表示当前对象 第一次:this.root.next 第二次: this.root.next.next ... } } public void toArrayNode() { Link.this.retArray[Link.this.flag++] = this.data ; //数组保持数据 if(this.next !=null){ this.next.toArrayNode(); } } public Node transposeNode(Node node) { //链表转置 将1-->2 2--->3 变更为 2-->1 3--->2 Node first =node; Node second = node.next; //为转置做准备 Node third = null; while(second!=null){ third = second.next ; //保持2-->3的节点信息 second.next = first; //将1--->2 变成2--->3 first = second; //为下一次替换做准备 second = third; } node.next = null; node = first; return node; } } private Node root = null; //定义根节点 private int count = 0 ; //统计数量 private int flag = 0; //定义下标 private Object[] retArray; //保持数据 public void add(Object data){ //添加数据 if(data == null){ return ; } Node newNode = new Node(data); if(this.root == null){ this.root = newNode; }else{ this.root.addNode(newNode); } this.count++; } public boolean isEmpty(){ //判断是否空 return this.count==0 && this.root==null; } public int size(){ //取得链表长度 return this.count; } public Object get(int index){ //取得指定索引的数据 if(this.isEmpty()){ System.out.println("链表为空,无法取值。"); return null; } if(index >= this.count){ System.out.println("输入的索引位置超过链表长度,请重新输入。"); return null; } this.flag = 0; return this.root.getNode(index); } public void set(int index,Object data){ //将指定索引的数据变更 if(this.isEmpty()){ System.out.println("链表为空,无法取值。"); return ; } if(index >= this.count){ System.out.println("输入的索引位置超过链表长度,请重新输入。"); return ; } this.flag = 0; this.root.setNode(index,data); } public boolean contains(Object data){ //查找数据是否存在 if(data == null){ System.out.println("输入的数据为空,无法查找。"); return false; } if(this.isEmpty()){ System.out.println("链表为空。"); return false; } return this.root.containsNode(data); } public boolean remove(Object data){ //删除数据 if(!(this.contains(data))){ System.out.println("要删除的数据不存在。");//删除前先确认下数据是否存在 return false; } if(this.root.data.equals(data)){ this.root = this.root.next; }else{ this.root.next.removeNode(this.root,data); } this.count--; return true; } public Object[] toArray(){ //保存数据 if(this.isEmpty()){ System.out.println("链表为空。"); return null; } this.retArray = new Object[this.count]; this.flag = 0; this.root.toArrayNode(); return retArray; } public void transpose(){ //链表转置 if(this.isEmpty()){ System.out.println("链表为空。"); return ; } this.root = (this.root.transposeNode(this.root)); } } public class Test{ //定义测试类 public static void main(String args[]){ Link all = new Link(); all.get(1); all.add("数据A"); all.add("数据B"); all.add("数据C"); all.add("数据D"); all.add("数据E"); System.out.println("链表是否为空:"+all.isEmpty()+"链表的长度:"+all.size()); System.out.println("第三个数据是:"+all.get(2)); all.set(2, "数据修改"); System.out.println("修改后的数据是:"+all.get(2)); all.remove("数据修改"); System.out.println("查询数据是否存在:"+all.contains("数据修改")); Object[] obj = all.toArray(); for (int i=0; i<obj.length;i++){ String str = (String)obj[i]; System.out.println(str); } all.transpose(); System.out.println("链表是否为空:"+all.isEmpty()+"链表的长度:"+all.size()); Object[] obj1 = all.toArray(); for (int i=0; i<obj1.length;i++){ String str = (String)obj1[i]; System.out.println(str); } } }
相关文章推荐
- C语言单链表的建立,查找,添加,删除,修改功能实现
- 数据库访问及其基本操作(sql server和添加删除查找替换)
- Java实现数组的添加、删除、查找(基础型)
- 用C语言模拟实现一个通讯录,要求实现其添加、删除、修改、查找、显示和排序联系人信息的功能
- 字典树(前缀树)的实现(添加、删除、查找、查找前缀)
- 实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定
- 单链表的查找、插入和删除运算的实现过程
- c语言实现对传统单链表的创建、添加 遍历 删除 反转元素操作
- java实现单链表的增加,删除,查找,打印
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- 26个字母单链表,实现插入,删除,查找操作
- 单链表实现类2(按关键字查找与删除)
- 如何用SED批量 查找、添加、删除、替换配置文件里面的选项
- C语言实现单链表的生成打印查找删除逆置排序
- 实现列表类,清空、添加、删除、查找、插入,判断元素是否存在
- 数据结构单链表实现插入删除查找
- JavaScript实现添加、查找、删除元素
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- JavaScript实现添加、查找、删除元素
- 用c语言实现一个通讯录; 通讯录可以存储1000个人的信息,信息包括姓名、性别、年龄、电话、住址 提供方法: 1.添加联系人信息 2.删除指定联系人信息 3.查找指定联系人信息 4.修改指定联系&