单向链表的操作

一、基本操作补充

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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值