美文网首页
C语言-循环链表

C语言-循环链表

作者: AibWang | 来源:发表于2018-11-10 17:46 被阅读0次

循环链表可以是首尾相连,也可以是在链表的某一位置有一个loop。


循环链表示意图1.png

下面给出一个创建循环链表的函数creatCircleList

Node * creatCircleList(float *data, int totalL, int circleL){
        /*
            Input PARA:
                float *data:  the input data for list
                int totalL: the total number of all nodes
                int circleL: the node number of circle
        */
        if(totalL<circleL || totalL < 3 || circleL<2){
                printf("The total length is less than 3 or the circle length is less than 2!!!");
                return(NULL);
        }
        //
        Node *head, *end;
        Node *node1;
        Node *nodestc;//the position for circle start
        int ii,k;
        head = (Node *)malloc(sizeof(Node));
        head->flag = 1;
        end = head;
        for(ii=0;ii<totalL-circleL;ii++){
            node1 =  (Node *)malloc(sizeof(Node));
            node1->value = data[ii];
            end->next = node1;
            end = node1;
        }
        // the first node of circle
        node1 =  (Node *)malloc(sizeof(Node));
        node1->value = data[ii];
        end->next = node1;
        end = node1;
        nodestc = end;
        for(k=1;k<circleL;k++){
            node1 =  (Node *)malloc(sizeof(Node));
            node1->value = data[ii+k];
            end->next = node1;
            end = node1;
        }
        end->next = nodestc;
        // return
        return(head);
    }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上述这个写法显得有些笨拙,也是我第一次写出来的,后来看了网上大神留下的c++代码,将过程简化:

  1. 创建一个单项链表,记住circle的第一个节点位置,最后将单向链表的尾节点“接到”该节点即可。
Node * creatCircleList(float *data, int totalL, int circleL){
        /*
            Input PARA:
                float *data:  the input data for list
                int totalL: the total number of all nodes
                int circleL: the node number of circle
        */
        if(totalL<circleL || totalL < 3 || circleL<2){
                printf("The total length is less than 3 or the circle length is less than 2!!!");
                return(NULL);
        }
        //
        Node *head, *end;
        Node *cirnode1st_posi;// the first node of circle
        Node *node1;
        int ii;
        head = (Node *)malloc(sizeof(Node));
        head->flag = 1;
        end = head;
        for(ii=0;ii<totalL;ii++){
            node1 =  (Node *)malloc(sizeof(Node));
            node1->value = data[ii];
            if(ii == totalL-circleL){
                cirnode1st_posi = node1;
                //printf("first node of circle: %d\n",ii);
            }
            end->next = node1;
            end = node1;
        }
        end->next = cirnode1st_posi;
        // return
        return(head);
    }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

相关文章

  • C语言-循环链表

    循环链表可以是首尾相连,也可以是在链表的某一位置有一个loop。 下面给出一个创建循环链表的函数creatCirc...

  • C++实现双向循环链表

    本次博文是关于利用C++模板的方式实现的双向循环链表以及双向循环链表的基本操作,在之前的博文C++语言实现双向链表...

  • 循环双链表(C语言)

    1、头文件circle_doublelist.h 2、相关操作函数文件circle_doublelist.c 3、...

  • 表、栈和队列

    数据结构与算法分析-c语言描述抽象数据类型(ADT)1、表ADT可以用由数组实现。链表实现。双链表。循环链表。 -...

  • C语言里的循环链表

    定义几个结构体,每个结构体里面包含俩个成员,一个整型变量,一个指针变量。让一个结构体里的指针变量指向另一个结构体...

  • C语言的双向循环链表

    SingleLinkedList.h SingleLinkedList.c main.c result

  • C语言实现双向循环链表

    在之前的文章中,我写过一篇关于C语言实现双向链表博文,介绍了双向链表的实现过程以及双向链表的优势,接下来我首先给大...

  • 链表逆置C语言完整代码

    链表逆置C语言完整代码

  • Java实现简单的链表-面向初学者

    很久之前用C语言实现过链表,现在已经太久没用C语言。就先用JAVA实现一个简单链表好了,还是使用最原始的C语言实现...

  • C语言 单向链表&单向循环链表 算法考察题

    随机生成10个元素,值为10以内,找到元素值为5的元素在这个下标后面插入一个元素值为11。 我认为这个题目主要分成...

网友评论

      本文标题:C语言-循环链表

      本文链接:https://www.haomeiwen.com/subject/jpozxqtx.html