java实现链表
单链表
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实现链表的更多相关文章
- JAVA单向链表实现
JAVA单向链表实现 单向链表 链表和数组一样是一种最常用的线性数据结构,两者各有优缺点.数组我们知道是在内存上的一块连续的空间构成,所以其元素访问可以通过下标进行,随机访问速度很快,但数组也有其缺点 ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- Java 单向链表学习
Java 单向链表学习 链表等同于动态的数组:可以不同设定固定的空间,根据需要的内容动态的改变链表的占用空间和动态的数组同一形式:链表的使用可以更加便于操作. 链表的基本结构包括:链表工具类和节点类, ...
- java 单链表 练习
练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...
- java ListNode 链表
链表是一种数据结构:由数据和指针构成,链表的指针指向下一个节点. java ListNode 链表 就是用Java自定义实现的链表结构. 基本结构: class ListNode { //类名 :Ja ...
- Java单链表反转图文详解
Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...
- Java关于链表的增加、删除、获取长度、打印数值的实现
package com.shb.java; public class Demo8 { public Node headNode = null; /** * @param args * @date 20 ...
- Java单链表的实现
将结点Node进行封装,假设Node的操作有增加,删除,查找,打印几个操作.将Node实现为链表Link的内部类,简化代码. package Chapter5; import java.securit ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
随机推荐
- Linux下安装Java环境配置步骤详述
0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...
- 20个JS优化代码技巧
原文网址链接为:http://www.jstips.co/ .截取了一部分本人认为比较实用的技巧分享给大家.其中一小部分技巧为JS面向对象的写法,不宜一一列出.关于JS面向对象的写法可参考本人前几篇随 ...
- Java Thread 的使用
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 一.线程的状态 在正式学习 ...
- GUID简介
GUID (全局唯一标识符) 编辑 全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点. ...
- LINQ系列:Linq to Object投影操作符
投影是指在将序列中的元素转换为一个自定义形式的操作.投影操作符Select和SelectMany用于选择出赋予了适当功能的值.SelectMany操作符可以处理多个集合. LINQ表达式语法: 1. ...
- Polynomial Library in OpenCascade
Polynomial Library in OpenCascade eryar@163.com 摘要Abstract:分析幂基曲线即多项式曲线在OpenCascade中的计算方法,以及利用OpenSc ...
- vue中v-bind:class动态添加class
1.html代码 <template v-for='item in names'> <div id="app" class="selectItem&qu ...
- MVC4做网站后台:栏目管理2、修改栏目
接上节添加栏目. 修改栏目与添加栏目非常相似,主要区别在于先向视图传递要修改栏目的model.另外在保存时比添加栏目验证要更多一些. 1.要验证父栏目不能是其本身: 2.父栏目不能是其子栏目: 3.父 ...
- canvas的用法介绍
目录 概述 绘图方法 图像处理方法 drawImage方法 getImageData方法,putImageData方法 toDataURL方法 save方法,restore方法 动画 像素处理 灰度效 ...
- spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor
官方文档: /** * Support class for throttling concurrent access to a specific resource. * * <p>Desi ...