通过while循环取出节点内容

  1. class Node{//定义一个节点类,用于保存数据和取得下一个节点
  2. private String data;//节点中数据
  3. private Node next;//下一个节点
  4. public Node(String data){
  5. this.data = data;
  6. }
  7. public void setNext(Node next){
  8. this.next = next;
  9. }
  10. public Node getNext(){
  11. return next;
  12. }
  13. public String toString(){//取出节点数据
  14. return this.data;
  15. }
  16.  
  17. }
  18. public class Test{
  19. public static void main(String args[]){
  20. //1.设置节点内容
  21. Node root = new Node("根节点");
  22. Node node1 = new Node("节点1");
  23. Node node2 = new Node("节点2");
  24. Node node3 = new Node("节点3");
  25. //2.设置节点之间的关系
  26. root.setNext(node1);
  27. node1.setNext(node2);
  28. node2.setNext(node3);
  29. //3.取出节点内容
  30. Node currentNode = root ; //表示当前节点
  31. while(currentNode != null){ //当前节点不为空
  32. System.out.println(currentNode);
  33. currentNode = currentNode.getNext();//将当前节点设置为下一个节点
  34. }
  35. }
  36. }

通过递归调用进行节点的取出

  1. class Node{//定义一个节点类,用于保存数据和取得下一个节点
  2. private String data;//节点中数据
  3. private Node next;//下一个节点
  4. public Node(String data){
  5. this.data = data;
  6. }
  7. public void setNext(Node next){
  8. this.next = next;
  9. }
  10. public Node getNext(){
  11. return next;
  12. }
  13. public String toString(){//取出节点数据
  14. return this.data;
  15. }
  16.  
  17. }
  18. public class Test{
  19. public static void main(String args[]){
  20. //1.设置节点内容
  21. Node root = new Node("根节点");
  22. Node node1 = new Node("节点1");
  23. Node node2 = new Node("节点2");
  24. Node node3 = new Node("节点3");
  25. //2.设置节点之间的关系
  26. root.setNext(node1);
  27. node1.setNext(node2);
  28. node2.setNext(node3);
  29. //3.取出节点内容,
  30. print(root);
  31. }
  32. //定义一个递归调用类
  33. public static void print(Node currentNode){
  34. if(currentNode == null){
  35. return;//结束调用
  36. }
  37. System.out.println(currentNode);
  38. print(currentNode.getNext());
  39. }
  40. }

完善链表客户端调用,简化客户端程序代码

  1. class Node{//定义一个节点类,用于保存数据和取得下一个节点
  2. private String data;//节点中数据
  3. private Node next;//下一个节点
  4. public Node(String data){
  5. this.data = data;
  6. }
  7. //=============================实现节点的添加操作==========================================
  8. public void addNode(Node newNode){
  9. //第一次调用addNode的当前对象 this = this.root
  10. //第二次调用addNode的当前对象 this = this.root.next
  11. if(this.next == null){ // 当前节点下一个节点不存在
  12. this.next = newNode;
  13. }else{ // 下一个节点存在
  14. this.next.addNode(newNode);
  15. }
  16. }
  17. //=============================实现节点的打印==========================================
  18. public void printNode(){
  19. System.out.println(this.data);//输出当前数据
  20. if(this.next != null){
  21. this.next.printNode();
  22. }
  23. }
  24. }
  25. class Link{
  26. private Node root;//表示根节点
  27. //=============================实现节点的添加操作==========================================
  28. public void add(String data){
  29. Node newNode = new Node(data);
  30. if(this.root == null){//根节点不存在
  31. this.root = newNode;
  32. }else{//表示根节点已经存在了
  33. this.root.addNode(newNode);
  34. }
  35. }
  36. //=============================实现节点的打印==========================================
  37. public void print(){
  38. if(this.root != null){
  39. this.root.printNode();
  40. }
  41. }
  42. }
  43. public class Test{
  44. public static void main(String args[]){
  45. Link link = new Link();
  46. link.add("根节点");
  47. link.add("节点1");
  48. link.add("节点2");
  49. link.add("节点3");
  50. link.print();
  51. }
  52. }

通过内部类的方法对Node类进行封装,只可以被Link类所利用;然后实现链表的增删改查等操作;

public void add(Object obj);      数据的增加

public int size();            取得链表长度

public boolean isEmpty();       判断链表是否为空

public void contains(Object obj);    判断某一数据是否存在

public Object get(int index){}      根据索引取得数据

public void set(int index,Obect obj);  修改指定索引内容

public void remove(Object obj);;    删除指定数据

