一、基本操作补充
1、查找倒数第k个结点
Link_Node_t *find_num_link(Link_t *link,DATATYPE cnt)
{
if(link->phead == NULL)
{
return NULL;
}
Link_Node_t *pfast = link->phead;
Link_Node_t *pslow = link->phead;
while(cnt != 0)
{
if(NULL == pfast)
{
return NULL;
}
pfast = pfast->pnext;
--cnt;
}
while(pfast != NULL)
{
pslow = pslow->pnext;
pfast = pfast->pnext;
}
return pslow;
}
2、链表倒置
int convert_link(Link_t *link)
{
if(link->phead == NULL)
{
return -1;
}
Link_Node_t *ptempt = link->phead;
link->phead = NULL;
Link_Node_t *pinsert = NULL;
while(ptempt != NULL)
{
pinsert = ptempt;
ptempt = ptempt->pnext;
pinsert->pnext = link->phead;
link->phead = pinsert;
}
return 0;
}
3、删除指定的结点
int pop_point_node(Link_t *plink,int key)
{
if (is_empty_link(plink))
return 0;
Link_Node_t *pdel = plink->phead;
Link_Node_t *ppre = NULL;
while (pdel != NULL)
{
if (pdel->data == key)
{
//del
if (plink->phead == pdel)
{
pop_link_head(plink);
}
else
{
ppre->pnext = pdel->pnext;
free(pdel);
plink->clen--;
}
return 1;
}
else
{
ppre = pdel;
pdel = pdel->pnext;
}
}
return 0;
}
4、插入排序
int insert_sort_link(Link_t *link)
{
if(link->phead == NULL)
{
return -1;
}
Link_Node_t *pnode = link->phead;
Link_Node_t *ptempt = pnode->pnext;
pnode->pnext = NULL;
Link_Node_t *p = NULL;
Link_Node_t *pinsert = NULL;
while(ptempt != NULL)
{//进行比较头结点,如果比头结点小,进行头插
pinsert = ptempt;
ptempt = ptempt->pnext;
if(pinsert->data < link->phead->data)
{
pinsert->pnext = link->phead;
link->phead = pinsert;
}
else
{
p = link->phead;
while(p->pnext != NULL && p->pnext->data < pinsert->data)
{
p = p->pnext;
}
pinsert->pnext = p->pnext;
p->pnext = pinsert;
}
}
}