Java实现单链表基本操作
来源:互联网 发布:c语言scanf 编辑:程序博客网 时间:2024/06/06 19:15
Java实现单链表的 插入头结点、删除头结点、在指定位置添加元素、在指定位置删除元素、根据数据删除元素、显示所有元素、根据位置查找元素、根据数值查找元素等。
public class Node {public int value;public Node next;public Node(int value) {this.value = value;}// 显示此节点public void display() {System.out.print(value + " ");}}
public class LinkedList {public Node first;// 定义头结点public int pos = 0;// 节点的位置/** * 构造方法 */public LinkedList() {this.first = null;}/** * 插入头结点(头插法) * * @param data */public void addFirstNode(int data) {Node node = new Node(data);node.next = first;first = node;}/** * 删除头结点 * * @return */public Node deleteFirstNode() {Node tempNode = first;first = tempNode.next;return tempNode;}/** * 在指定位置添加元素 * * @param index * @param data */public void add(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;while (current.value != data) {if (current.next == null) {return null;}previous = current;current = current.next;}if (current == first) {first = first.next;} else {previous.next = current.next;}return current;}/** * 显示所有元素 */public void displayAllNode() {Node current = first;while (current != null) {current.display();current = current.next;}System.out.println();}/** * 根据位置查找元素 * * @param index * @return */public Node findByPos(int index) {Node current = first;while (pos != index) {current = current.next;pos++;}pos = 0;return current;}/** * 根据数值查找元素 * * @param data * @return */public Node findByData(int data) {Node current = first;while (current.value != data) {if (current.next == null) {return null;}current = current.next;}return current;}public static void main(String[] args) {LinkedList linkedList = new LinkedList();// 添加元素linkedList.addFirstNode(26);linkedList.addFirstNode(25);linkedList.addFirstNode(23);linkedList.addFirstNode(22);linkedList.addFirstNode(20);linkedList.displayAllNode();// 此时显示内容为 :20 22 23 25 26linkedList.add(1, 21);linkedList.add(4, 24);linkedList.displayAllNode();// 此时显示内容为 :20 21 22 23 24 25 26Node node = linkedList.deleteByPos(0);System.out.println("删除元素为:" + node.value);linkedList.displayAllNode();Node node2 = linkedList.deleteByData(21);System.out.println("删除元素为:" + node2.value);linkedList.displayAllNode();Node node3 = linkedList.deleteFirstNode();System.out.println("删除元素为:" + node3.value);linkedList.displayAllNode();Node node4 = linkedList.findByPos(0);System.out.println("查找到的元素为:" + node4.value);linkedList.displayAllNode();Node node5 = linkedList.findByData(24);System.out.println("查找到的元素为:" + node5.value);linkedList.displayAllNode();}}
阅读全文
0 0
- 单链表基本操作java实现
- Java实现单链表基本操作
- java实现单链表的基本操作
- 数据结构复习--java实现单链表基本操作
- java实现单链表的基本操作
- java自己实现单链表的基本操作
- 单链表基本操作实现
- 单链表基本操作实现
- JAVA单链表基本操作
- 用java简单的实现单链表的基本操作
- 用java简单的实现单链表的基本操作
- Java实现单链表插入删除等基本操作
- 单链表的基本操作与算法(Java实现)
- 用java实现单链表结构与基本数据操作
- 用java简单的实现单链表的基本操作
- 带头节点的单链表及其基本操作(Java实现)
- JAVA实现数据库基本操作指南
- Java实现二叉树的基本操作
- redis
- h5/5+APP消息推送神器:Goeasy.js
- 汇编--学习笔记(七)-数据传送指令
- 2924 数独挑战
- Kotlin for Android (II)创建一个工程
- Java实现单链表基本操作
- Google 的软件工程经验总结
- 分布式服务
- Lagom参考指南(三)
- 在构造器中为什么this或super必须放在第一行?
- java内存过高问题定位
- TF/02_TensorFlow_Way/03 Working with Multiple Layers04 Implementing Loss Functions
- KMP算法java实现
- Live555源码解析(2)