Java单链表基本操作(七)--排序

来源:互联网 发布:淘宝手机卡怎么领取 编辑:程序博客网 时间:2024/05/18 03:31

单链表的插入排序比数组麻烦,因为每次都都要从头节点开始往后遍历,头节点也需要单独处理

package listnode;/**  * @author Gavenyeah * @date Start_Time:2016年4月1日 下午14:07:04  * @date End_Time:2016年4月1日 下午14:55:04  */public class SortList {    public static void main(String[] args) {        Node head=ListNode.getSingleList();        ListNode.printList(head);        head=new SortList().insertSortList(head);        ListNode.printList(head);    }    public Node insertSortList(Node head){        Node p=head.next;        Node pre=head;        while(p!=null){            Node cur=head;  //比较节点,每次都是从头节点开始            Node q=p.next;            if(p.data<head.data){ //由于是单链表,每次只能从头节点开始比较                pre.next=q;                p.next=head;                head=p;            }else                while(cur.next!=p){                    if(p.data<cur.next.data){//将P与cur.next进行比较,方便单链表插入                        pre.next=q;                        p.next=cur.next;                        cur.next=p;                        p=pre;  //保证pre每次指向的都是p前面的一个节点                    }else                        cur=cur.next;                }            pre=p;            p=q;        }        return head;    }}

代码中调用的Node类和ListNode类,代码详见
Java单链表基本操作(一)–顺序查找

1 0
原创粉丝点击