一、定义单链表结构
- 代码:
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; //< 链表头结点指针
}myLinkList;
二、插入函数
1、思路
- 代码:
/// 题目:在元素值等于key的结点前插入元素值为x的新结点
/// 1、定义遍历指针p,前驱指针pre指向p结点前驱,新结点指针pnew
/// 2、遍历链表,结点值等于key时,在pre与p结点之间插入pnew结点
/// 3、值不等于key时,跳过该结点,继续遍历
void yyy_insert(myLNode* phead, linkType key, linkType x)
{
if (phead == NULL || phead->pnext == NULL)
{
return;
}
myLNode* p = phead->pnext;
myLNode* pre = phead;
myLNode* pnew = NULL;
while (p != NULL)
{
if (p->data == key)
{
pnew = (myLNode*)malloc(sizeof(myLNode));
assert(pnew);
pnew->data = x;
pnew->pnext = pre->pnext;
pre->pnext = pnew;
}
pre = p;
p = p->pnext;
}
}
标签:结点,单链,元素,myLNode,pnew,phead,key,pnext
From: https://www.cnblogs.com/kxwslmsps/p/17125286.html