複習下C 鏈表操作(雙向迴圈鏈表,尋找迴圈節點)

來源:互聯網
上載者:User

標籤:style   blog   http   ar   color   os   sp   div   log   

雙向迴圈鏈表  和 單向迴圈鏈表 尋找迴圈節點 思路都是一樣。 快慢指標尋找法。 理論可參考

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]=‘\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,結束迴圈\n",next->point);            break;        }        if (i==3) {            circleStu = next;        }        printf("index %d; studentName is %s;  point is %d\n",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("該鏈表 不是迴圈鏈表\n");        return NULL;    }        //尋找迴圈節點    fast = head;    while (fast!=slow) {        fast=fast->nextStu;        slow=slow->nextStu;    }    printf("=====找到迴圈鏈表迴圈節點為%d\n",fast->point);    return fast;}int main(void){    char sf[15];  //建立雙向迴圈鏈表    StudentDouble *head = NULL;    printf("建立雙向迴圈鏈表Y|N\n");    scanf("%s",sf);    if (strcmp(sf,"Y")==0) {        head = CreateDoubleCircleLink_Table();    }    printf("已知情況查詢迴圈鏈表Y|N \n");    scanf("%s",sf);    if (strcmp(sf,"Y")==0) {        SelectDoubleLinkTable(head);    }    printf("未知情況查詢迴圈鏈表Y|N \n");    scanf("%s",sf);    if (strcmp(sf,"Y")==0) {        SelectCircleNodeInDoubleLinkTable(head);    }    return 0;}

 

複習下C 鏈表操作(雙向迴圈鏈表,尋找迴圈節點)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.