一、单链表结构定义
typedef void(myOpFunType)(void*); ///< 定义操作函数类型
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; //< 链表头结点
}myLinkList;
二、清空单链表函数
/// @mybrief 清空单链表
/// @param plist:链表结构指针
/// @return 无返回值
void xxx_clear(myLinkList* plist)
{
if (plist == NULL||plist->phead==NULL)
{
return;
}
xxx_clear_(plist->phead);
}
void xxx_clear_(myLNode* phead)
{
if (phead == NULL)
{
return;
}
myLNode* pdel = phead->pnext;
while (pdel != NULL)
{
/// 当pdel指向尾结点时,下句包含着将phead指针域置空
phead->pnext = pdel->pnext;
free(pdel);
pdel = phead->pnext;
}
}
三、销毁单链表函数
void xxx_destroy(myLinkList* plist)
{
if (plist == NULL)
{
return;
}
xxx_destroy_(plist->phead);
free(plist->phead);
plist->phead = NULL;
}
void xxx_destroy_(myLNode* phead)
{
if (phead == NULL)
{
return;
}
myLNode* pdel = phead->pnext;
while (pdel != NULL)
{
phead->pnext = pdel->pnext;
free(pdel);
pdel = phead->pnext;
}
}
四、求单链表结点个数
/// @brief 求链表结点个数
/// @param plist 链表指针
/// @return 返回结点个数
/// @retval -1:链表不存在
/// @retval 非负整数:链表结点个数
int xxx_size(const myLinkList* plist)
{
if (plist == NULL)
{
return -1;
}
return xxx_size_(plist->phead);
}
int xxx_size_(const myLNode* phead)
{
if (phead == NULL)
{
return -1;
}
myLNode* p = phead->pnext;
int i = 0;
while (p != NULL)
{
++i;
p = p->pnext;
}
return i;
}
五、判空函数
/// @brief 判空函数
/// @param plist 链表指针
/// @return status:返回链表是否为空
status xxx_empty(const myLinkList* plist)
{
if (plist == NULL || plist->phead == NULL)
{
return ERROR;
}
return plist->phead->pnext == NULL ? TRUE : FALSE;
}
标签:结点,单链,return,链表,phead,判空,NULL,plist,pnext From: https://www.cnblogs.com/kxwslmsps/p/17120555.html