Java单链表实现
/**
*
* 单链表基本操作
*
* @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单链表实现的更多相关文章
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- java 单链表 练习
练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...
- Java单链表反转图文详解
Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- java单链表常用操作
总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...
- JAVA单链表的实现-不带头结点但带有尾指针
1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...
- JAVA单链表的实现-不带头结点且没有尾指针
本程序采用JAVA语言实现了线性表的链式实现.首先定义了线性表的接口ListInterface,然后LList类实现了ListInterface完成了链表的实现. 本实现中,链表是不带表头结点的,且有 ...
- Java单链表简单实现* @version 1.0
package com.list; /** * 数据结构与算法Java表示 * @version 1.0 * @author 小明 * */ public class MyLinkedList { p ...
- java 单链表反转
最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...
- Java单链表、双端链表、有序链表实现
单链表: insertFirst:在表头插入一个新的链接点,时间复杂度为O(1) deleteFirst:删除表头的链接点,时间复杂度为O(1) 有了这两个方法,就可以用单链表来实现一个栈了,见htt ...
随机推荐
- 秒懂OAuth2.0
1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常简单的一件事情,网上一堆神乎其神的讲解,让我不得不写一篇文章来终结它们. 一项新的技术,无非就是了解它是什么,为什 ...
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...
- C# 使用NPOI 实现Excel的简单导入导出
private void btnImport_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt ...
- 如何搭建Zookeeper集群
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的 ...
- Swiper 滑动
1.http://www.swiper.com.cn/download/ 下载Swiper.JS Swiper.CSS 2.引入项目,添加html <div class="cont ...
- NOPI读xls文件写到txt中(NPOI系列二)
private void button2_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); //找 ...
- ★电车难题的n个坑爹变种
哲学家都不会做的电车难题变异 此题会答清华北大 "电车难题(Trolley Problem)"是伦理学领域最为知名的思想实验之一,其内容大致是: 一个疯子把五个无辜的人绑在电车轨道 ...
- JAVA基础第三组(5道题)
11 [程序11] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件 ...
- 英语学习app案列分析
很多同学有误解,软件工程课是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件工程有理论,有实践,更重要的是分析,思辨,总结.在课程 ...
- 201521123061 《Java程序设计》第十一周学习总结
201521123061 <Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 本周学习的是如何解决多线程访问中的互斥 ...