一、定义单链表结构
- 代码:
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; //< 链表头结点指针
}myLinkList;
二、单链表尾删法
1、思路
- 代码:
/// @brief 删除链表尾结点,即尾删法
/// @param plist 链表指针
/// @return 返回是否删除成功标志
/// @retval ERROR(0):链表不存在或链表为空,不可操作
/// @retval OK(1):删除成功
status xxx_pop_back(myLinkList* plist)
{
if (plist == NULL)
{
return ERROR;
}
return xxx_pop_back_(plist->phead);
}
status xxx_pop_back_(myLNode* phead)
{
if (phead == NULL || phead->pnext == NULL)
{
return ERROR;
}
myLNode* ptail = phead;
myLNode* pre = NULL;
/// 循环让ptail最终指向尾结点,pre指向尾结点前驱
while (ptail->pnext != NULL)
{
pre = ptail;
ptail = ptail->pnext;
}
pre->pnext = NULL;
free(ptail);
ptail = NULL;
return OK;
}
标签:结点,单链,表尾,phead,链表,ptail,NULL,DS
From: https://www.cnblogs.com/kxwslmsps/p/17125276.html