一、定义单链表结构
- 代码:
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 删除表中第pos个结点
/// @param plist 链表指针
/// @param pos 位序
/// @return status 是否成功删除的标志
/// @retval ERROR(0):表不存在或为空,不可操作
/// @retval ERR_PARA(-1):pos位序不合理,删除失败
/// @retval OK(1):删除成功
status xxx_remove(myLinkList* plist, int pos)
{
if (plist == NULL)
{
return ERROR;
}
return xxx_remove_(plist->phead, pos);
}
status xxx_remove_(myLNode* phead, int pos)
{
if (phead == NULL || phead->pnext == NULL)
{
return ERROR;
}
int i = 0;
myLNode* pre = phead; ///< 记录第pos个结点的前驱
while (pre != NULL && i < pos - 1)
{
pre = pre->pnext;
++i;
}
if (pre == NULL || pre->pnext == NULL || i > pos - 1)
{
return ERR_PARA;
}
myLNode* pdel = pre->pnext;
pre->pnext = pdel->pnext; ///< 建新链
pdel->pnext = NULL; ///< 断旧链
free(pdel);
pdel = NULL;
return OK;
}
标签:pre,结点,单链,pos,phead,NULL,DS,pnext
From: https://www.cnblogs.com/kxwslmsps/p/17125280.html