【数据结构】链式型存储结构-循环单链表

1  前言

对于单链表,由于每个结点只存储了向后的指针,到了尾部标识就停止了向后链的操作。也就是说,按照这样的方式,只能索引后继结点不能索引前驱结点。这样一来,不从头结点出发,这样就无法访问到全部结点。

为了解决这个问题,我们只需要将单链表的尾结点的指针由空指针改为指向头结点的指针。

2  循环链表

将单链表中尾结点的指针由空指针改为指向头结点,就使整个单链表形成一个环,这种头尾相接的单链表成为单循环链表,简称循环链表。

其实循环链表的单链表的主要差异就在于循环的判断空链表的条件上,原来判断head->next是否为空,现在则是head->next是否等于head;

终端结点用尾指针rear指示,则查找终端结点是O(1),而开始结点是rear->next->next,当然也是O(1)

3  循环链表的操作

3.1  循环链表的初始化以及插入

typedef struct CLinkList
{
    int data;
    struct CLinkList *next;
}node;

void ds_init(node **pNode)
{
    int item;
    node *temp;
    node *target;
    
    printf("请输入结点的值,输入0完成初始化\n");
    
    while(1)
    {
        scanf("%d",&item);
        fflush(stdin);
        
        if(item == 0)
            return;
        
        if((*pNode) == NULL)
        {
            *pNode = (node*)malloc(sizeof(struct CLinkList));
            
            if(!(*pNode))
                exit(0);
            
            (*pNode)->data = item;
            (*pNode)->next = *pNode;
        }
        else
        {
            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;
        }
    }
}
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));
        
        if(!temp)
            exit(0);
        
        temp->data = item;
        
        p = target->next;
        target->next = temp;
        temp->next = p;
    }
}

3.2  循环链表的删除

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);
    }
}

3.3  循环链表的查找

/*返回结点所在位置*/
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) /*表中不存在该元素*/
  {
      if(target->data == elem){
            return i;
      }
      return 0;
  }
    else
        return i;
}

4  引申

4.1  实现将两个线性表连接成一个线性表的运算

  • 若在单链表或头指针表示的单链表上做这种链接操作,都需要遍历第一个链表,找到最后一个结点,然后将第二个链表链接到第一个的后面,其执行时间是O(n)

  • 若在尾指针表示的单循环链表上实现,则只需要修改指针,无需遍历,其执行时间是O(1)

LinkList Connect(LinkList A, LinkList B)
{
    LinkList p = A->next;
    A->next = B->next->next;
    
    free(B->next);
    
    B->next = p;
    
    return B;
}

5  小结

好了,本节循环单链表就看到这里,就是在单链表的基础上,尾结点不指向空了,指向头结点了哈,复杂度相较于单链表其实基本一样的哈,有理解不对的地方欢迎指正哈。

posted @ 2023-04-30 20:59  酷酷-  阅读(57)  评论(0编辑  收藏  举报