publci Object [] toArray();       将链表以对象数组的形式返回

范例:以下是相对完整的链表结构

  1. //利用内部类对链表进行开发
  2. class Link{
  3. private class Node{
  4. private String data;
  5. private Node next;
  6. public Node(String data){
  7. this.data = data;
  8. }
  9. //==========================1.数据增加方法====================================
  10. public void addNode(Node newNode){
  11. if(this.next == null){ //1.this = this.root 2.this = this.root.next;...
  12. this.next = newNode;
  13. }else{
  14. this.next.addNode(newNode); //递归调用
  15. }
  16. }
  17. //==========================4.判断某个内容是否包含在节点之中====================================
  18. public boolean containsNode(String data){
  19. if(data.equals(this.data)){
  20. return true;
  21. }else{
  22. if(this.next != null){
  23. return this.next.containsNode(data);
  24. }else{
  25. return false;
  26. }
  27. }
  28. }
  29. //==========================5.根据索引取得数据====================================
  30. public String getNode(int index){
  31. if(Link.this.foot++ == index){
  32. return this.data;
  33. }else{
  34. return this.next.getNode(index);
  35. }
  36. }
  37. //==========================6.根据索引替换内容====================================
  38. public void setNode(int index,String data){
  39. if(Link.this.foot++ == index){
  40. this.data = data;
  41. }else{
  42. this.next.setNode(index,data);
  43. }
  44. }
  45. //==========================7.删除指定的数据====================================
  46. public void removeNode(Node preNode,String data){//preNode表示当前节点的前一个节点
  47. if(data.equals(this.data)){
  48. preNode.next = this.next;//当前的节点的上一个
  49. }else{
  50. this.next.removeNode(this,data);
  51. }
  52. }
  53. //==========================8.将链表变为数组====================================
  54. public void toArrayNode(){
  55. Link.this.retArray[Link.this.foot++] = this.data;
  56. if(this.next != null){
  57. this.next.toArrayNode();
  58. }
  59. }
  60. }
  61. //==========================上面显示的是内部类====================================
  62. private Node root;
  63. private int count = 0;//表示链表的长度
  64. private int foot =0;//节点的脚标
  65. private String[] retArray;//表示返回的数组
  66. //==========================1.数据增加方法====================================
  67. public void add(String data){
  68. Node newNode = new Node(data);
  69. if(this.root == null){ //表示根节点对象是一个空对象
  70. this.root = newNode; //实例化根节点对象
  71. }else{ //根节点对象已经实例化
  72. this.root.addNode(newNode);
  73. }
  74. if(data != null){
  75. this.count++;//每一次调用数据count自增一次
  76. }
  77. }
  78. //==========================2.取得链表长度====================================
  79. public int size(){
  80. return count;
  81. }
  82. //==========================3.判断链表是否是空链表====================================
  83. public boolean isEmpty(){
  84. return this.count==0;
  85. }
  86. //==========================4.判断某个内容是否包含在节点之中====================================
  87. public boolean contains(String data){
  88. if(this.root == null||data == null){
  89. return false;
  90. }else{
  91. return this.root.containsNode(data);
  92. }
  93. }
  94. //==========================5.根据索引取得数据====================================
  95. public String get(int index){
  96. this.foot = 0;
  97. if(this.count <= index){
  98. return null;
  99. }else{
  100. return this.root.getNode(index);
  101. }
  102. }
  103. //==========================6.根据索引替换内容====================================
  104. public void set(int index,String data){
  105. this.foot = 0;//重新设置脚标内容
  106. if(this.count <= index){
  107. return ;//结束方法的调用
  108. }else{
  109. this.root.setNode(index,data);
  110. }
  111. }
  112. //==========================7.删除指定的数据====================================
  113. public void remove(String data){
  114. if(this.contains(data)){ //要删除的数据包含在数据里面
  115. if(this.root.data.equals(data)){// 删除的是根节点数据
  116. //根节点要变为原来根节点的下一个节点;
  117. this.root = this.root.next;
  118. }else{ //要删除的不是根节点
  119. this.root.next.removeNode(this.root,data);
  120. }
  121. this.count--;
  122. }
  123. }
  124. //==========================8.将链表变为数组====================================
  125. public String [] toArray(){
  126. if(this.root == null){
  127. return null;
  128. }else{
  129. this.foot = 0;
  130. this.retArray = new String[this.count];
  131. this.root.toArrayNode();
  132. return this.retArray;
  133. }
  134. }
  135. }
  136. public class Test{
  137. public static void main(String args[]){
  138. Link link = new Link();
  139. System.out.println(link.isEmpty());
  140. link.add("根节点");
  141. link.add("节点1");
  142. link.add("节点2");
  143. link.add("节点3");
  144. /*
  145. System.out.println(link.isEmpty());
  146. System.out.println(link.size());
  147. System.out.println(link.contains("节点"));
  148. System.out.println(link.get(3));
  149. link.set(1,"修改节点内容");
  150. System.out.println(link.get(1)); //根节点
  151. */
  152. /*
  153. System.out.println(link.get(1));
  154. System.out.println(link.size());
  155. link.remove("节点1");
  156. System.out.println(link.get(1));
  157. System.out.println(link.size());
  158. */
  159. String data[] = link.toArray();
  160. for(int x=0;x<data.length;x++){
  161. System.out.print(data[x]+"\t");
  162. }
  163.  
  164. }
  165. }

