单向循环链表删除尾结点,成功返回true,失败返回false

/*
 * ==============================================================
 * File name:        del_tail_node_in_circular_list.c
 * Author:           3360652783@qq.com
 * Date created:     2026-07-23
 * Description:      Delete the tail node in a circular singly linked list with head node.
 * Copyright notice: This code is for course "Data Structure" only. Please cite the source when reprinting.
 * ==============================================================
 */

//构建删除单向循环链表中尾结点的函数
bool LastNode_del(*L){
    CirLinkList *p = L->next;         //指向当前结点
    CirLinkList *p_prev = L;          //指向当前结点的直接前驱结点
    if( L->next == L ){
        return false;                 //如果链表为空,则删除失败
    }
    while( p->next != L->next ){      //遍历链表,找到链表的最后一个结点
        p_prev = p;
        p = p->next;
    }
    p_prev->next = p->next;         //将尾结点前驱结点的next指向第一个结点
    p->next = NULL;
    free(p);                       //删除最后一个结点并回收空间
    if( L->next == NULL ){         //如果删除后仅剩头结点则将头结点的指针指向自己
        L->next = L;
    }
    return true;
}
posted @ 2026-07-23 20:16    阅读(1)  评论(0)    收藏  举报