归并排序(链表)

来源:互联网 发布:java jstack 编辑:程序博客网 时间:2024/05/18 13:11

数据结构课链表练手- =

功能大致有:
链表正序建立,复杂度:O(n)
链表逆序建立,复杂度:O(n)
链表输出,复杂度:O(n)
判断链表是否升序,复杂度:O(n)
链表插入,复杂度:O(n)
链表反转,复杂度:O(n)
链表归并排序,复杂度:O(nlogn)?

代码:

#include<iostream>using namespace std;struct Node{    int _val;    Node *_next;    Node(int val=0,Node* next=NULL):_val(val),_next(next){} };int testdata1[10]={2,4,2,1,4,7,92,1,-1,10};int testdata2[10]={2,2,2,3,4,7,92,93,100,100};int testdata3[10]={1,2,3,3,5,7,8,16,17,25};int testdata4[13]={-10,-1,2,3,3,6,11,14,21,23,24,25,26};int testdata5[13]={2,25,3,6,-1,26,11,14,21,7,17,-10,24};int testdata6[1]={1};int testdata7[2]={2,1};int testdata8[3]={3,2,1};Node* buildChain1(int n,int *testdata){    Node *front=NULL;    for(int i=0;i<n;i++)    {        front=new Node(testdata[i],front);    }    return front;}Node* buildChain2(int n,int *testdata){    Node* front=new Node(),*p=front;    for(int i=0;i<n;i++)    {        p->_next=new Node(testdata[i],NULL);        p=p->_next;     }    p=front;    front=p->_next;    delete p;    return front;}void Output(Node* front){    Node* p=front;    while(p!=NULL)    {        cout<<p->_val<<' ';        p=p->_next;    }       cout<<endl; }bool isAscend(Node* front){    Node *p=front;    int pre=-0x3f3f3f3f;    while(p!=NULL)    {        if(p->_val<pre)            return 0;        pre=p->_val;        p=p->_next;    }    return 1;}bool insertElement(Node* front,int val,int pos){    if(pos<=0)        return false;    int bulk=1;    Node *p=front;    while(p!=NULL)    {        if(pos==bulk)        {            Node *tmp=new Node(val,p->_next);            p->_next=tmp;            return true;            }        bulk++;        p=p->_next;    }    return false;}Node* reverseChain(Node *front){    if(front==NULL)        return front;    Node *pre=front,*cur=front->_next;    while(cur!=NULL)    {        front->_next=cur->_next;        cur->_next=pre;        pre=cur;        cur=front->_next;    }    front=pre;    return front;}Node* midChain(Node* front){    if(front==NULL)         return front;    Node* p1=front,*p2=front->_next;    while(p2!=NULL)    {        p2=p2->_next;        if(p2!=NULL)        {            p1=p1->_next;            p2=p2->_next;        }           }    return p1;}Node* getMerge(Node* front1,Node* front2){    Node* front=new Node(-0x3f3f3f3f,NULL);    Node* p=front;    while(front1!=NULL||front2!=NULL)    {        if(front1!=NULL&&front2!=NULL)        {            if(front1->_val<=front2->_val)            {                p->_next=front1;                front1=front1->_next;            }            else             {                p->_next=front2;                front2=front2->_next;            }        }        else if(front1!=NULL)        {            p->_next=front1;            front1=front1->_next;        }        else         {            p->_next=front2;            front2=front2->_next;        }        p=p->_next;    }    p=front;    front=p->_next;    delete p;    return front;}Node* mySort(Node* front){    if(front==NULL||front->_next==NULL)        return front;    Node* front1,*front2,*mid;    mid=midChain(front);    front1=front,front2=mid->_next;    mid->_next=NULL;    front1=mySort(front1);    front2=mySort(front2);    front=getMerge(front1,front2);    return front;}int main(){    Node* front1=buildChain2(13,testdata5);    Output(front1);    Node* newfront1=mySort(front1);    Output(newfront1);    Node* front2=buildChain2(1,testdata6);    Output(front2);    Node* newfront2=mySort(front2);    Output(newfront2);    Node* front3=buildChain2(2,testdata7);    Output(front3);    Node* newfront3=mySort(front3);    Output(newfront3);    Node* front4=buildChain2(3,testdata8);    Output(front4);    Node* newfront4=mySort(front4);    Output(newfront4);    return 0;}