Java数据结构——Java语言下的链表
文章目录
- 1. Java语言下的链表
- 1.1 内部类定义结点
- 1.2 初始链表,只有头结点
- 1.3 清空链表
- 1.4 返回元素的位置(在第几个结点)
- 1.5 在某个结点处插入元素
- 1.5 删除某个结点的元素
- 1.6 便历链表
- 2. Java源码
- 2.1 LinkList类
- 2.2 运行结果
- 2.3 注意
1. Java语言下的链表
1.1 内部类定义结点
class Node {int data;Node next;Node(int elem) {data = elem;next = null;}}
1.2 初始链表,只有头结点
class LinkList {...Node head;LinkList() {head = new Node(0);}
}
1.3 清空链表
void reset() {head.next = null;}
1.4 返回元素的位置(在第几个结点)
public int locate(int elem) {int location = -1;Node tempNode = head.next;int TempLocation = 0;while (tempNode != null) {if (tempNode.data == elem) {location = TempLocation;break;}tempNode = tempNode.next;TempLocation++;} return location;}
1.5 在某个结点处插入元素
public boolean Insert_L(int location, int elem) {Node tempNode = head;Node NewNode;for(int i=0; i<location; i++) {if(tempNode.next==null) {System.out.println("ERROR");return false;}tempNode = tempNode.next;}NewNode = new Node(location);NewNode.next = tempNode.next;tempNode.next = NewNode;return true;}
1.5 删除某个结点的元素
//数据结构原型
Status Delete_L(LinkList &L, int i, ElemType &e) {LinkList p; int j;p = L; j = 0;while(p->next&&j<i-1) {p = p->next;++j;}if(!(p->next)||j>i-1) return ERROR;LinkList q;q = p->next; p->next = q->next;e = q->data;free(q);return OK;
}// public boolean delete_L(int location) {// if(head.next==null) {// System.out.println("Empty");
// return false;
// }
// Node tempNode = head;
// for(int i=0; i<location-1; i++) {// if(tempNode.next==null) {// System.out.print("ERROR");
// return false;
// }
// tempNode = tempNode.next;
// }
// tempNode.next = tempNode.next.next;
// return true;
// }public boolean Delete_L(int location) {if(head.next==null) {System.out.println("Empty");return false;}Node p = head; //用p来找前驱int j = 0;
// while(p.next!=null&&j<location-1) {// p = p.next;
// ++j;
// }for(int i=0; i<location-1; i++) {if(p.next==null) {System.out.print("ERROR");return false;}p = p.next;}if(p.next==null||j>location-1) {System.out.println("ERROR");return false;}Node q;q = p.next; p.next = q.next;return true;}
1.6 便历链表
public String toString() {String resulting = null;if(head.next==null) {return "Empty";}Node NewNode = head.next;while(NewNode!=null) {resulting += NewNode.data+",";NewNode = NewNode.next;}return resulting;}
2. Java源码
2.1 LinkList类
package datastructure.linearlist;/*** 链表* * @author NoBug* @time 2022/3/9**/
public class LinkList {/*** 内部类作为链表结点* * @author NoBug**/class Node {/** 数据域 */int data;/** 链域 */Node next;Node(int elem) {data = elem;next = null;}}/** 头结点 */Node head;/*** 构造函数,初始头结点*/public LinkList() {head = new Node(0);}/*** 链表清空*/void reset() {head.next = null;}/*** 得到结点在第几个结点* * @param elem 元素* @return*/public int getLocation(int elem) {int location = -1;Node tempNode = head.next;int tempLocation = 0;int t = 0;while (tempNode != null) {if (tempNode.data == elem) {location = tempLocation;t++;break;}tempNode = tempNode.next;tempLocation++;}if (t == 0) {return -1;}return location + 1;}/*** 链表的插入* * @param location* @param elem* @return*/public boolean insert(int location, int elem) {Node tempNode = head;Node NewNode;for (int i = 1; i < location; i++) {if (tempNode.next == null) {System.out.println("ERROR");return false;}tempNode = tempNode.next;}NewNode = new Node(location);NewNode.next = tempNode.next;tempNode.next = NewNode;return true;}/*** 链表的删除* * @param location* @return*/public boolean delete(int location) {if (head.next == null) {System.out.println("ERROR");return false;}Node tempNode = head;for (int i = 0; i < location - 1; i++) {if (tempNode.next.next == null) {System.out.print("ERROR");return false;}tempNode = tempNode.next;}tempNode.next = tempNode.next.next;return true;}public String toString() {String resultString = " ";if (head.next == null) {return "Empty";}Node NewNode = head.next;while (NewNode != null) {resultString += NewNode.data + " ";NewNode = NewNode.next;}return resultString;}public static void main(String[] args) {LinkList linklist = new LinkList();int[] elem = { 1, 2, 3 };int[] location = { 1, 2, 3 };// 插入linklist.insert(location[0], elem[0]);linklist.insert(location[1], elem[1]);linklist.insert(location[2], elem[2]);System.out.println("打印链表:" + linklist.toString());// 查寻System.out.println(elem[2] + "->这个元素在第几个结点:" + linklist.getLocation(elem[2]));// 删除if (linklist.delete(location[2]))System.out.println("删除第" + location[2] + "个结点后,打印链表:" + linklist.toString());elseSystem.out.println(",删除不成功");// 清空linklist.reset();System.out.println("清空链表后,打印链表:" + linklist.toString());}} // LinkList
2.2 运行结果
2.3 注意
- 我的源码的结点个数从1开始,而没有按照数组的下标从0开始。
- 因为这样便于理解:头结点 -> 第1个结点 -> 第2个结点 -> … …
Java数据结构——Java语言下的链表相关推荐
- java数据结构与算法之双链表设计与实现
转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/53047590 出自[zejian的博客] 关联文章: java数据结 ...
- Java数据结构与算法-SingleLinkedList单向链表插入,删除,查找,修改详解及代码
SingleLinkedList单向链表插入,删除,查找,修改详解及代码 单向链表学习目标 1. 链表的介绍 2. 单向链表的存储特点以及原理 3. 基本操作:插入,删除等 4. 单向链表应用场景举例 ...
- Java数据结构 利用双栈实现链表操作
利用双栈实现链表操作 class CQueue {private int count;private ListNode head;private ListNode tail;public CQueue ...
- 数据结构C语言实现顺序链表基本操作(插入,排序,合并)
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int ElemType; ...
- java数据结构与算法之顺序表与链表深入分析
转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/52953190 出自[zejian的博客] 关联文章: java数据结 ...
- java数据结构与算法之(Queue)队列设计与实现
[版权申明]转载请注明出处(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/53375004 出自[zejian的博客] ...
- Java——数据结构之双向链表
文章目录 Java--数据结构之双向链表 1.单链表的实现 (1)定义一个节点类型 (2)头插法 (3)尾插法 (4)根据下标插入节点 (5)查找关键字 (6)删除第一次出现的关键字 (7)删除所有出 ...
- java数据结构:双向链表结构与实现
本教程的内容基本来自于<Java数据结构与算法> 单向链表的缺点在于每个节点只能知道自己与下一个节点,无法寻找到上一个节点.而双向链表可以解决这个问题,通过在节点内部添加一个previou ...
- 20180826(01)-Java数据结构
Java 数据结构 Java工具包提供了强大的数据结构.在Java中的数据结构主要包括以下几种接口和类: 枚举 (Enumeration) 位集合(BitSet) 向量 (Vector) 栈 (Sta ...
最新文章
- 技术安全与伦理的较量:论道AI的能与不能 | AI Time
- phpcms V9 相关阅读/相关文章
- java urlencode 解码_java.net.URLEncode编码 与 URLDecode解码问题
- 阿里巴巴将赴NIPS 2017 3大事业部联袂展示AI全技能
- tcpdump的用法
- 局域网打印机反应慢_为什么你的Excel这么慢,这些原因必须要知道!
- P3-weixin-2.0.1 版本发布,JAVA微信插件框架
- PAT乙:1022 D进制的A+B
- Linux入门之VIM快捷使用
- Java Web 开发详解
- Neo4j之下载安装:windows
- 项目管理常用套表模板介绍
- c语言电脑蓝屏代码,电脑蓝屏代码0x0000001a的解决方法
- dejavu歌曲识别介绍
- 路由控制配置network命令解析
- 计算机路由器上网配置,电脑如何设置路由器上网?
- UE4编辑器工具——制作一个批量改名的小工具
- 无人机航测流程详解:航线规划、像控点布设、CC刺点建模及CASS成图
- IDEA官方中文插件!!!
- CSS3——鼠标变小手