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 ...
随机推荐
- PHP 设计模式阅读清单
社区文章推荐 S.O.L.I.D 面向对象设计和编程(OOD&OOP)笔记 浅谈 Laravel 设计模式 PHP 完整实战 23 种设计模式 Laravel Dependency Injec ...
- (译文)JavaScript基础——JavaScript中的深拷贝
在JavaScript中如何拷贝一个对象? 通过引用调用 function mutate(obj) { obj.a = true; } const obj = {a: false}; mutate(o ...
- 【Spring系列】自己手写一个 SpringMVC 框架
参考文章 一.了解SpringMVC运行流程及九大组件 1.SpringMVC的运行流程 1)用户发送请求至前端控制器DispatcherServlet 2)DispatcherServlet收到请求 ...
- python 归并排序
def merge_sort(alist): if len(alist) <= 1: return alist # 二分分解 num = len(alist)/2 left = merge_so ...
- vue项目结构
前言 我在 搭建vue项目环境 简单说明了项目初始化完成后的目录结构. 但在实际项目中,src目录下的结构需要跟随项目做一些小小的调整. 目录结构 ├── src 项目源码目录 │ ├── api 所 ...
- 织梦dedecms默认网站地图sitemap.html优化
网站地图对于网站优化很重要,搜索引擎就是靠网站地图去收录网站页面,本文主要讲解优化织梦自带的网站地图功能. 织梦自带的网站地图使用方法:织梦后台--生成--HTML更新--更新网站地图,可以在 ...
- 构建自己的 PHP 框架
这是一个系列的文章,项目地址在这里,欢迎大家star. 这个框架前一部分比较像Yii,后一部分比较像Laravel,因为当时正在看相应框架的源码,所以会有不少借鉴参考.捂脸- 这个框架千万不要直接应用 ...
- php的数组的函数
1.可以将一个二位数组转化成两个一维数组,没有指定键就是默认的索引 注意二位数组有几种类型,其中最常见的一种是外层循环是一个索引数组,然后内层是一个关联数组.这种通过便利第一层,然后第二层指定关联词就 ...
- C语言使用vs2013进行编辑
由于vs2013是微软开发的产品所以在windows平台下无限兼容windows所有虽然比较大,但是还是比较值得 但是在运行C程序的遇到问题就是控制台一闪而过通过ctrl+F5执行也是不管用: #in ...
- Web Service的工作原理
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...