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

    双向循环链表  和 单向循环链表 查找循环节点 思路都是一样。 快慢指针查找法。 理论可参考

    c 链表之 快慢指针 查找循环节点

    typedef struct Student_Double
    {
        char name[10];
        int  point;
        struct Student_Double *preStu;
        struct Student_Double *nextStu;
    } StudentDouble;
    StudentDouble *  CreateDoubleCircleLink_Table(){
        
        int i = 0;
        StudentDouble *head = NULL;
        head=(StudentDouble *)malloc(sizeof(StudentDouble));
        head->name[0]='';
        head->point = 0;
        head->nextStu = NULL;
        head->preStu = NULL;
        
        //循环节点
        StudentDouble *cirleStu = NULL;
        StudentDouble *temp = NULL;
        StudentDouble *currentNode = head;
        while (i<=9) {
            temp = (StudentDouble *)malloc(sizeof(StudentDouble));
            strncpy(temp->name,"Node",sizeof(temp->name));
            temp->point = i;
            temp->nextStu = NULL;
            temp->preStu = currentNode;
            
            currentNode->nextStu = temp;
            currentNode = temp;
            
            if (i==3) {
                cirleStu = currentNode;
            }
            i++;
        }
        //最后 合并循环节点
        currentNode->nextStu=cirleStu;
        return head;
    }
    //已知循环节点情况查询循环 链表,验证是否可用
    void SelectDoubleLinkTable(StudentDouble *student){
        StudentDouble *next = student->nextStu;
        int i = 0;
        StudentDouble *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->nextStu;
        }
    
    
    }
    //未知情况查询循环节点
    StudentDouble * SelectCircleNodeInDoubleLinkTable(StudentDouble *head){
        //快慢指针查询
        StudentDouble *fast = head;
        StudentDouble *slow = head;
        
        while (fast) {
            fast = fast->nextStu->nextStu;
            slow = slow->nextStu;
            
            if (fast==NULL) {//不是循环链表推出
                break;
            }
            if (fast==slow) {//快慢指针相遇
                break;
            }
        }
        if (fast == NULL) {
            printf("该链表 不是循环链表
    ");
            return NULL;
        }
        
        //查找循环节点
        fast = head;
        while (fast!=slow) {
            fast=fast->nextStu;
            slow=slow->nextStu;
        }
        printf("=====找到循环链表循环节点为%d
    ",fast->point);
        return fast;
    }
    int main(void){
        char sf[15];
      //创建双向循环链表
        StudentDouble *head = NULL;
    
        printf("创建双向循环链表Y|N
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
            head = CreateDoubleCircleLink_Table();
        }
        printf("已知情况查询循环链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
            SelectDoubleLinkTable(head);
        }
        printf("未知情况查询循环链表Y|N 
    ");
        scanf("%s",sf);
        if (strcmp(sf,"Y")==0) {
            SelectCircleNodeInDoubleLinkTable(head);
        }
        return 0;
    }
  • 相关阅读:
    建模时选择SVM还是LR?
    decision_function详解
    HIVE SQL与SQL的区别
    数据不平衡如何处理
    Spring Boot 两步集成 日志收集ELK与分布式系统监控CAT
    Web Application Integrate with Box
    字符编解码的故事(ASCII,ANSI,Unicode,Utf-8区别)
    SharePoint Client Api Search user 几种方法
    C#,反射和直接调用的效率差别
    System.Data.SqlClient.SqlConnectionStringBuilder 链接字符串的问题
  • 原文地址:https://www.cnblogs.com/DamonTang/p/4128366.html
Copyright © 2011-2022 走看看