单链表之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实 ...
随机推荐
- 【规律】A Rational Sequence
题目描述 An infinite full binary tree labeled by positive rational numbers is defi ned by:• The label of ...
- No database provider has been configured for this DbContext
var context = ((IInfrastructure<IServiceProvider>)set).GetService<DbContext>(); 在EF Core ...
- linux 下使用opengl的glut库显示和旋转BMP图片
效果图: 这里显示的图和原图有明显的色差,目前猜测是opengl渲染时的颜色表顺序跟BMP文件里的颜色表顺序相反导致. BMP里应该是BGRBGRBRG... ,而opengl渲染时应该是按照RGBR ...
- 【loj#2133 && luoguP2178】[NOI2015]品酒大会
题目传送门:loj#2133 luoguP2178 简要题意:给定一个字符串\(s\),每个后缀都有权值,对于每个长度\(len\),求出所有最长公共前缀\(\geq len\)的后缀对的总数 ...
- MYSQL 遇见各种有意思题库
1 使用sql查询每个学生a_id最常借图书类型u_id.表名:t1 (学生图书借阅) [问题分析,1 先选出每个学生,每个类型所借数量] SELECT a_id,u_id,count(u_id) a ...
- 目标检测后处理之NMS(非极大值抑制算法)
1.定义: 非极大值抑制算法NMS广泛应用于目标检测算法,其目的是为了消除多余的候选框,找到最佳的物体检测位置. 2.原理: 使用深度学习模型检测出的目标都有多个框,如下图,针对每一个被检测目标,为了 ...
- 2.Bacula Server端安装配置
1. Bacula Server端安装配置 1.1. Bacula Server端安装 1.1.1. 安装bacula依赖包 For Centos6: yum install -y mysql ...
- Java中Static关键字详解以及静态变量和成员变量的区别
一.static关键字的特点 (1)修饰成员变量.成员方法(2)随着类的加载而加载(3)优先于对象存在(4)被所有对象共享(5)可以通过类名调用 它本身也可以通过对象名调用 例如:main()方法由j ...
- Fiddler抓包HTTPS捕捉旧版App
“现在可以公开的情报:简易操作Fiddler抓包可能” 任何App的更新都限于苹果开发者规定,有时为了上架不得已放弃一些真正实用的功能,比如视频音频的直接下载,脚本的直接导入,手机上IPA的直接安装等 ...
- Tomcat - Tomcat安装
Tomcat官网:http://tomcat.apache.org/ 准备:JAVA环境布置完成 一.Windows平台 1. 版本选择 1) 进入官网 2) 查看版本匹配 官网说明 https:// ...