zoukankan      html  css  js  c++  java
  • 复习下C 链表操作(单向循环链表、查找循环节点)

    循环链表 稍复杂点。

     肯能会有0 或 6 字型的单向循环链表。  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点。

    这里已6字型单向循环链表为例。

    //创建 循环链表
    Student * CreateCircleLink_Table(void){
        int i = 0;
        Student *head = NULL;
        head = (Student *)malloc(sizeof(Student));
        head->name[0]='';
        head->point = 0;
        head->stu = NULL;
        //循环节点
        Student *circleStu = NULL;
        Student *temp = NULL;
        Student *currentNode = head;
        while (i<=9) {
    
            temp = (Student *)malloc(sizeof(Student));
            strncpy(temp->name,"Node",sizeof(temp->name));
            temp->point = i;
            temp->stu = NULL;
            
            currentNode->stu = temp;
            currentNode = temp;
            
            //循环节点
            if (i==3) {
                circleStu = currentNode;
            }
            
            i++;
        }
        
        //最后 合并循环节点
        currentNode->stu = circleStu;
        
        return head;
    }
    
    //已知循环节点 查询 主要为了验证循环链表是否可用
    void SelectCircleLinkTable(Student *student){
        Student *next = student->stu;
        int i = 0;
        Student *circleStu = NULL;
    
        while (next) {
            if (circleStu!=NULL&&next->point == circleStu->point) {
                printf("循环节点%d,结束循环
    ",next->point);
                break;
            }
            
            if (i==3) {
                circleStu = next;
            }
            printf("index %d; studentName is %s;  point is %d
    ",i,next->name,next->point);
            i++;
            next = next->stu;
        }
    }
    
    //查找循环链表中循环节点
    Student * SelectCircleNodeInLinkTable(Student *student) {
        Student *fast = student;
        Student *slow = student;
        
        Student *circleStu = NULL;
        while (fast->stu) {
            fast = fast->stu->stu;//快指针节点 为慢指针节点的2倍
            slow = slow->stu;
            
            if (fast==NULL) { //不存在循环节点
                return NULL;
            }
            if (fast == slow) {//快慢指针相遇。找到循环节点
                circleStu = fast;
                break;
            }
            
        }
        
        if (fast==NULL) { //不存在循环节点
            return NULL;
        }
        printf("相遇节点为==%d
    ",circleStu->point);
        fast=student;
        while (fast!=slow) {
            slow=slow->stu;
            fast=fast->stu;
        }
        return fast;
    }
    
    
    
    int main(void){
        char sf[15];
        
    //    /**
    //     *  创建单向链表
    //     */
    //    int num;
    //    printf ("请输入学生人数
    ");
    //    scanf("%d",&num);
    //    Student *link_stu = CreateLink_Table(num);
    //
    //    
    //    /**
    //     *  单向链表插入节点
    //     */
    //    printf ("请插入节点内容 在 已存在节点名字的后面,如 已存在节点名字|待插入名字|待插入分数 
    ");
    //    scanf("%s",sf);
    //
    //    link_stu = insertStudentLinkTable(link_stu,sf);
    //    
    //    /**
    //     *  反转单向链表
    //     */
    //    printf("反转链表Y|N 
    ");
    //    scanf("%s",sf);
    //    if (strcmp(sf,"Y")==0) {
    //       Student *newLt= ReversionStudentLinkTable(link_stu);
    //        //查询
    //        selectStudent(newLt);
    //    }
        
        /**
         *  创建循环链表
         */
        Student *student = NULL;
        printf("开始创建循环链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
          student =  CreateCircleLink_Table();
        }
        
        printf("已知情况查询循环链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
            SelectCircleLinkTable(student);
        }
        
        
        printf("未知情况查询循环链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
          Student *circleStu = SelectCircleNodeInLinkTable(student);
            printf("=====循环节点==%d
    ",circleStu->point);
        }
        
        
        return 0;
    }

    参考这便:http://blog.csdn.net/wenqian1991/article/details/17452715

  • 相关阅读:
    当前疫情期间,家里可以适当储备的物资
    35岁改行做程序员,需要勇气和决心
    离婚潮来临,女性在崛起
    摄影作品首先要取悦自己,更要打动他人
    京剧是该阳春白雪还是下里巴人?
    汶川和武汉哪个更让人铭记?
    35以上的女强人不结婚,只用平常待之
    苏州记忆之上班路上偶遇2美女打架
    SAP SD微观研究之销售发票自动生成初探
    Python requests库的使用(二)
  • 原文地址:https://www.cnblogs.com/DamonTang/p/4121221.html
Copyright © 2011-2022 走看看