单链表的操作(添加,遍历,反序,排序,合并)
来源:互联网 发布:健康网络教育的意义 编辑:程序博客网 时间:2024/06/11 22:25
package cn.edu.ujs;public class Node { private int value; private Node next; public Node() { } public Node(int value){ this.value = value; this.next = null; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; }}package cn.edu.ujs;public class NodeOperating { /** * 遍历链表 * * @param head * 头节点 */ public static void Traversal(Node head) { if (head == null) { System.out.println("Null LinkList!"); } else { Node current = head; while (current != null) { System.out.print(current.getValue() + "\t"); current = current.getNext(); } System.out.println(); } } /** * 合并有序(升序)链表 * * @param list1 * @param list2 */ public static Node Merge(Node list1, Node list2) { Node current = null; Node head = null; while (list1 != null && list2 != null) { if (list1.getValue() <= list2.getValue()) { if (head == null) { head = list1; } else { current.setNext(list1); } current = list1; list1 = list1.getNext(); } else { if (head == null) { head = list2; } else { current.setNext(list2); } current = list2; list2 = list2.getNext(); } } if (list1 != null) { current.setNext(list1); } if (list2 != null) { current.setNext(list2); } return head; } /** * 链表反序 * * @param head * @return */ public static Node ReverseOrder(Node head) { if (head == null) { return null; } else { Node tail = head; if (head.getNext() != null) { while (tail.getNext() != null) { Node temp = tail.getNext(); tail.setNext(temp.getNext()); temp.setNext(head); head = temp; } } return head; } } /** * 在链表末尾添加节点 * * @param head * @param value * @return */ public static Node add(Node head, int value) { Node node = new Node(value); if (head == null) { head = node; } else { Node current = head; while (current.getNext() != null) { current = current.getNext(); } current.setNext(node); } return head; } /** * 链表排序 * @param list * @return */ public static Node sortList(Node list) { if (list == null) { System.out.println("Error:The list is null!"); } else { //把链表分开,以list为头节点的为新链表,以current为头节点的为待插入链表 Node current = list.getNext(); //初始化新链表,把next节点置空 list.setNext(null); //循环结束条件为current链表所有节点插入完成 while (current != null) { boolean flag = false; Node p = list; Node tmp = current.getNext(); //内部循环条件为指针移动到新链表尾 while (p.getNext() != null) { if (list.getValue() > current.getValue()) {//插在头节点之前 current.setNext(list); list = current; flag = true; break; } else if (current.getValue() < p.getNext().getValue()) {// 插在中间 current.setNext(p.getNext()); p.setNext(current); flag = true; break; } else { //继续移动指针寻找位置 p = p.getNext(); } } if (flag == false) { if(p.getValue()>current.getValue()){//新链表中只有头节点时且数据大于当前节点时插入头节点之前 current.setNext(list); list = current; }else{//指针到达最后节点数据仍小于当前节点则插入新链表最后 p.setNext(current); current.setNext(null); } } //current指针后移 current = tmp; } } return list; }}package cn.edu.ujs;import java.util.Random;public class TestList { public static void main(String[] args) { Random rd = new Random(); Node list1 = null; Node list2 = null; for(int i=0;i<10;i++){ list1 = NodeOperating.add(list1,rd.nextInt(100)); } System.out.println("-----------------------list1---------------------"); NodeOperating.Traversal(list1); list1 = NodeOperating.ReverseOrder(list1); System.out.println("-----------------------list1反序---------------------"); NodeOperating.Traversal(list1); for(int j=0; j<10; j++){ list2 = NodeOperating.add(list2,rd.nextInt(100)); } System.out.println("-----------------------list2---------------------"); NodeOperating.Traversal(list2); list1 = NodeOperating.sortList(list1); System.out.println("-----------------------排序后list1---------------------"); NodeOperating.Traversal(list1); list2 = NodeOperating.sortList(list2); System.out.println("-----------------------排序后list2---------------------"); NodeOperating.Traversal(list2); Node head = NodeOperating.Merge(list1, list2); System.out.println("-----------------------list1与list2合并---------------------"); NodeOperating.Traversal(head); }}
运行结果:(随机数作为节点数据,每次运行都不同)
-----------------------list1---------------------
36 22 4568 5 9 50 235 98
-----------------------list1反序---------------------
98 5 2350 9 5 68 4522 36
-----------------------list2---------------------
94 73 7630 92 89 13 5328 23
-----------------------排序后list1---------------------
5 5 922 23 36 45 5068 98
-----------------------排序后list2---------------------
13 23 2830 53 73 76 8992 94
-----------------------list1与list2合并---------------------
5 5 913 22 23 23 2830 36 45 50 5368 73 76 89 9294 98
- 单链表的操作(添加,遍历,反序,排序,合并)
- 合并有序单链表并排序(遍历一次)
- 合并两个排序的链表及简单链表的一些操作(添加节点、删除节点)
- Python中的列表(添加二元列表、连接、查找、排序、反序、赋值、删除、分片操作、负索引、)
- 合并Log的右键操作功能添加
- 数组的添加、删除、排序、与字符串之间的转换、截取、查找、遍历操作总结!!!
- 单链表一次遍历反序
- C++ vector的初始化、添加、遍历、插入、删除、查找、排序、释放操作
- 近期面试题整理(二叉树的中序遍历、合并排序链表数组)
- javascript DOM的操作,更新,遍历,添加
- list容器基本操作(添加、遍历)
- 对数组的遍历及排序操作
- js 数组的 创建 操作 遍历 排序
- 单链表的排序与合并
- 合并两个排序的单链表
- 单链表的排序合并实现
- 单链表的头插 尾插 反序 遍历 插入 删除
- 带头结点单链表的基本操作(创建、测长、打印、插入、删除、取值、合并、排序、逆置)
- 编译U-boot 出现Nothing to be done for `_depend'.
- 软件公司安全测试基线
- socket 编程入门教程(一)TCP server 端:1、建模
- 2013年10月13日--2013年10月19日(共56小时,剩4952小时)
- socket 编程入门教程(一)TCP server 端:2、socket与文件描述符
- 单链表的操作(添加,遍历,反序,排序,合并)
- [C语言]菜鸟的一些理解-预处理
- socket 编程入门教程(一)TCP server 端:3、sockaddr与sockaddr_in
- socket 编程入门教程(一)TCP server 端:4、构造函数涉及的概念
- ubuntu下装gmp,libfplll经验
- socket 编程入门教程(一)TCP server 端:5、创建监听嵌套字
- EL 表达式的用法
- socket 编程入门教程(一)TCP server 端:6、创建“通讯 ”嵌套字
- socket 编程入门教程(一)TCP server 端:7、接收与发送