链表归并排序

#include 
#include 
#include 
#include 
//#include 
//using namespace std;

typedef int TYPE;
typedef struct NODETAG
{
    struct NODETAG* pNext;
    TYPE value;
}
NODE;


NODE* build(TYPE A[], int n)
{
    if(n <= 0) return NULL;
    int i;
    NODE* pList, *pNode;
    pList = pNode = (NODE*)malloc(sizeof(NODE));
    pNode->value = A[0];
    for(i = 1; i < n; ++i){
        pNode->pNext = (NODE*)malloc(sizeof(NODE));
        pNode = pNode->pNext;
        pNode->value = A[i];
    }
    pNode->pNext = NULL;
    return pList;
}

void print(NODE *pList)
{
    if(pList == NULL) {printf("NULL\n"); return;}
    while(pList != NULL)
    {
        printf("%d ", pList->value);
        pList = pList->pNext;
    }
    printf("\n");
}

NODE* divide(NODE* pSubList)
{
    NODE* pPos, 
        * pMid,
        * pSecondList;
    if((pMid = pSubList) == NULL) return NULL;
    pPos = pMid->pNext;
    while(pPos != NULL)
    {
        pPos = pPos->pNext;
        if(pPos != NULL)
        {
            pPos = pPos->pNext;
            pMid = pMid->pNext;
        }
    }
    pSecondList = pMid->pNext;
    pMid->pNext = NULL;
    return pSecondList;
}

NODE* divide2(NODE* pSubList)
{
    if(pSubList == NULL) return NULL;
    if(pSubList->pNext == NULL)return NULL;
    
    NODE* p1 = pSubList, *p2 = pSubList->pNext;
    while(p2->pNext != NULL){
        p1 = p1->pNext;
        p2 = p2->pNext;
        if(p2->pNext == NULL)
            break;
        p2 = p2->pNext;
    }
    p2 = p1->pNext;
    p1->pNext = NULL;
    return p2;
}

NODE* merge(NODE* pList1, NODE* pList2)
{
    if(pList1 == NULL) return pList2;
    if(pList2 == NULL) return pList1;
    
    NODE* pHead = NULL, *p1 = pList1, *p2 = pList2, *p;
    while(p1 != NULL && p2 != NULL)
    {
        if(p1->value < p2->value){
            if(pHead == NULL) pHead = p = p1;
            else {
                p->pNext = p1;
                p = p->pNext;
            }
            p1 = p1->pNext;
        }else {
            if(pHead == NULL) pHead = p = p2;
            else {
                p->pNext = p2;
                p = p->pNext;
            }
            p2 = p2->pNext;
        }   
    }
    if(p1 != NULL) p->pNext = p1;
    if(p2 != NULL) p->pNext = p2;
    
    return pHead;
}

NODE* mergeSort(NODE* pList)
{
    NODE* pMid = divide(pList);
    
    if(pMid != NULL){
        pList = mergeSort(pList);
        pMid = mergeSort(pMid);
        return merge(pList, pMid);
    }
    return pList;
}

#define N 100


int main(int argc, const char * argv[])
{
    srand(time(NULL));
    
    int A[N], i;
    for(i = 0; i < N; ++i)A[i] = rand() % N;
    //sort(A, A+N);
    
    
    
    NODE* pList, *p2, *p;
    pList = build(A, N);
    printf("List:   ");
    print(pList);
    
    printf("sorted: ");
    pList = mergeSort(pList);
    print(pList);
    
    p2 = divide(pList);
    
    printf("List1:  ");
    print(pList);
    printf("List2:  ");
    print(p2);
    
    p = merge(pList, p2);
    printf("Merged: ");
    print(p);
    
    
    
    return 0;
}

你可能感兴趣的:(code)