/**
*
* 单链表基本操作
*
* @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. mysql my.ini配置文件修改无效,修改mysql默认字符集

    问题 开始于 使用mysql命令插入中文数据插不进去 ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column ...

  2. 三菱Ethernet工业以太网

    1.什么是Ethernet? 1)网络组成信息网和控制网 2)IP地址的概念 3)端口号的概念 4)通信方式,通信协议 5)开放关闭处理 6)要点总结 2.Ethernet模块 1)CUP能够安装的E ...

  3. 8.23.2 IO-输入输出规范代码

    文件路径4种写法: 相对路径,相对当前而言,在当前路径下找: String filePath = "temp01";  绝对路径: String filePath = " ...

  4. 利用JavaScript来切换样式表

    切换样式表 html页 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  5. 为什么Java字符串是不可变对象?

    转自 http://developer.51cto.com/art/201503/468905.htm 本文主要来介绍一下Java中的不可变对象,以及Java中String类的不可变性,那么为什么Ja ...

  6. 进程管理之system

    system定义 #include<stdlib.h> int system(const char *command); 首先要知道,system函数是c库中的函数,而不是系统调用.其实s ...

  7. Python内存优化

    实际项目中,pythoner更加关注的是Python的性能问题,之前也写过一篇文章<Python性能优化>介绍Python性能优化的一些方法.而本文,关注的是Python的内存优化,一般说 ...

  8. 重写equals就必须重写hashCode的原理分析

    因为最近在整理Java集合的源码, 所以今天再来谈谈这个古老的话题,因为后面讲HashMap会用到这个知识点, 所以重新梳理下. 如果不被重写(原生Object)的hashCode和equals是什么 ...

  9. 转载 远程用户连接mysql授权

    授权法:  在安装mysql的机器上运行:  1.d:\mysql\bin\>mysql -h localhost -u root  //这样应该可以进入MySQL服务器  2.mysql> ...

  10. c# DateTime 类

    获得当前系统时间: DateTime dt = DateTime.Now;Environment.TickCount可以得到"系统启动到现在"的毫秒值DateTime now = ...