一、头插法
代码
/// @brief 在链表头部插入元素x,即头插法
/// @param plist 链表指针
/// @param x 待插元素
/// @return 返回插入是否成功
/// @retval ERROR(0):链表不存在,不可操作
/// @retval - ERR_OVERFLOW(-2):创建新结点失败,插入失败
/// @retval OK(1):插入成功
status xxx_push_front(myLinkList* plist, linkType x)
{
if (plist == NULL)
{
return ERROR;
}
return xxx_push_front_(plist->phead, x);
}
status xxx_push_front_(myLNode* phead, linkType x)
{
if (phead == NULL)
{
return ERROR;
}
/// 创建新结点,存放x元素
myLNode* pnew = xxx_createNode(x);
if (pnew == NULL)
{
return ERR_OVERFLOW;
}
pnew->pnext = phead->pnext; ///< 建新链
phead->pnext = pnew; ///< 断旧链
return OK;
}