一、内存探测工具valgrind

1.网络配置

2. 检测是否存在内存泄漏

二、查找中间结点(运用快慢指针法)

 1.快慢指针法

2.程序

 Node_t *find_Mid_node(Link_t *plink)
{
    if(is_empty_link(plink))
    {
        return NULL;
    }
    
    Node_t *pfast = plink ->phead;
    Node_t *pslow = pfast;
    while(pfast != NULL)
    {
        pfast = pfast->pnext;
        if(NULL == pfast)
        {
            break;
        }
        pfast = pfast->pnext;
        pslow = pslow->pnext;
    }
    return pslow;
}

二、查找k为倒数第N个结点,返回该结点的地址

程序

 Node_t *find_last_node(Link_t *plink,int k)
{
    Node_t *pfast;
    Node_t *pslow;
    pfast = plink->phead;
    pslow = pfast;
    
    for(int i = 0;i < k; ++i)
    {
        if(NULL == pfast)
        {
            return NULL;    
        }
        pfast = pfast->pnext;
    }
    
    while(pfast != NULL)
    {
    
        pslow = pslow->pnext;
        pfast = pfast->pnext;
    }
    return pslow;
}

三、链表逆序

1.思考过程

2.link.c程序

 void link_reverse(Link_t *plink)
{
    if(is_empty_link(plink))
    {
        return ;
    }
    Node_t *ptmp = plink->phead;
    plink->phead = NULL;
    while(ptmp != NULL)
    {
        Node_t *pinsert = ptmp;
        ptmp = ptmp->pnext;
        
        pinsert->pnext = plink->phead;
        plink->phead = pinsert;
    }
    return ;
}
        

四、插入排序

1.思考过程

2.程序

void link_sort(Link_t *plink)
{
    if(is_empty_link(plink) || 1 == plink->clen)
    {
        return ;
    }
    Node_t *ptmp = plink->phead->pnext;
    plink->phead->pnext = NULL;

    while(ptmp != NULL)
    {
        Node_t *pinsert = ptmp;
        ptmp = ptmp->pnext;
    

        if(plink->phead->data >= pinsert->data)
        {    
            pinsert->pnext = plink->phead;
            plink->phead = pinsert;
        }
        else
        {
            Node_t *p = plink->phead->pnext;
            while(p -> pnext != NULL && pinsert->data > p->pnext->data)
            {
                p = p->pnext;
            }
            pinsert->pnext = p->pnext;
            p->pnext = pinsert;
        }

    }
    return ;
}

 五、循环链表

1.定义

        循环链表是一种特殊的链表,其最后一个节点的next指针不指向NULL,而是指向头节点,形成一个闭环

2.特点

  • 从任意节点出发可遍历整个链表;
  • 无需判断next == NULL,而是判断是否回到起点;
  • 适合实现环形队列、约瑟夫问题等场景。

3.创建程序

4.判断是否有环

5.main函数测试

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