单链表

 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. Java 8函数编程轻松入门

    函数接口介绍 在Java1.8中,新增了Lambda表达式.在.net3.5(C# 3.0)在原先的匿名方法基础上演变出了Lambda表达式.学过C# Lambda表达式的同学,对于Java的Lamb ...

  2. 兼容各浏览器的js判断上传文件大小

    由于项目需要,在网上找了一个JS判断上传文件大小的程序,经测试兼容IE6-,Firefox10,Opera11.,safari5.,chrome17 <!DOCTYPE html> < ...

  3. 人之初,性本动 - G2 2.1 发布

    前言 随着可视化进入深水区,G2面临了越来越多交互上的需求.动画是提升交互必不可少的一部分,也是之前G2的薄弱环节.这个版本里我们开发并替换了动画底层,统一了时间轴,使G2的动画性能大大提升,并提供了 ...

  4. 了解Package Configurations

    使用VS2010创建的SSIS Project有两种deployment model:project deployment 和 package deployment,默认是Project deploy ...

  5. 【WP开发】加密篇:单向加密

    单向加密,简单地说就是对数据进行哈希处理,平时我们见得较多的有MD5.SHA1等,都属于单向加密.上一篇文章中,老周跟大家扯了有关双向加密的事,本文咱们就扯一下单向加密吧. 要对数据进行哈希处理也不是 ...

  6. OpenCascade Primitives BRep-Cone

    OpenCascade Primitives BRep-Cone eryar@163.com Abstract. BRep is short for Boundary Representation. ...

  7. [转]自己写PHP扩展之创建一个类

    原文:http://www.imsiren.com/archives/572 比如我们要创建一个类..PHP代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  8. 自定义配置UEditor 工具栏上的按钮列表

    修改配置项的方法: 1. 方法一:修改 ueditor.config.js 里面的 toolbars 2. 方法二:实例化编辑器的时候传入 toolbars 参数 方法一:在ueditor.confi ...

  9. 深入学习jQuery选择器系列第六篇——过滤选择器之状态选择器

    × 目录 [1]焦点状态 [2]哈希状态 [3]动画状态[4]显隐状态 前面的话 过滤选择器的内容非常多,本文介绍过滤选择器的最后一部分——状态选择器 焦点状态 :focus :focus选择器选择当 ...

  10. 安装Ubuntu时分区选择

    最近购买来一台二手笔记本.型号是:Dell Latitude D520.回来之后就装上来Ubuntu12.04,开始是安装的UbuntuKylin 13.04.不知道是机器配置不行,还是本身系统有点卡 ...