循环链表(circular linked list )

对于单链表, 由于每个节点只存储了向后的指针,到了尾部标识就停止了向后链的操作,也就是说按照这样的方式,只能索引后继节点不能索引前驱节点。
这会带来什么问题呢?
例如不从头节点出发,就无法访问到全部节点。
要解决这个问题,只需要将单链表中终端节点的指针端由空指针改为指向头节点,问题就解决了。
将单链表中终端节点的指针端由空指针改为指向头结点,就使整个单链表形成一个环,这种头尾相接的单链表称为***循环单链表****,简称循环链表。如下图:

《循环链表(circular linked list )》 图片.png

注·这里并不是说循环链表一定要有头结点。

  1. 其实循环链表与单链表的主要差异就在于循环的判断空链表的条件上,原来判断head->next是否为null,现在则是head->next是否等于head。
  2. 由于终端节点用尾指针rear指示,这查询终端节点是O(1),而开始节点是rear->next->next,当然也是O(1)。

循环链表的代码实现(C语言)

  1. 循环链表的初始化
typedef int ElemType;
typedef struct CLinkList
{
    ElemType data;
    struct CLinkList *next;
} node;
//初始化循环链表
void ds_init(node **pNode) {
    int item;
    node *temp;
    node *target;
    printf("输入节点的值,输入0完成初始化\n");
    while (1)
    {
        scanf("%d", &item);
        if (item == 0)
        {
            return;
        }
        if ((*pNode) == NULL)
        {
            //循环链表只有一个节点
            *pNode = (node *)malloc(sizeof(struct CLinkList));
            if (!(*pNode))
            {
                exit(0);
            }
            (*pNode)->data = item;
            (*pNode)->next = *pNode;
        }
        else
        {
            //找到next指向第一个节点的节点
            for (target == (*pNode); target->next != (*pNode); target = target->next)
            {
            }
            //生成一个新节点
            temp = (node *)malloc(sizeof(struct CLinkList));
            if (!temp)
            {   
                exit(0);
            }
            temp->data = item;
            temp->next = *pNode;
            target->next = temp;
        }
    }
}
  1. 循环链表中插入节点
//插入节点
//参数:链表的第一个节点,插入位置
void ds_insert(node **pNode, int i) {
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;
    printf("输入要插入节点的值:");
    scanf("%d", &item);
    if (i == 1) {
        //新插入的节点作为第一个节点
        temp = (node*)malloc(sizeof(struct CLinkList));
        if (!temp)
        {
            exit(0);
        }
        temp->data = item;
        //寻找最后一个节点
        for (target = (*pNode); target->next != (*pNode); target = target->next)
        {

        }
        temp->next = (*pNode);
        target->next = temp;
        *pNode = temp;
    }
    else
    {
        target = *pNode;
        for (; j < (i - 1); ++j)
        {
            target = target->next;
        }
        temp = (node*)malloc(sizeof(struct CLinkList));
        temp->data = item;
        p = target->next;
        target->next = temp;
        temp->next = p;
    }
}
  1. 循环链表中删除节点
//删除节点
void ds_delete(node **pNode, int i) {
    node *target;
    node *temp;
    int j = 1;
    if (i == 1)
    {
        //删除的是第一个节点
        //找到最后一个节点
        for (target = (*pNode); target->next != (*pNode); target = target->next)
        {

        }
        temp = *pNode;
        *pNode = (*pNode)->next;
        target->next = *pNode;
        free(temp);
    }
    else
    {
        target = *pNode;
        for (; j < i - 1; ++j)
        {
            target = target->next;
        }
        temp = target->next;
        target->next = temp->next;
        free(temp);
    }
}
  1. 循环链表中查找节点所在位置
//返回节点所在的位置
int ds_search(node *pNode, int elem) {
    node *target;
    int i = 1;
    for (target = pNode; target->data != elem && target->next != pNode; ++i)
    {
        target = target->next;
    }
    if (target->next == pNode)
    {
        //表中不存在该元素
        return 0;
    }
    else
    {
        return i;
    }
}
    原文作者:Ace严
    原文地址: https://www.jianshu.com/p/13ecc62d98de
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