一、定义单链表结构
- 代码:
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; //< 链表头结点指针
}myLinkList;
二、定位第pos个结点
1、思路
- 代码:
/// @brief 定位链表第pos个位序结点
/// @param plist :链表指针
/// @param pos :位序,取值范围是[1,size]
/// @return 返回第pos个结点地址
myLNode* xxx_locate(myLinkList* plist, int pos)
{
if (plist == NULL)
{
return NULL;
}
return xxx_locate_(plist->phead, pos);
}
myLNode* xxx_locate_(myLNode* phead, int pos)
{
if (phead == NULL || phead->pnext == NULL)
{
return NULL;
}
/// 方法1:令p=phead->pnext,i=1,则pos范围为[1,size],不可定位phead
/// 方法2:令p=phead,i=0,则pos范围为[0,size],可定位phead
/// 方法3:p=locatePre(phead,pos),return p->pnext
myLNode* p = phead->pnext;
int i = 1;
while (p != NULL && i < pos)
{
p = p->pnext;
++i;
}
if (i > pos)
{
p = NULL;
}
return p;
}
标签:结点,单链,return,pos,链表,phead,表第,NULL
From: https://www.cnblogs.com/kxwslmsps/p/17125266.html