template<typename T>
struct Node
{
T m_Data;
Node * m_pNext;
};
template<typename T>
bool BubbleSort(Node<T> * & pHead)
{
Node<T> * pCurNode = NULL;
Node<T> * pTemp = NULL;
Node<T> * pNext = NULL;
T tTemp;
bool bChange = false;
if (!pHead)
return false;
pCurNode = pHead;
while (pCurNode)
{
pTemp = pHead;
pNext = pTemp->m_pNext;
bChange = false;
while (pTemp && pNext)
{
if (pTemp->m_Data > pNext->m_Data)
{
tTemp = pTemp->m_Data;
pTemp->m_Data = pNext->m_Data;
pNext->m_Data = tTemp;
bChange = true;
}
pTemp = pTemp->m_pNext;
pNext = pTemp->m_pNext;
}
if (!bChange)
break;
pCurNode = pCurNode->m_pNext;
}
return true;
}
模版冒泡
最新推荐文章于 2018-08-02 10:18:05 发布