单链表

 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. WCF:传输EntityFramework 实体类的POCO 代理

    WCF传输EntityFramework 实体类的POCO 代理 Windows Communication Foundation (WCF) 不能对代理进行直接序列化或反序列化,因为 DataCon ...

  2. maven package 知识(转载)

    “打包“这个词听起来比较土,比较正式的说法应该是”构建项目软件包“,具体说就是将项目中的各种文件,比如源代码.编译生成的字节码.配置文件.文档,按照规范的格式生成归档,最常见的当然就是JAR包和WAR ...

  3. SqlServer2008到期升级企业版 密钥+图解

    最近使用SQL Server2008,结果Sql Server Management Studio提示过期了,如图: 遇到如上图情况,需要将SQL Server2008升级维护下,还是输入原来的密钥就 ...

  4. sleep和wait区别

    1. sleep和wait都是用来进行线程控制,他们最大本质的区别是: sleep()不释放同步锁,wait()释放同步锁.               sleep(milliseconds)可以用时 ...

  5. Atitit View事件分发机制

    1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...

  6. java IO流 之 其他流

    一.内存操作流(ByteArrayInputStream.ByteArrayOutputStream) (一).   public class ByteArrayInputStream extends ...

  7. Vue.js学习笔记(2)vue-router

    vue中vue-router的使用:

  8. 在 ML2 中 enable local network - 每天5分钟玩转 OpenStack(79)

    前面完成了一系列准备工作,本节开始将创建各种 Neutorn 网络,我们首先讨论 local network. local network 的特点是不会与宿主机的任何物理网卡相连,也不关联任何的 VL ...

  9. 应用程序框架实战三十八:项目示例VS解决方案的创建(一)

    进行项目开发的第一步,是创建出适合自己团队习惯的VS解决方案,虽然我已经提供了项目示例,但毕竟是我创建的,你直接使用可能并不合适,另外你如果尝试模仿重新创建该示例,中间可能碰到各种障碍,特别是项目间的 ...

  10. OWIN 中 K Commands(OwinHost.exe)与 Microsoft.AspNet.Hosting 的角色问题

    问题详情:K Commands(OwinHost.exe)是不是 OWIN 中的 Host 角色?如果是,那 Microsoft.AspNet.Hosting 对应的是 OWIN 中的哪个角色? OW ...