循环链表的插入删除实现

循环链表的插入、删除

循环链表的插入示意:
循环链表的插入
循环链表的删除示意:
循环链表的删除

typedef void CircleList;

typedef struct _tag_CircleListNode
{
    struct _tag_CircleListNode * next;
}CircleListNode;

typedef struct _tag_CircleList
{
    CircleListNode header;
    CircleListNode* slider;
    int length;
}TCircleList;

//插入,对应第一幅示意图
int CircleList_insert(CircleList* list, CireListNode* node, int pos)
{
    int ret = 0, i=0;
    TCircleList* sList = (TCircleList*)list;

    if (list == NULL || node== NULL || pos<0)
    {
        return -1;
    }

    CircleListNode* current = (CircleListNode*)sList;

        for(i=0; (i<pos) && (current->next != NULL); i++)
        {
            current = current->next;
        }

        //current->next 0号节点的地址
        node->next = current->next; //1
        current->next = node; //2

        //若第一次插入节点
        if( sList->length == 0 )
        {
            sList->slider = node;
        }

        sList->length++;

        //若头插法 current仍然指向头部
        //(原因是:跳0步,没有跳走) 中间第一种情况
        if( current == (CircleListNode*)sList )
        {
            //获取最后一个元素
            CircleListNode* last = CircleList_Get(sList, sList->length - 1); 
            last->next = current->next; //3
        }

        return ret;
}

CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n)
{
    TCircleList* sList = (TCircleList*)list;
    CircleListNode* ret = NULL;
    int i = 0;

    if (list==NULL || pos<0)
    {
        return NULL;
    }

    {
        CircleListNode* current = (CircleListNode*)sList;

        for(i=0; i<pos; i++)
        {
            current = current->next;
        }

        ret = current->next;
    }

    return ret;
}

//删除,对应第二幅图
CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n)
{
    TCircleList* sList = (TCircleList*)list;
    CircleListNode* ret = NULL;
    int i = 0;

    if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
    {
        CircleListNode* current = (CircleListNode*)sList;
        CircleListNode* last = NULL;

        for(i=0; i<pos; i++)
        {
            current = current->next;
        }

        //若删除第一个元素(头结点)左上
        if( current == (CircleListNode*)sList )
        {
            last = (CircleListNode*)CircleList_Get(sList, sList->length - 1);
        }

        //求要删除的元素  右上 左上
        ret = current->next;
        current->next = ret->next;

        sList->length--;

        //判断链表是否为空
        if( last != NULL )
        {
            sList->header.next = ret->next;
            last->next = ret->next;
        }

        //若删除的元素为游标所指的元素
        if( sList->slider == ret )
        {
            sList->slider = ret->next;
        }

        //若删除元素后,链表长度为0
        if( sList->length == 0 )
        {
            sList->header.next = NULL;
            sList->slider = NULL;
        }
    }

    return ret;
}

总结

循环链表的插入:注意第一次插入的条件判断(维护游标);只有一个头结点的插入
循环链表的删除:注意删除第一个元素(头结点);current->next = current->next->next;不需要考虑链表是否为空,而last->next = ret->next;需要考虑,即为第一个节点的删除。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tingzhushaohua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值