上一篇博客讲述了不带头节点的单链表,这篇博客就是有关带头结点单链表的基本函数
结构体:
typedef int DataType;
typedef struct SHListNode
{
struct SHListNode* _pNext;
DataType _data;
}SHListNode,Node;
基本函数:
SHListNode* BuySHListNode(DataType data)//创建一个节点
{
SHListNode* pNewNode = (SHListNode*)malloc(sizeof(SHListNode));
if (NULL == pNewNode)
{
assert(0);
return NULL;
}
pNewNode->_data = data;
pNewNode->_pNext = NULL;
return pNewNode;
}
void SHListInit(SHListNode** pHead)//头节点
{
assert(pHead);
*pHead = BuySHListNode(0);
}
void SHListPushBack(SHListNode* pHead, DataType data)//尾插
{
Node* pTailNode = pHead;
assert(pHead);
while (pTailNode->_pNext)
{
pTailNode = pTailNode->_pNext;
}
pTailNode->_pNext = BuySHListNode(data);
}
//空链表:pHead->头节点:->NULL
//非空链表
void SHListPopBack(SHListNode* pHead)//尾删
{
Node* pTailNode = pHead;
Node* pre = NULL;
assert(pHead);
if (NULL == pHead->_pNext)
{
return 0;
}
while (pTailNode->_pNext)
{
pre = pTailNode;
pTailNode = pTailNode->_pNext;
}
pre->_pNext = NULL;
free(pTailNode);
pTailNode = NULL;
}
void SHListPushFront(SHListNode* pHead, DataType data)//头插
{
Node* pNewNode = NULL;
assert(pHead);
pNewNode = BuySHListNode(data);
if (pNewNode != NULL)
{
pNewNode->_pNext = pHead->_pNext;
pHead->_pNext = pNewNode;
}
}
void SHListPopFront(SHListNode* pHead)//头删
{
Node* pDelNode = NULL;
assert(pHead);
if (NULL == pHead->_pNext)
{
return;
}
pDelNode = pHead->_pNext;
pHead->_pNext = pDelNode->_pNext;
free(pDelNode);
pDelNode = NULL;
}