欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

循环链表的插入删除实现

程序员文章站 2024-03-19 19:35:10
...

循环链表的插入、删除

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

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;需要考虑,即为第一个节点的删除。