单链表的操作(添加,遍历,反序,排序,合并)

来源:互联网 发布:健康网络教育的意义 编辑:程序博客网 时间: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