java中的链表编写
通过while循环取出节点内容
- class Node{//定义一个节点类,用于保存数据和取得下一个节点
- private String data;//节点中数据
- private Node next;//下一个节点
- public Node(String data){
- this.data = data;
- }
- public void setNext(Node next){
- this.next = next;
- }
- public Node getNext(){
- return next;
- }
- public String toString(){//取出节点数据
- return this.data;
- }
- }
- public class Test{
- public static void main(String args[]){
- //1.设置节点内容
- Node root = new Node("根节点");
- Node node1 = new Node("节点1");
- Node node2 = new Node("节点2");
- Node node3 = new Node("节点3");
- //2.设置节点之间的关系
- root.setNext(node1);
- node1.setNext(node2);
- node2.setNext(node3);
- //3.取出节点内容
- Node currentNode = root ; //表示当前节点
- while(currentNode != null){ //当前节点不为空
- System.out.println(currentNode);
- currentNode = currentNode.getNext();//将当前节点设置为下一个节点
- }
- }
- }
通过递归调用进行节点的取出
- class Node{//定义一个节点类,用于保存数据和取得下一个节点
- private String data;//节点中数据
- private Node next;//下一个节点
- public Node(String data){
- this.data = data;
- }
- public void setNext(Node next){
- this.next = next;
- }
- public Node getNext(){
- return next;
- }
- public String toString(){//取出节点数据
- return this.data;
- }
- }
- public class Test{
- public static void main(String args[]){
- //1.设置节点内容
- Node root = new Node("根节点");
- Node node1 = new Node("节点1");
- Node node2 = new Node("节点2");
- Node node3 = new Node("节点3");
- //2.设置节点之间的关系
- root.setNext(node1);
- node1.setNext(node2);
- node2.setNext(node3);
- //3.取出节点内容,
- print(root);
- }
- //定义一个递归调用类
- public static void print(Node currentNode){
- if(currentNode == null){
- return;//结束调用
- }
- System.out.println(currentNode);
- print(currentNode.getNext());
- }
- }
完善链表客户端调用,简化客户端程序代码
- class Node{//定义一个节点类,用于保存数据和取得下一个节点
- private String data;//节点中数据
- private Node next;//下一个节点
- public Node(String data){
- this.data = data;
- }
- //=============================实现节点的添加操作==========================================
- public void addNode(Node newNode){
- //第一次调用addNode的当前对象 this = this.root
- //第二次调用addNode的当前对象 this = this.root.next
- if(this.next == null){ // 当前节点下一个节点不存在
- this.next = newNode;
- }else{ // 下一个节点存在
- this.next.addNode(newNode);
- }
- }
- //=============================实现节点的打印==========================================
- public void printNode(){
- System.out.println(this.data);//输出当前数据
- if(this.next != null){
- this.next.printNode();
- }
- }
- }
- class Link{
- private Node root;//表示根节点
- //=============================实现节点的添加操作==========================================
- public void add(String data){
- Node newNode = new Node(data);
- if(this.root == null){//根节点不存在
- this.root = newNode;
- }else{//表示根节点已经存在了
- this.root.addNode(newNode);
- }
- }
- //=============================实现节点的打印==========================================
- public void print(){
- if(this.root != null){
- this.root.printNode();
- }
- }
- }
- public class Test{
- public static void main(String args[]){
- Link link = new Link();
- link.add("根节点");
- link.add("节点1");
- link.add("节点2");
- link.add("节点3");
- link.print();
- }
- }
通过内部类的方法对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(); 将链表以对象数组的形式返回
范例:以下是相对完整的链表结构
- //利用内部类对链表进行开发
- class Link{
- private class Node{
- private String data;
- private Node next;
- public Node(String data){
- this.data = data;
- }
- //==========================1.数据增加方法====================================
- public void addNode(Node newNode){
- if(this.next == null){ //1.this = this.root 2.this = this.root.next;...
- this.next = newNode;
- }else{
- this.next.addNode(newNode); //递归调用
- }
- }
- //==========================4.判断某个内容是否包含在节点之中====================================
- public boolean containsNode(String data){
- if(data.equals(this.data)){
- return true;
- }else{
- if(this.next != null){
- return this.next.containsNode(data);
- }else{
- return false;
- }
- }
- }
- //==========================5.根据索引取得数据====================================
- public String getNode(int index){
- if(Link.this.foot++ == index){
- return this.data;
- }else{
- return this.next.getNode(index);
- }
- }
- //==========================6.根据索引替换内容====================================
- public void setNode(int index,String data){
- if(Link.this.foot++ == index){
- this.data = data;
- }else{
- this.next.setNode(index,data);
- }
- }
- //==========================7.删除指定的数据====================================
- public void removeNode(Node preNode,String data){//preNode表示当前节点的前一个节点
- if(data.equals(this.data)){
- preNode.next = this.next;//当前的节点的上一个
- }else{
- this.next.removeNode(this,data);
- }
- }
- //==========================8.将链表变为数组====================================
- public void toArrayNode(){
- Link.this.retArray[Link.this.foot++] = this.data;
- if(this.next != null){
- this.next.toArrayNode();
- }
- }
- }
- //==========================上面显示的是内部类====================================
- private Node root;
- private int count = 0;//表示链表的长度
- private int foot =0;//节点的脚标
- private String[] retArray;//表示返回的数组
- //==========================1.数据增加方法====================================
- public void add(String data){
- Node newNode = new Node(data);
- if(this.root == null){ //表示根节点对象是一个空对象
- this.root = newNode; //实例化根节点对象
- }else{ //根节点对象已经实例化
- this.root.addNode(newNode);
- }
- if(data != null){
- this.count++;//每一次调用数据count自增一次
- }
- }
- //==========================2.取得链表长度====================================
- public int size(){
- return count;
- }
- //==========================3.判断链表是否是空链表====================================
- public boolean isEmpty(){
- return this.count==0;
- }
- //==========================4.判断某个内容是否包含在节点之中====================================
- public boolean contains(String data){
- if(this.root == null||data == null){
- return false;
- }else{
- return this.root.containsNode(data);
- }
- }
- //==========================5.根据索引取得数据====================================
- public String get(int index){
- this.foot = 0;
- if(this.count <= index){
- return null;
- }else{
- return this.root.getNode(index);
- }
- }
- //==========================6.根据索引替换内容====================================
- public void set(int index,String data){
- this.foot = 0;//重新设置脚标内容
- if(this.count <= index){
- return ;//结束方法的调用
- }else{
- this.root.setNode(index,data);
- }
- }
- //==========================7.删除指定的数据====================================
- public void remove(String data){
- if(this.contains(data)){ //要删除的数据包含在数据里面
- if(this.root.data.equals(data)){// 删除的是根节点数据
- //根节点要变为原来根节点的下一个节点;
- this.root = this.root.next;
- }else{ //要删除的不是根节点
- this.root.next.removeNode(this.root,data);
- }
- this.count--;
- }
- }
- //==========================8.将链表变为数组====================================
- public String [] toArray(){
- if(this.root == null){
- return null;
- }else{
- this.foot = 0;
- this.retArray = new String[this.count];
- this.root.toArrayNode();
- return this.retArray;
- }
- }
- }
- public class Test{
- public static void main(String args[]){
- Link link = new Link();
- System.out.println(link.isEmpty());
- link.add("根节点");
- link.add("节点1");
- link.add("节点2");
- link.add("节点3");
- /*
- System.out.println(link.isEmpty());
- System.out.println(link.size());
- System.out.println(link.contains("节点"));
- System.out.println(link.get(3));
- link.set(1,"修改节点内容");
- System.out.println(link.get(1)); //根节点
- */
- /*
- System.out.println(link.get(1));
- System.out.println(link.size());
- link.remove("节点1");
- System.out.println(link.get(1));
- System.out.println(link.size());
- */
- String data[] = link.toArray();
- for(int x=0;x<data.length;x++){
- System.out.print(data[x]+"\t");
- }
- }
- }
java中的链表编写的更多相关文章
- Java中的链表数据结构
首先,我们来定义一个链表的数据结构,如下: 1 public class Link { 2 private int value; 3 private Link next; 4 public void ...
- java中实现链表(转)
分析: 上述节点具备如下特征: 1. 每个节点由两部分组成(存储信息的字段,存储指向下一个节点的指针) 2. 节点之间有着严格的先后顺序. 3. 单链表节点是一种非线性的结构,在内存中不连续分配空间. ...
- Java中回调函数编写
package XXX.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStr ...
- Java中LinkedList的remove方法真的耗时O(1)吗?
这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...
- java算法01 - 链表
1.链表 在Java中实现链表,每个节点都有一个值,然后把它链接到下一个节点.下面来看一下节点的实现 class Node<E> { private E e; private Node&l ...
- Java中的List集合和迭代器
一.Java中的List集合. 终于有时间来好好整理一下Java中的集合. 首先要讲的就是List集合.Java中List集合主要将两个: 第一个是底层使用数组维护的ArrayList,第二个是底层是 ...
- java中如何使用列表数组
java中如何使用列表数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me 转载链接 https://blog.csdn.net/hgtjcxy/article/details/8183519 ...
- 面试大总结:Java搞定面试中的链表题目总结
package LinkedListSummary; import java.util.HashMap; import java.util.Stack; /** * http://blog.csdn. ...
- java中的集合链表
java中的集合类有很多种,每个都有自己的一些特点,推荐你专门在这方面研究一下,比方Vector,ArrayList,,LinkedList,Hashtable等,其中你问到的链表,是不是指Linke ...
随机推荐
- SQL语法语句总结
一.SQL语句语法 ALTER TABLE ALTER TABLE 用来更新已存在表的结构. ALTER TABLE tablename (ADD|DROP column datatype [NULL ...
- backpropagation
github: https://github.com/mattm/simple-neural-network blog: https://mattmazur.com/2015/03/17/a-step ...
- 201621123057 《Java程序设计》第1周学习总结
1.本周学习总结 .java - - 源程序 .class - - 字节码文件 JVM - - 虚拟机 JRE - - 执行环境 JDK - - 开发工具包 其中,运行的是.class,而非.java ...
- JAVAGUI设计步骤
①创建容器 首先要创建一个GUI应用程序,需要创建一个用于容纳所有其它GUI组件元素的载体,Java中称为容器.典型的包括窗口(Window).框架(Frame/JFrame).对话框(Dialog/ ...
- Android实验报告
实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...
- 最短路算法模板SPFA、disjkstra、Floyd
朴素SPFA(链表建边) #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 关于python爬虫经常要用到的一些Re.正则表达式
转载:https://blog.csdn.net/skyeyesxy/article/details/50837984 1.正则表达式的常用符号与方法 常用符号:点号,星号,问号与括号(小括号) (. ...
- Linq 连接运算符:Concat
//Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...
- 分布式版本控制系统Git的安装及使用
Git的安装分为客户端安装和服务端安装,鉴于我平时码代码在windows环境下,因此本文客户端安装直接在windows环境,服务端安装在linux环境下(centos). Git客户端安装 客户端下载 ...
- 容器化的 DevOps 工作流
对于 devops 来说,容器技术绝对是我们笑傲江湖的法宝.本文通过一个小 demo 来介绍如何使用容器技术来改进我们的 devops 工作流. devops 的日常工作中难免会有一些繁琐的重复性劳动 ...