数据结构之单链表
单链表操作简单,下边提供逆序与插入排序的代码。
template<class T>
struct Node
{
T Val;
Node<T>* next;
};
template <class T>
class LinkList
{
public:
LinkList();
LinkList(const T* a, const int nLen);
void InsertHead(const T& Val);
void InsertTail(const T& Val);
bool InsertByPos(const int nIndex, const T& Val);
void DeleteHead();
void DeleteTail();
bool DeleteByPos(const int nIndex);
//void DeleteByRange(const int nStart,const int nEnd);
Node<T>* FindByPos(const int nIndex);
Node<T>* FindByVal(const T& Val);
int Length()const;
Node<T>* reserve();
void Sort();
void PrintList()const;
void Clear();
~LinkList();
//Node<T>* operator[](const int nIndex);
//Node<T>* operator++(const Node<T>* pNode);
protected:
Node<T>* pHead;
int m_nLen;
};
template<class T>
LinkList<T>::LinkList()
{
pHead = nullptr;
m_nLen = 0;
}
template<class T>
LinkList<T>::LinkList(const T *a, const int nLen)
{
if (nLen < 1 || nullptr == a)
{
throw "参数非法";
}
for (int i = 0; i < nLen; ++i)
{
InsertTail(a[i]);
}
}
//头插
template<class T>
void LinkList<T>::InsertHead(const T& Val)
{
Node<T>* pNew = new Node<T>;
pNew->Val = Val;
pNew->next = pHead;
pHead = pNew;
++m_nLen;
}
//尾插
template<class T>
void LinkList<T>::InsertTail(const T& Val)
{
if (nullptr == pHead)
{
InsertHead(Val);
return;
}
Node<T>* pTemp = pHead;
while (pTemp->next != nullptr)
{
pTemp = pTemp->next;
}
Node<T>* pNew = new Node<T>;
pNew->Val = Val;
pNew->next = nullptr;
pTemp->next = pNew;
++m_nLen;
}
//按位置插入
template<class T>
bool LinkList<T>::InsertByPos(const int nIndex, const T& Val)
{
if (nIndex > m_nLen || nIndex < 0)
{
return false;
}
int nPos = 0;
Node<T>* pTemp = pHead;
while (nPos < nIndex&&pTemp != nullptr)//注意按位置插入时 默认规则 与边界
{
pTemp = pTemp->next;
}
Node<T>* pNew = new Node<T>;
pNew->Val = Val;
pNew->next = pTemp->next;
pTemp->next = pNew;
++m_nLen;
return true;
}
//遍历输出
template<class T>
void LinkList<T>::PrintList()const
{
Node<T>* pTemp = pHead;
while (pTemp != nullptr)
{
cout << pTemp->Val << " ";
pTemp = pTemp->next;
}
}
template<class T>
void LinkList<T>::DeleteHead()
{
if (nullptr == pHead)
{
return;
}
Node<T>* pTemp = pHead;
pHead = pHead->next;
delete pTemp;
pTemp = nullptr;
--m_nLen;
}
template<class T>
void LinkList<T>::DeleteTail()
{
if (nullptr == pHead || nullptr == pHead->next)
{
DeleteHead();
return;
}
Node<T>* pTemp = pHead;
while (pTemp->next->next != nullptr)
{
pTemp = pTemp->next;
}
delete pTemp->next;
pTemp->next = nullptr;
--m_nLen;
}
template<class T>
bool LinkList<T>::DeleteByPos(const int nIndex)
{
if (nIndex < 0 || nIndex >= m_nLen)
{
return false;
}
if (0 == nIndex)
{
DeleteHead();
}
Node<T>* pTemp = PHead;
for (int i = 0; i < nIndex - 1; ++i)
{
pTemp = pTemp->next;
}
Node<T>* pNode = pTemp->next;
pTemp->next = pTemp->next->next;
delete pNode;
pNode = nullptr;
--m_nLen;
}
template<class T>
Node<T>* LinkList<T>::FindByPos(const int nIndex)
{
if (nIndex < 0 || nIndex >= m_nLen)
{
return nullptr;
}
Node<T>* pTemp = pHead;
for (int i = 0; i < nIndex; ++i)
{
pTemp = pTemp->next;
}
return pTemp;
}
template<class T>
Node<T>* LinkList<T>::FindByVal(const T& Val)
{
int nCount = 0;
Node<T>* pTemp = pHead;
while (nullptr != pTemp)
{
if (pTemp->Val == Val)
{
break;
}
pTemp = pTemp->next;
}
return pTemp;
}
template<class T>
int LinkList<T>::Length()const
{
return m_nLen;
}
template<class T>
void LinkList<T>::Clear()
{
while (pHead != nullptr)
{
DeleteTail();
}
}
template<class T>
LinkList<T>::~LinkList()
{
Clear();
}
//-------------------------------------重要函数
template<class T>
Node<T>* LinkList<T>::reserve()
{
if (nullptr == pHead || nullptr == pHead->next)
{
return pHead;
}
Node<T>* p1, p2, p3;
p1 = pHead;
p2 = p1->next;
p3 = p2->next;
while (1)
{
p2->next = p1;
if (nullptr == p3)
{
break;
}
p1 = p2;
p2 = p3;
p3 = p2->next;
}
pHead->next = nullptr;
pHead = p2;
return pHead;
}
template<class T>
void LinkList<T>::Sort()
{
if (nullptr = pHead || nullptr == pHead->next)
{
return;
}
Node<T>* pOldHead = pHead->next;//保存
Node<T>* pCur = pOldHead;//保存带插入节点
Node<T>* pPre = nullptr;//保存带插入位置前一节点
Node<T>* pTemp = nullptr;//游标节点
pHead->next = nullptr;//新链表头结点
int nPos = 0;
while (pOldHead != nullptr)
{
pCur = pOldHead;
pPre = nullptr;
pOldHead = pOldHead->next;
pTemp = pHead;
while (pTemp != nullptr&&pTemp->Val <= pCur->Val)
{
pPre = pTemp;
pTemp = pTemp->next;
}
if (nullptr = pPre)//插在首节点
{
pCur->next = pHead;
pHead = pCur;
}
else//插入至相应位置
{
pCur->next = pPre->next;
pPre->next = pCur;
}
}
}