// SinglyLinkedList.h
#ifndef _SINGLYLINKEDLIST_H_
#define _SINGLYLINKEDLIST_H_
template <typename T>
struct node
{
T data;
node<T> *next;
node<T>(const T &pData)
{
data = pData;
next = NULL;
}
node<T>()
{
data = T();
next = NULL;
}
};
template <typename T>
class SinglyLinkedList
{
private:
node<T> *pHead;
node<T> *pTail;
size_t length;
public:
SinglyLinkedList()
{
pHead = NULL;
pTail = NULL;
length = 0;
}
~SinglyLinkedList()
{
clearAllNodes();
}
node<T> *getListHead()
{
return pHead;
}
node<T> *getListTail()
{
return pTail;
}
node<T> *indexAt(size_t pIndex)
{
if (pIndex < 0 || pIndex >= length)
{
return NULL;
}
node<T> *ptrNodeResult = pHead;
for (size_t i = 0; i < pIndex; i++)
{
ptrNodeResult = ptrNodeResult->next;
}
return ptrNodeResult;
}
bool appendToHead(const T &pData)
{
node<T> *ptrNodeNew = new node<T>(pData);
if (NULL == ptrNodeNew)
{
return false;
}
return appendToHead(ptrNodeNew);
}
bool appendToHead(node<T> *pNode, bool isNewBuild = false)
{
if (NULL == pNode)
{
return false;
}
node<T> *ptrNodeNew = pNode;
if (true == isNewBuild)
{
ptrNodeNew = new node<T>(pNode->data);
if (NULL == ptrNodeNew)
{
return false;
}
}
else if (isNodeExist(pNode))
{
return false;
}
ptrNodeNew->next = pHead;
pHead = ptrNodeNew;
if (NULL == pTail)
{
pTail = pHead;
}
length++;
return true;
}
bool appendToTail(const T &pData)
{
node<T> *ptrNodeNew = new node<T>(pData);
if (NULL == ptrNodeNew)
{
return false;
}
return appendToTail(ptrNodeNew);
}
bool appendToTail(node<T> *pNode, bool isNewBuild = false)
{
if (NULL == pNode)
{
return false;
}
node<T> *ptrNodeNew = pNode;
if (true == isNewBuild)
{
ptrNodeNew = new node<T>(pNode->data);
if (NULL == ptrNodeNew)
{
return false;
}
}
else if (isNodeExist(pNode))
{
return false;
}
if (NULL == pTail)
{
pTail = ptrNodeNew;
pHead = pTail;
}
else
{
pTail->next = ptrNodeNew;
pTail = ptrNodeNew;
}
length++;
return true;
}
bool insertAt(size_t pIndex, const T &pData)
{
if (pIndex < 0 || pIndex > length)
{
return false;
}
node<T> *ptrNodeNew = new node<T>(pData);
if (NULL == ptrNodeNew)
{
return false;
}
if (0 == pIndex)
{
ptrNodeNew->next = pHead;
pHead = ptrNodeNew;
if (NULL == pTail)
{
pTail = pHead;
}
}
else
{
node<T> *ptrNodeBeforeIndex = indexAt(pIndex - 1);
ptrNodeNew->next = ptrNodeBeforeIndex->next;
ptrNodeBeforeIndex->next = ptrNodeNew;
}
length++;
return true;
}
bool deleteAt(size_t pIndex)
{
if (pIndex < 0 || pIndex >= length)
{
return false;
}
if (0 == pIndex)
{
node<T> *ptrNodeTemp = pHead;
pHead = pHead->next;
if (NULL == pHead)
{
pTail = NULL;
}
delete ptrNodeTemp;
}
else
{
node<T> *ptrNodeBeforeIndex = indexAt(pIndex - 1);
node<T> *ptrNodeTemp = ptrNodeBeforeIndex->next;
ptrNodeBeforeIndex->next = ptrNodeBeforeIndex->next->next;
delete ptrNodeTemp;
}
length--;
return true;
}
void clearAllNodes()
{
while (NULL != pHead)
{
node<T> *ptrNodeTemp = pHead;
pHead = pHead->next;
delete ptrNodeTemp;
}
pHead = NULL;
pTail = NULL;
length = 0;
}
void reverse()
{
node<T> *ptrNodeTemp = pHead;
pHead = NULL;
pTail = NULL;
length = 0;
while (NULL != ptrNodeTemp)
{
node<T> *ptrNodeInsert = ptrNodeTemp;
ptrNodeTemp = ptrNodeTemp->next;
appendToHead(ptrNodeInsert);
}
}
bool isNodeExist(const node<T> *pNode)
{
bool isFlag = false;
node<T> *pNodeTemp = pHead;
while (NULL != pNodeTemp)
{
if (pNodeTemp == pNode)
{
isFlag = true;
break;
}
pNodeTemp = pNodeTemp->next;
}
return isFlag;
}
};
#endif
单向单链表
于 2021-10-26 20:28:22 首次发布