java中的链表编写的更多相关文章

  1. Java中的链表数据结构

    首先,我们来定义一个链表的数据结构,如下: 1 public class Link { 2 private int value; 3 private Link next; 4 public void ...

  2. java中实现链表(转)

    分析: 上述节点具备如下特征: 1. 每个节点由两部分组成(存储信息的字段,存储指向下一个节点的指针) 2. 节点之间有着严格的先后顺序. 3. 单链表节点是一种非线性的结构,在内存中不连续分配空间. ...

  3. Java中回调函数编写

    package XXX.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStr ...

  4. Java中LinkedList的remove方法真的耗时O(1)吗?

    这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...

  5. java算法01 - 链表

    1.链表 在Java中实现链表,每个节点都有一个值,然后把它链接到下一个节点.下面来看一下节点的实现 class Node<E> { private E e; private Node&l ...

  6. Java中的List集合和迭代器

    一.Java中的List集合. 终于有时间来好好整理一下Java中的集合. 首先要讲的就是List集合.Java中List集合主要将两个: 第一个是底层使用数组维护的ArrayList,第二个是底层是 ...

  7. java中如何使用列表数组

    java中如何使用列表数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me 转载链接 https://blog.csdn.net/hgtjcxy/article/details/8183519 ...

  8. 面试大总结:Java搞定面试中的链表题目总结

    package LinkedListSummary; import java.util.HashMap; import java.util.Stack; /** * http://blog.csdn. ...

  9. java中的集合链表

    java中的集合类有很多种,每个都有自己的一些特点,推荐你专门在这方面研究一下,比方Vector,ArrayList,,LinkedList,Hashtable等,其中你问到的链表,是不是指Linke ...

随机推荐

  1. SQL语法语句总结

    一.SQL语句语法 ALTER TABLE ALTER TABLE 用来更新已存在表的结构. ALTER TABLE tablename (ADD|DROP column datatype [NULL ...

  2. backpropagation

    github: https://github.com/mattm/simple-neural-network blog: https://mattmazur.com/2015/03/17/a-step ...

  3. 201621123057 《Java程序设计》第1周学习总结

    1.本周学习总结 .java - - 源程序 .class - - 字节码文件 JVM - - 虚拟机 JRE - - 执行环境 JDK - - 开发工具包 其中,运行的是.class,而非.java ...

  4. JAVAGUI设计步骤

    ①创建容器 首先要创建一个GUI应用程序,需要创建一个用于容纳所有其它GUI组件元素的载体,Java中称为容器.典型的包括窗口(Window).框架(Frame/JFrame).对话框(Dialog/ ...

  5. Android实验报告

    实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...

  6. 最短路算法模板SPFA、disjkstra、Floyd

    朴素SPFA(链表建边) #include <iostream> #include <cstdio> #include <cstring> #include < ...

  7. 关于python爬虫经常要用到的一些Re.正则表达式

    转载:https://blog.csdn.net/skyeyesxy/article/details/50837984 1.正则表达式的常用符号与方法 常用符号:点号,星号,问号与括号(小括号) (. ...

  8. Linq 连接运算符:Concat

    //Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...

  9. 分布式版本控制系统Git的安装及使用

    Git的安装分为客户端安装和服务端安装,鉴于我平时码代码在windows环境下,因此本文客户端安装直接在windows环境,服务端安装在linux环境下(centos). Git客户端安装 客户端下载 ...

  10. 容器化的 DevOps 工作流

    对于 devops 来说,容器技术绝对是我们笑傲江湖的法宝.本文通过一个小 demo 来介绍如何使用容器技术来改进我们的 devops 工作流. devops 的日常工作中难免会有一些繁琐的重复性劳动 ...