/**
*
* 单链表基本操作
*
* @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. SLF4J源码解析-LoggerFactory(二)

    承接前文SLF4J源码解析-LoggerFactory(一),本文则主要针对获取ILoggerFactory对象作下简单的分析 LoggerFactory#getILoggerFactory() 源码 ...

  2. 总结切面编程AOP的注解式开发和XML式开发

    有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...

  3. os.path python使用遍历文件夹文件

    import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirnames,filenames in ...

  4. ruby开发环境搭建

    ruby开发可以在max os .Linux系统或windows下进行.推荐使用linux系统,这里以在linux系统下搭建开发环境为例.大体上需要以下几步: 一.下载并安装virtualbox和ub ...

  5. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  6. LESS文档

    less官方介绍文档(http://lesscss.org/#docs) Getting Started Less is a CSS pre-processor, meaning that it ex ...

  7. zip7压缩

    7-zip 解压 1.引入依赖文件 sevenzipjbinding.jar sevenzipjbinding-Allwindows.jar <!-- https://mvnrepository ...

  8. python编码错误的解决办法 SyntaxError: Non-ASCII character '\xe5' in file

    [提出问题]. 在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息: SyntaxError: Non-ASCII character '\xe5' in file ******* ...

  9. pip源相关问题

    指定源地址安装: pip install -i http://pypi.douban.com/simple/ packagename pip install -i http://pypi.tuna.t ...

  10. h5video标签

    在video标签中,我们可以使用属性:videoWidth & videoHeight,它获取的是video的宽度和高度(媒体本身).虽然不能直接使用,但是可以通过计算宽高比得到 video ...