单链表

 package com.voole.linkedlist;

 public class Test {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.insert(new Node(null, null));
linkedList.insert(new Node(null, null));
linkedList.insert(new Node(null, null));
linkedList.insert(new Node(null, null));
linkedList.insert(new Node(null, null));
linkedList.insert(new Node(null, null),2);
Node node = new Node(null, null);
linkedList.update(node,2);
System.out.println(linkedList.select(2));
System.out.println(node);
}
} package com.voole.linkedlist;
/**
* 链表类(单链表)
* @author TMAC-J
*
*/
public class LinkedList {
/**
* 定义头结点
*/
private Node head = null;
/**
* 定义链表长度
*/
private int size = 0;
/**
* 定义指针,当为0时,代表指向头结点
*/
private int point = 0;
/**
* 构造方法
*/
public LinkedList(){
head = new Node(null, null);//初始化头结点
}
/**
* 增(在末尾)
*/
public void insert(Node node){
Node currentNode = null;
while(point!=size){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
/**
* if-else防止currentNode.next的空指针异常
*/
if(currentNode!=null){
currentNode.next = node;
}
else{
head.next = node;
}
point = 0;//将指针指向头结点
size++;
LinkedListLog.getInstance().insert();
}
/**
* 增(在任意位置)
*/
public void insert(Node node,int position){
checkPosition(position);
Node currentNode = null;
while(point!=position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
if(currentNode!=null){
node.next = currentNode.next;
currentNode.next = node;
}
else{
head.next = node;
}
point = 0;//将指针指向头结点
size++;
LinkedListLog.getInstance().insert();
}
/**
* 删
*/
public void delete(int position){
checkPosition(position);
Node currentNode = null;
while(point!=position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
if(size == 1){
currentNode.next = null;
}
else{
currentNode.next = currentNode.next.next;
}
point = 0;//将指针指向头结点
size--;
LinkedListLog.getInstance().delete();
}
/**
* 改
*/
public void update(Node node,int position){
checkPosition(position);
Node currentNode = null;
while(point!=position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
if(size == 1){
currentNode.next = node;
}
else{
node.next = currentNode.next.next;
currentNode.next = node;
}
point = 0;//将指针指向头结点
LinkedListLog.getInstance().update();
}
/**
* 查
*/
public Node select(int position){
checkPosition(position);
Node currentNode = null;
while(point!=position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
point = 0;
return currentNode.next;
}
/**
* 检查位置是否正确
*/
public void checkPosition(int position){
if(position>size+1||position<=0){
LinkedListLog.getInstance().error();
return;
}
}
} package com.voole.linkedlist;
/**
* @description 链表节点
* @author TMAC-J
*
*/
public class Node {
/**
* 定义指针域
*/
public Node next = null;
/**
* 定义数据域
*/
public Data data = null;
/**
* @description 构造方法
*/
public Node(Node next,Data data){
this.next = next;
this.data = data;
}
} package com.voole.linkedlist; import java.io.Serializable; public class Data implements Serializable{ /**
*
*/
private static final long serialVersionUID = 1L; } package com.voole.linkedlist;
/**
* 单例日志类(饿汉)
* @author TMAC-J
*
*/
public class LinkedListLog {
private static final LinkedListLog instance = new LinkedListLog(); private LinkedListLog(){} public static LinkedListLog getInstance(){
return instance;
} public void insert(){
System.out.println("插入成功!");
} public void delete(){
System.out.println("删除成功!");
} public void update(){
System.out.println("修改成功!");
} public void select(){
System.out.println("查询成功!");
} public void error(){
System.out.println("错误!");
}
}

插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
修改成功!
com.voole.linkedlist.Node@7cb8f891
com.voole.linkedlist.Node@7cb8f891

以上就是java代码实现单链表的过程,注意,单链表的效率其实是很低的,读者看代码也可以知道,每一次操作都需要从表头开始遍历节点,更高的效率可以在此基础上改一部分,改成双链表,这样就可以从两头查询,并且可以进一步优化,让指针不用每一次都操作完都置为0,很多方式都可以提升效率,并且可以添加同步的方法提升安全性,在此,我就不去完善了,这里只是一个最基础功能的链表。


java实现链表的更多相关文章

  1. JAVA单向链表实现

    JAVA单向链表实现 单向链表 链表和数组一样是一种最常用的线性数据结构,两者各有优缺点.数组我们知道是在内存上的一块连续的空间构成,所以其元素访问可以通过下标进行,随机访问速度很快,但数组也有其缺点 ...

  2. Java单链表反转 详细过程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...

  3. Java 单向链表学习

    Java 单向链表学习 链表等同于动态的数组:可以不同设定固定的空间,根据需要的内容动态的改变链表的占用空间和动态的数组同一形式:链表的使用可以更加便于操作. 链表的基本结构包括:链表工具类和节点类, ...

  4. java 单链表 练习

    练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...

  5. java ListNode 链表

    链表是一种数据结构:由数据和指针构成,链表的指针指向下一个节点. java ListNode 链表 就是用Java自定义实现的链表结构. 基本结构: class ListNode { //类名 :Ja ...

  6. Java单链表反转图文详解

    Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...

  7. Java关于链表的增加、删除、获取长度、打印数值的实现

    package com.shb.java; public class Demo8 { public Node headNode = null; /** * @param args * @date 20 ...

  8. Java单链表的实现

    将结点Node进行封装,假设Node的操作有增加,删除,查找,打印几个操作.将Node实现为链表Link的内部类,简化代码. package Chapter5; import java.securit ...

  9. java单链表代码实现

    用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...

随机推荐

  1. spring快速入门(一)

    对于为什么使用spring框架,这里不多做解释,详情请百度.本人推荐面向驱动程序学习,通过实战来瞧瞧spring技术的伟大.所以先来看看原始开发一个简单的例子,由例子引入spring相关的技术.如果错 ...

  2. IL指令汇总

    名称 说明   名称 说明 Add 将两个值相加并将结果推送到计算堆栈上.   Ldelem.I1 将位于指定数组索引处的 int8 类型的元素作为 int32 加载到计算堆栈的顶部. Add.Ovf ...

  3. Android开发-之环境的搭建

    关于Android开发可以使用的工具有eclipse和Android studio等,这两个工具都各有各的好处和不足.studio是谷歌推出的一款开发工具,而我们都知道Android就是谷歌公司的,所 ...

  4. Security5:Execute AS 和 impersonate 权限

    principal 是统称,包括login,user,role等,可以向作为安全主体的用户授予权限,principal分为server level和database level. 登录名是Server ...

  5. python进程池:multiprocessing.pool

    本文转至http://www.cnblogs.com/kaituorensheng/p/4465768.html,在其基础上进行了一些小小改动. 在利用Python进行系统管理的时候,特别是同时操作多 ...

  6. OpenCASCADE6.8.0 Reference Manual Serach Problem

    OpenCASCADE6.8.0 Reference Manual Serach Problem eryar@163.com 1. Problem 有网友反映OpenCASCADE6.8.0的Refe ...

  7. 基础知识javascript--事件

    群里有一个小伙伴在处理事件监听函数的时候,遇到了一点问题,正好我比较空闲,于是帮他指出了代码中的问题,顺便整理一下,方便以后遇到类似问题的伙伴们有一个参考. 这是一个很简单的问题,对于基础知识比较杂实 ...

  8. Sybase数据库,普通表修改分区表步骤

    本文目标:指导项目侧人员再遇到此类改动需求时可以自己参照更改.需求:Sybase数据库,普通表t_jingyu修改为按天分区的分区表. 1.sp_help查看t_jingyu的表结构,索引等信息 sp ...

  9. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  10. ApiController使用Session验证出现Null解决方案

    问题描述 在服务端保存登录信息,出现异常信息 分析发现HttpContext.Current.Session为null 解决方案 执行时出报异常,要在Global.asax里添加:开启Session功 ...