/**
*
* 单链表基本操作
*
* @author John
*
*/
class LinkList { private Node first;
private int pos = 0; public LinkList() {
this.first = null;
} /**
* 插入头结点
*
* @param data
*/
public void insertFirstNode(int data) {
Node node = new Node(data);
node.next = first;
first = node;
} /**
* 删除头结点
*
* @return
*/
public Node deleteFirstNode() {
Node tempNode = first;
first = first.next;
return tempNode;
} /**
* 在index之后的位置插入date数据
*
* @param index
* @param data
*/
public void insertNode(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while (pos != index) {
previous = current;
current = current.next;
pos++;
}
node.next = current;
previous.next = node;
pos = 0;
} /**
* 根据位置删除节点
*
* @param index
* @return
*/
public Node deleteBypos(int index) {
Node current = first;
Node previous = first;
while (pos != index) {
pos++;
previous = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
pos = 0;
previous.next = current.next;
}
return current;
} /**
* 根据节点数据删除指定节点
*
* @param data
* @return
*/
public Node deleteByData(int data) {
Node current = first;
Node previous = first;
Node temp = null;
while (current != null) {
while (current.data != data) {
if (current.next == null) {
if (temp != null)
return temp;
return null;
}
previous = current;
current = current.next;
}
if (current == first) {
temp = current;
first = first.next;
current = first;
previous = first;
} else {
temp = current;
previous.next = current.next;
current = current.next;
}
}
return temp;
} /**
* 根据某一位置查找节点信息
*
* @param index
* @return
*/
public Node findByPos(int index) {
Node current = first;
if (pos != index) {
current = current.next;
pos++;
}
pos = 0;
return current;
} /**
* 输出所有节点信息
*/
public void displayAllNodes() {
Node current = first;
String next = "";
while (current != null) {
System.out.print(next);
current.display();
current = current.next;
next = " --> ";
}
System.out.println();
} /**
* 得到链表的长度
*
* @return
*/
public int length() {
Node current = first;
int lenght = 0;
while (current != null) {
lenght++;
current = current.next;
}
return lenght;
} /**
* 对链表元素数据进行排序
*/
public void sort() {
int n = this.length();
int temp = 0;
Node p = first;
if (first == null || first.next == null) {
return;
}
for (int i = 1; i < n; i++) {
p = first;
for (int j = i; j < n; j++) {
if (p.data > p.next.data) {
temp = p.data;
p.data = p.next.data;
p.next.data = temp;
}
p = p.next;
}
}
} /**
* 反转链表
*/
public void reverse() {
if (first == null || first.next == null) {
return;
}
Node p1 = first;
Node p2 = p1.next;
Node p3 = null;
while (p2 != null) {
p3 = p2.next;
p2.next = p1;
p1 = p2;
p2 = p3;
}
first.next = null;
first = p1;
} } class Node {
Node next;
int data; public Node(int data) {
this.data = data;
} public void display() {
System.out.print(data);
}
}

Java单链表实现的更多相关文章

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

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

  2. java 单链表 练习

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

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

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

  4. java单链表代码实现

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

  5. java单链表常用操作

    总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...

  6. JAVA单链表的实现-不带头结点但带有尾指针

    1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...

  7. JAVA单链表的实现-不带头结点且没有尾指针

    本程序采用JAVA语言实现了线性表的链式实现.首先定义了线性表的接口ListInterface,然后LList类实现了ListInterface完成了链表的实现. 本实现中,链表是不带表头结点的,且有 ...

  8. Java单链表简单实现* @version 1.0

    package com.list; /** * 数据结构与算法Java表示 * @version 1.0 * @author 小明 * */ public class MyLinkedList { p ...

  9. java 单链表反转

    最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...

  10. Java单链表、双端链表、有序链表实现

    单链表: insertFirst:在表头插入一个新的链接点,时间复杂度为O(1) deleteFirst:删除表头的链接点,时间复杂度为O(1) 有了这两个方法,就可以用单链表来实现一个栈了,见htt ...

随机推荐

  1. CSS三种样式表

    1.外部样式表当样式需要应用于很多页面时,外部样式表将是理想的选择.在使用外部样式表的情况下,你可以通过改变一个文件来改变整个站点的外观.每个页面使用 <link> 标签链接到样式表. & ...

  2. 【JavaScript】Js控制页面所有元素只读

    在页面初始化加载以下readOnlyPage()方法,可实现所有元素只读,方便实用. <script language="javascript"> function r ...

  3. 7_linux下PHP、Apache、Mysql服务的安装

    1.首先安装之前,要确保你的虚拟机能连上外网. Mysql: 1.yum -y install mysql   连接数据库命令行模式 2.yum install mysql-server  安装mys ...

  4. 利用python设计PDF报告,jinja2,whtmltopdf,matplotlib,pandas

    转自:https://foofish.net/python-crawler-html2pdf.html 工具准备 弄清楚了网站的基本结构后就可以开始准备爬虫所依赖的工具包了.requests.beau ...

  5. mysql外键

    1.直接在SQLyog中创建外键: 在SQLyog中的架构设计器中拖进2张表,然后选择t_book的bookTypeId拖到另一张表t_booktype的ID位置,然后就能创建外键了. MySQL的外 ...

  6. 报表 jasper + ireport5.6

    下载 iReport-5.6.0,jdk7,以及众多lib , 这里我提供下资源(我的百度云) 安装好iReport-5.6.0和jdk7,  在安装目录的\etc\ireport.conf,修改其中 ...

  7. Linux下检测进程是否存在

    这个问题看起来好像很简单,"ps -ef | grep xx"一下就行啦!这样做当然可以,但是如果我们考究起性能来,这恐怕不是个好办法. 假设我们现在要监测某进程是否存活,每分钟检 ...

  8. 关于Java的发展前景

    各位看官觉得Java还能火几年?未来的发展方向是什么?

  9. ROS学习记录(二)————使用smartcar进行仿真(用.xacro文件来运行rviz)

    我发现一个学习ROS系统的好网站: 创客智造http://www.ncnynl.com/ 这里面关于ROS的各个方面都有很详细的介绍. 这周,不,上周我对整个ROS是绝望的,我用一个一个下午的时间在敲 ...

  10. Motion control encoder extrapolation

    Flying Saw debug Part1 Encoder extrapolation Machine introduction A tube cutting saw, is working for ...