单链表之Java实现
初次接触java,用java也写了一个链表。代码如下:
import java.io.*; class Node{
public int data; //数据域
public Node next; //指针域,全局变量可以不用初始化
public Node(int data1){
data = data1;
next = null;
} public void display(){
System.out.println("data: "+this.data);
}
} //定义单链表
class LinkList{
public int pos = 0; //结点的位置
public Node first; //头结点
public LinkList(){
Node first1 = new Node(0);
first = first1;
} //插入结点(从尾部插入结点)
public void addNode(int data){
if(first == null){
Node node = new Node(data);
first = node;
node.next = null;
}
else{
Node node = new Node(data);
Node current = first;
while(current.next != null)
current = current.next;
current.next = node;
node.next = null;
}
} //删除尾部的结点
public Node deleteNode(){
Node current = first;
Node pre = current;
while(current.next != null){
pre = current;
current = current.next;
}
Node temp = pre.next;
pre.next = null;
return temp;
} //在任意位置插入节点
public void insertNode(int index,int data){
Node node = new Node(data);
Node current = first;
while(pos != (index-1)){
current = current.next;
pos++;
}
node.next = current.next;
current.next = node;
pos = 0;
} //删除任意位置的结点
public Node deleteNode(int index){
Node current = first;
while(pos != (index-1)){
current = current.next;
pos++;
}
Node temp = current.next;
current.next = (current.next).next;
pos = 0;
return temp;
} //显示出所有结点信息
public void displayAllNodes(){
Node current = first;
while(current.next != null){
current.display();
current = current.next;
}
} //根据位置查找结点信息
public Node findByPos(int index){
Node current = first;
while(pos != index){
current = current.next;
pos++;
}
pos = 0;
return current;
} //根据数据查找结点信息
public Node findByData(int data){
Node current = first;
while(current.data != data){
if(current.next == null)
return null;
current = current.next;
}
return current;
} } public class test{
public static void main(String[] args){
LinkList link = new LinkList();
link.addNode(1);
link.addNode(2);
link.addNode(3);
link.addNode(4);
link.addNode(5);
link.insertNode(1,1000);
link.displayAllNodes();
}
}
下面是运行结果:
如有错误,欢迎交流指正。
单链表之Java实现的更多相关文章
- 线性表概述及单链表的Java实现
一.线性表概述 线性表是指一组数据元素之间具有线性关系的元素序列,它表现为:除第一个元素没有直接前驱元素.最后一个元素没有直接后继元素外,其余所有元素都有且仅有一个直接前驱元素和直接后继元素. 根据存 ...
- 单链表数据结构 - java简单实现
链表中最简单的一种是单向链表,每个元素包含两个域,值域和指针域,我们把这样的元素称之为节点.每个节点的指针域内有一个指针,指向下一个节点,而最后一个节点则指向一个空值.如图就是一个单向链表 一个单向链 ...
- 单链表反转java代码
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. public class Node { int index; Nod ...
- 使用java实现单链表----(java中的引用就是指针)
//一直以为java中没有指针,其实java的引用就是指针,只不过堆栈中的引用储存了在堆中的地址,可以看做java中的指针.public class sibgleLink<E> { // ...
- 两个有序单链表合并成一个有序单链表的java实现
仅作为备注, 便于自己回顾. import java.util.Arrays; public class MergeSort { public static class LinkedNode<V ...
- 单链表的java实现
class LNode { public LNode next; public int data; } class Lianbiao { private static LNode head = new ...
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- 单链表Java实现
近期在复习基本数据结构,本文是单链表的Java实现,包含对单链表的实现插入删除查找遍历等.最后还实现了单链表的逆置. 实现了多项式相加,多项式相乘. 原文章及完整源码在这里 http://binhua ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
随机推荐
- java中的exception stack有时候不输出的原因
有时候,我们在看java错误日志时,只看到一个java.lang.NullPointerException,却没有看到错误的栈,原因是启动时候有一项参数可以选择配置:OmitStackTraceInF ...
- 怎样通过id属性值获取元素节点
方法1: 使用document.getElementById(); 方法2: 使用document.querySelector(); document.getElementById("app ...
- RestShrap Simple REST and HTTP Client for .NET 了解
最近做一个项目,需要上传文件到文件服务器, 文件服务器是 内部的webapi形式的接口.经朋友推荐使用restshrap , 例子: //上传文件 var request=new RestClient ...
- .NET Core 3.0 发布单文件可执行程序
Windows dotnet publish -r win10-x64 /p:PublishSingleFile=true maxOS dotnet publish -r osx-x64 /p:Pub ...
- YOLOV3 训练WIDER_FACE
1. dowload the img and labels : http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/index.html 2.
- win10下 安装迅雷精简版
下载链接:https://files-cdn.cnblogs.com/files/del88/ThunderMini_1.5.3.288.zip 他妈的 今天安装迅雷精简版 在win10上 竟然报错, ...
- div 清除浮动的四种方法
概述:为了解决父级元素因为子级内部高度为0的问题 (很多情况 不方便给父级元素高,因为不知道有多少内容,让里面的盒子自动撑起高度),清除浮动本质叫闭合浮动更好一些,清除浮动就是把浮动的盒子关到里面,让 ...
- property配置
之前把设备历史数据存储的时间周期存储在了数据库中,因为以下一些原因,我打算改写到property配置文件中 1.这个周期时间的配置没有单独放一个tabel中,导致现在设备类型越来越多,每次添加或者修改 ...
- maskrcnn-benchmark训练自己数据
需要修改的地方 1. ./maskrcnn_benchmark/data/datasets/voc.py 将CLASSES 内容改为自己的数据标签 2. ./maskrcnn_benchmark/co ...
- Flutter——Padding组件
在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属性.这个时候我们可以用 Padding 组件处理容器与子元素直接的间距. ...