模版快拍


template<typename T>  
struct Node  
{  
    T m_Data;  
    Node * m_pNext;  
};  
template<typename T>  
void ListQuickSort(Node<T> * pHead, Node<T> * pEnd/*尾结点可以为空*/)  
{  
    T Key;  
    T Tmp;  
    Node<T> * pLow = NULL;  
    Node<T> * pHigh = NULL;  
    if (!pHead)  
        return ;  
    if (pHead == pEnd)  
        return;  
    pLow = pHead;  
    pHigh = pHead->m_pNext;  
    Key = pHead->m_Data;  
    while (pHigh != pEnd)  
    {  
        if (pHigh->m_Data < Key)  
        {  
            pLow = pLow->m_pNext;  
            Tmp = pLow->m_Data;  
            pLow->m_Data = pHigh->m_Data;  
            pHigh->m_Data = Tmp;  
        }  
        pHigh = pHigh->m_pNext;  
    }  
    Tmp = pHead->m_Data;  
    pHead->m_Data = pLow->m_Data;  
    pLow->m_Data = Tmp;  
    ListQuickSort(pHead, pLow);  
    ListQuickSort(pLow->m_pNext, pEnd);  
}  

#include <time.h>  
#include <iostream>  
using namespace std;  
void main()  
{  
    int i = 0;  
    Node<int> * pInt = NULL;  
    Node<int> * pNewNode = NULL;  
    Node<int> * pCurNode = NULL;  
    srand(time(NULL));  
    for (i = 0; i < 10; i++)  
    {  
        pNewNode = new Node<int>;  
        if (pNewNode == NULL)  
        {  
            while (pInt)  
            {  
                pCurNode = pInt;  
                pInt = pInt->m_pNext;  
                delete pCurNode;  
            }  
            pInt = NULL;  
            return;  
        }  
        pNewNode->m_Data = rand() % 100;  
        pNewNode->m_pNext = pInt;  
        pInt = pNewNode;  
    }  
    cout << "排序前:" << endl;  
    pCurNode = pInt;  
    while (pCurNode)  
    {  
        cout << pCurNode->m_Data << '\t';  
        pCurNode = pCurNode->m_pNext;  
    }  
    cout << endl;  
    ListQuickSort<int>(pInt, NULL);  
    cout << "排序后:" << endl;  
    pCurNode = pInt;  
    while (pCurNode)  
    {  
        cout << pCurNode->m_Data << '\t';  
        pCurNode = pCurNode->m_pNext;  
    }  
    cout << endl;  
    while (pInt)  
    {  
        pCurNode = pInt;  
        pInt = pInt->m_pNext;  
        delete pCurNode;  
    }  
    pInt = NULL;  
    _CrtDumpMemoryLeaks();  
    system("pause");  
    return;  
}  





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值