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

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 45 68 5 9 50 23 5 98
-----------------------list1反序---------------------
98 5 23 50 9 5 68 45 22 36
-----------------------list2---------------------
94 73 76 30 92 89 13 53 28 23
-----------------------排序后list1---------------------
5 5 9 22 23 36 45 50 68 98
-----------------------排序后list2---------------------
13 23 28 30 53 73 76 89 92 94
-----------------------list1与list2合并---------------------
5 5 9 13 22 23 23 28 30 36 45 50 53 68 73 76 89 92 94 98

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值