一、单链表结构定义
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; //< 链表头结点
}myLinkList;
/// @brief 在位序pos处插入新结点
/// @param plist 链表指针
/// @param pos 位序
/// @param x 待插元素
/// @return 返回是否插入成功的标志
/// @retval ERROR(0):链表不存在,不可操作
/// @retval ERR_OVERFLOW(-2):新建结点失败,导致插入失败
/// @retval ERR_PARA(-1):插入失败,pos值小于1或大于size + 1
status xxx_insert(myLinkList* plist, int pos, linkType x)
{
if (plist == NULL)
{
return ERROR;
}
return xxx_insert_(plist->phead, pos, x);
}
status xxx_insert_(myLNode* phead, int pos, linkType x)
{
if (phead == NULL)
{
return ERROR;
}
int i = 0;
myLNode* p = phead;
/// 定位到第pos-1个结点
while (p != NULL && i < pos - 1)
{
p = p->pnext;
++i;
}
/// pos值不合理。pos取值范围是[1,size+1]
if (p == NULL || i > pos - 1)
{
return ERR_PARA;
}
myLNode* pnew = xxx_createNode(x);
if (pnew == NULL)
{
return ERR_OVERFLOW;
}
pnew->pnext = p->pnext;
p->pnext = pnew;
return OK;
}
标签:结点,单链,return,pos,链表,phead,表第,NULL From: https://www.cnblogs.com/kxwslmsps/p/17120791.html