首页 > 其他分享 >【数据结构】链式型存储结构-循环单链表

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

时间:2023-04-30 21:12:10浏览次数:37  
标签:node 结点 单链 target temp next pNode 链式 数据结构

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

4  小结

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

标签:node,结点,单链,target,temp,next,pNode,链式,数据结构
From: https://www.cnblogs.com/kukuxjx/p/17365655.html

相关文章

  • 【数据结构】链式型存储结构-静态链表
    1 前言地球人都知道C语言是个伟大的语言,它的魅力在于指针的灵活性,使得它可以非常容易地操作内存中的地址和数据,这比其他高级语言更加灵活方便。(面向对象语言,比如java,可以使用对象引用机制间接地实现指针的某些功能)但是古人还是木有C语言丫,木有JAVA丫,只有原始的Basic,Fortran等......
  • 【数据结构】链式型存储结构-单链表
    1 前言线性表的链式存储结构的特点就是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以在内存中未被占用的任意位置。比起顺序存储结构每个元素只需要存储一个位置就可以了。现在链式存储结构中,除了要存储数据信息外,还要存储它的后继元素的存储地址(指针)。也就是说......
  • 【数据结构】线性表分类以及顺序型存储结构
    1 什么是线性表线性表的定义:由零个或多个数据元素组成的有限序列首先它是一个序列,也就是说元素之间是有先来后到之分。若元素存在多个,则第一个元素无前驱,而最后一个元素无后继,其他元素都有且只有一个前驱和后继。线性表强调是有限的,事实上无论计算机发展到多强大,他所能处理......
  • 数据结构与算法基础复习--(1)
    基本术语1.数据(Data)数据是能输入计算机且能被计算机处理的各种符号的集合信息的载体是对客观事物符号化的表示能够被计算机识别、存储和加工包括:数值型的数据:整数、实数等非数值型的数据:文字、图像、图形、声音等2.数据元素数据元素是数据的基本单位,在计......
  • c#-单链表
    namespaceMyLink;publicclassMyLinkedList{privateint_size{get;set;}publicclassMyTreeNode{publicintval{get;set;}publicMyTreeNodenext{get;set;}publicMyTreeNode(intval){this.val=val;......
  • C语言链式存储(使用引用传递)
    #include<stdio.h>#include<stdlib.h>typedefstructLinkNode{ intdata; structLinkNode*next;}LinkNode;typedefstructLink{ LinkNode*front,*rear;//frontrear为链表的伴随指针}LinkQueue;voidInitQueue(LinkQueue&Q){Q.front=Q.rear......
  • c语言创建队列的链式存储
    #include<stdio.h>#include<stdlib.h>typedefstructLinkNode{intdata;structLinkNode*next;}LinkNode;typedefstructLink{LinkNode*front,*rear;//frontrear为链表的伴随指针}LinkQueue;//初始化voidInitQueue(LinkQueue*......
  • 数组模拟实现数据结构
    数组模拟链表实现①单链表:邻接表(存储图和树)②双链表:优化某些问题单链表inte[N]存储val,intne[N]存储next//单链表模板inthead,e[N],ne[N],idx;//head表示头节点的下标,e[i]表示节点i的值,ne[i]表示节点i的指针是多少,idx存储当前已经用到了哪个点v......
  • 【Redis】Redis数据结构——链表
    【Redis】Redis数据结构——链表注意事项:本文第三点redis中操作列表的相关命令可参考博文:【Redis】Redis基础命令集详解_Etui۹(・༥・´)و̑̑的博客本文参考内容如下:1、Redis数据结构——链表-随心所于-2、《Redis设计与实现》(黄健宏·著)第3章链表本文仅供学习交流使用!1、Redi......
  • 《大话数据结构》读书笔记 附PDF #C3
    刚刚读完了《大话数据结构》,这本书真的是一本不错的入门级别的数据结构和算法的教材。首先,作者通过幽默的语言和丰富的图示,使得枯燥的数据结构与算法变得生动有趣。在阅读过程中,我感受到了作者对于知识点深入浅出的讲解,即使是像我这样初学者也能够轻松理解。其次,书中的配套练习......