/**
*
* 单链表基本操作
*
* @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. 自制简易Linux系统

    l 要求: 裁剪一个装载有网卡驱动可以上网并且使用到init的一个小系统 l 准备: 虚拟机,centos6.4 l 步骤: 一.在宿主机上添加一块硬盘,并为其安装grub 1. 在宿主机上添加一块硬 ...

  2. 关于批量插入数据之我见(100万级别的数据,mysql) (转)

    因前段时间去面试,问到如何高效向数据库插入10万条记录,之前没处理过类似问题,也没看过相关资料,结果没答上来,今天就查了些资料,总结出三种方法: 测试数据库为MySQL!!! 方法一: public  ...

  3. 【Centos7】安装mongodb 使用yum源

    根据mongodb官网提供的教程安装: 1.创建mongdb-org-3.4.repo 2.使得selinux的config为disabled 3.yum -y install mongodbxxxx ...

  4. 再起航,我的学习笔记之JavaScript设计模式17(模板方法模式)

    模板方法模式 由模板方法模式开始我们正式告别结构型设计模式,开始行为型设计模式的学习分享 行为型设计模式用于不同对象之间职责划分或算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模 ...

  5. Ext.grid.EditorGridPanel分页刷新

    store.reload(); var start = grid.getBottomToolbar().cursor;//获取当前页开始条数 上面获取当前页第一条记录的方法有时候说未定义,我现在使用下 ...

  6. 处理 Vue 单页面应用 SEO 的另一种思路

    vue-meta-info 官方地址: monkeyWangs/vue-meta-info (设置vue 单页面meta info信息,如果需要单页面SEO,可以和 prerender-spa-plu ...

  7. 文档API生成神器SandCastle使用心得

    一.功能描述 关于Sandcastle网上的参考资料相对较少,Google出来很多资料都是全英文的,相对于我这种英语渣渣看起来还是很费劲的. 言简意赅,Sandcastle主要功能是能够将C#类生成类 ...

  8. jdbc hibernate myBatis比较

    jdbc hibernate myBatis比较 jdbc 优点:性能高,易掌握 缺点:代码繁琐 hibernate 优点:不用写sql,代码简洁 缺点:性能不好 自动生成的sql效率低下(复杂业务) ...

  9. 从送外卖到建站售主机还有共享自行车说起-2017年8月江西IDC排行榜与发展报告

    曾几何时,送外卖,这样的"低技术含量"工作,很难被互联网公司看上,直到百度将其当作连接终端用户与大数据的管道. 同样,销售主机域名和建站业务,本也是"微小体量" ...

  10. [转]Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp85   根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给 ...