链表:依靠引用传递关系实现多个数据保存。

链表的常规实现:

1 public class Node {
2     String data;
3     Node next;
4     public Node(String data){
5         this.data = data;
6     }
7 }

实现增、删、查、改:

为了对链表实现保护,将链表进行内部私有化实现:

增加数据:

count : 增加数据的标记

foot : 遍历链表的角标

    private class Node {private String data;// save dataprivate Node next;// Save the data of the next nodepublic Node(String data) {// Contractorthis.data = data;}public void addNode(Node temp) {// add node by recursiveif (this.next == null)this.next = temp;elsethis.next.addNode(temp);}}private Node root;private int count = 0;private int foot;// index of nodepublic void add(String str) {Node node = new Node(str);// crate new nodeif (this.root == null)this.root = node;// root no node existselsethis.root.addNode(node);// root node existscount++;}public void print() {if (count == 0)System.out.println("Current List is null");Node temp = root;while (temp != null) {System.out.print(temp.data + " ");temp = temp.next;}}

测试:

我的外部类为ListNode

// for testpublic static void main(String[] args) {ListNode listNode = new ListNode();listNode.add("a");listNode.add("b");listNode.add("c");listNode.add("d");listNode.print();}

输出为:a b c d

链表的长度:

当前列表的长度和是否为空都可以通过count进行判断(在ListNode内)。

    public int getLength() {return count;}public boolean isEmpty() {return count == 0;}

判断数据是否存在:

判断数据是否存在需要到Node类里面去查找。

在ListNode内:

    public boolean contains(String str) {if (str == null || count == 0)return false;return root.containsNode(str);}

在Node类里面:

        public boolean containsNode(String str) {if (str.equals(this.data))// The data of the current node is the data to be found.return true;// base caseif (this.next != null)// Find in subsequent nodesreturn this.next.containsNode(str);return false;}

取得链表中index位置的数据(重要):

在ListNode内:

    public String getData(int index) {if (count < index)return null;foot = 0;return root.getNodeData(index);}

在Node类里面:

        public String getNodeData(int index) {if (ListNode.this.foot++ == index)// The current node is the targetreturn this.data;// base casereturn this.next.getNodeData(index);// Find in subsequent nodes}

测试:

// for testpublic static void main(String[] args) {ListNode listNode = new ListNode();listNode.add("a");listNode.add("b");listNode.add("c");listNode.add("d");System.out.println(listNode.getLength());System.out.println(listNode.getData(3));System.out.println(listNode.contains("a"));System.out.println(listNode.isEmpty());listNode.print();}

输出:

4
d
true
false
a b c d

变更数据:

变更数据和之前的查询操作类似。

在ListNode内:

    public void setData(int index, String data) {if(this.count < index){System.out.println("Exceeding the length of List!");return;}this.foot = 0;this.root.setDataNode(index, data);}

在Node内:

        public void setDataNode(int index, String data) {if (ListNode.this.foot++ == index)// The current node is the targetthis.data = data;// base caseelse// importantthis.next.setDataNode(index, data);}

删除在index出的数据:

在Node内:

        public void removeIndexNode(Node pervious, int index){if (ListNode.this.foot++ == index){// The current node is the targetpervious.next = this.next;// base case
            }else{this.next.removeIndexNode(this, index);}}

在ListNode内:

    public void removeIndex(int index){if(this.count < index){System.out.println("Exceeding the length of List!");return;}if(index == 0){// remove top nodethis.root = this.root.next;// Clear current nodereturn;}this.foot = 1;// Judging from the second elementthis.root.next.removeIndexNode(this.root, index);this.count--;}

注意,起初在这儿的时候卡了,传进去原链表才能对其进行修改。如果是按照数据进行删除数据类似的写就行。

链表转为数组:

在ListNode内:

    public String[] toArray(){String[] strings = new String[count];if (this.count == 0){System.out.println("Current List is null");return null;}Node temp = root;int i = 0;while (temp != null) {strings[i] = temp.data;i++;
//            System.out.print(temp.data + " ");temp = temp.next;}return strings;}

链表转数组和打印链表类似。

测试:

// for testpublic static void main(String[] args) {ListNode listNode = new ListNode();for (int i = 0; i <= 5; i++)listNode.add(i + "");listNode.print();System.out.println();listNode.setData(1, "7");listNode.print();System.out.println();listNode.removeIndex(2);listNode.print();System.out.println();String[] strings = listNode.toArray();System.out.println(Arrays.toString(strings));}

输出:

0 1 2 3 4 5
0 7 2 3 4 5
0 2 3 4 5
[0, 2, 3, 4, 5]

转载于:https://www.cnblogs.com/fengxilee/p/9372586.html

java的实现内部类实现链表相关推荐

  1. 数据结构之链表及其Java实现_数据结构之链表及其Java实现

    数据的存储一般分线性存储结构和链式存储结构两种.前者是一种顺序的存储方式,在内存中用一块连续的内存空间存储数据,即逻辑上相连的物理位置相邻,比较常见的就是数组:后者是一种链式存储方式,不保证顺序性,逻 ...

  2. java 为什么有匿名类_全面了解Java中的内部类和匿名类

    Java内部类(Inner Class),类似的概念在C++里也有,那就是嵌套类(Nested Class),乍看上去内部类似乎有些多余,它的用处对于初学者来说可能并不是那么显著,但是随着对它的深入了 ...

  3. Java学习笔记二十:Java中的内部类

    Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...

  4. Java Inner Class 内部类

    内部类  Inner Class 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Java中的内部类共分为四种: 静态内部类static inner class ( ...

  5. Java核心类库——内部类那点事儿

    内部类 ---定义在类的内部的类 为什么需要内部类? 典型的情况是,内部类继承自某个类或实现某个接口,内部类的代码操作创建其的外围类的对象.所以你可以认为内部类提供了某种进入其外围类的窗口. java ...

  6. 自学java 第十章内部类(一)

    今天学习了一部分java中的内部类. 内部类指将一个类的定义放在另一个类的内部,那它就是一个内部类. 指明一个引用时内部类的类型可以通过"外部类名.内部类名"的写法声明,如果想返回 ...

  7. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档. Java Nested Classes Reference From Oracle Documentation 嵌套类-Nested Classes 在 ...

  8. Java版数据结构之单向链表 新增,有序新增的两种方式,修改和删除(CRUD)

    Java版数据结构之单向链表 CRUD Java版数据结构之单向链表 新增,有序新增的两种方式,修改和删除; 留了一个疑问; 我的代码仓库:https://github.com/zhuangbinan ...

  9. Java版数据结构之单向链表

    Java版数据结构之单向链表 我的代码仓库:https://github.com/zhuangbinan/datastructure package club.zhuangbinan.linkedli ...

最新文章

  1. 基于扰动观测器的非线性系统控制
  2. 第九期直播|《深度相机与应用》精彩回顾
  3. 基于注解的 IOC 配置——创建对象(Component、Controller、Service、Repository)注入数据(Autowired、Qualifier、Resource、Value)
  4. To Java程序员:切勿用普通for循环遍历LinkedList
  5. Git cherry-pick 使用总结
  6. 图解全排列问题_一道笔试题(122345求有条件全排列)的两种做法
  7. PLsql的汉化工具
  8. 华为编程规范_华为 Java 编程规范出炉,究竟和官方文档有何不同?
  9. VsCode git报错 git add -A -- xxx is outside repository
  10. 关于HTTPS的几个问题
  11. cuSPAESE库:(五)零基索引和一基索引
  12. 花书+吴恩达深度学习(十二)卷积神经网络 CNN 之全连接层
  13. 第一章,安装 composer
  14. JAVA毕业设计高校实习实训管理系统计算机源码+lw文档+系统+调试部署+数据库
  15. 三节锂电池充电芯片,IC设计模块的几种电路
  16. 环境保护设施运营组织服务认证 认证专业分类及运营设施范围
  17. 介绍identity matrices
  18. 攻防世界(练习小题)
  19. 一款简洁的导航网源码
  20. 中国工程院院士高文ICTC演讲《国家新一代人工智能发展规划》

热门文章

  1. Spring Boot 导出Excel表格
  2. leetcode 61. Rotate List
  3. paping使用来测试联通网站由于tcp协议导致的无法通信问题超时问题
  4. Perl重命名当前目录下的文件
  5. Hibernate框架之HQL查询与Criteria 查询的区别
  6. html5 多文件选择
  7. (28)FPGA面试题寄生效应
  8. FPGA系统时间戳偶尔异常分析及定位
  9. git 创建webpack项目_使用webpack手动创建一个完整项目的全过程
  10. 14002.petalinux编译配置项目