鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/while' title='while'>while   鍑虹幇   bubuko   erro   浣嶇疆   澶辫触   malloc   ==   鎬濊矾   

锛堜竴锛夊悎骞朵袱涓惊鐜摼琛?/h2>

鎶€鏈垎浜浘鐗? src=

鎶€鏈垎浜浘鐗? src=

    p = rearA->next;    //A鐨勫ご缁撶偣锛屼竴浼氳繕瑕佷娇鐢?/span>
    rearA->next = rearB->next->next;    //鏄疉鐨勫熬缁撶偣鎸囧悜B鐨勭涓€涓粨鐐?/span>
    q = rearB->next;    //瀛樻斁B鐨勫ご缁撶偣锛岄渶瑕侀噴鏀?/span>
    rearB->next = p;    //浣緽鐨勫熬缁撶偣鎸囧悜A鐨勫ご缁撶偣
    free(q);    //閲婃斁B鐨勫ご缁撶偣

锛堜簩锛夊垽鏂崟閾捐〃涓槸鍚︽湁鐜?/h2>

鎶€鏈垎浜浘鐗? src=

 

 鏂规硶涓€锛氫娇鐢ㄤ袱涓寚閽堬紝寰幆宓屽,A鎸囬拡鍦ㄥ灞傚惊鐜紝涓€姝ヤ竴姝ュ悜涓嬭蛋锛孊鎸囬拡鍦ㄥ唴灞傚惊鐜紝寰幆鍒癆鐨勪綅缃紝褰撲袱鑰呯殑浣嶇疆鐩稿悓鏃跺垽鏂蛋鐨勬鏁版槸鍚︿竴鑷达紝涓嶄竴鑷村垯浠h〃鏈夌幆銆備笖鑳藉寰楀埌鍑嗙‘鐨勭幆璺妭鐐广€傚叾涓瑼鏄灏嗛摼琛ㄤ粠澶磋蛋鍒板熬锛孊鏄竴鐩村湪鍐呭眰杩涜寰幆锛屾椂闂村鏉傚害涓?span style="color: #ff0000">O(n^2)

//涓ゅ眰寰幆杩涜鍒ゆ柇
Status HasCircle01(List L,int *seq)
{
    List la, lb;
    int stepa, stepb;
    la = lb = L;    //la鍦ㄥ灞傚惊鐜紝lb鍦ㄥ唴灞傚惊鐜?/span>
    stepa = stepb = 1;
    while (la)
    {
        while (lb!=la)
        {
            lb = lb->next;
            stepb++;
        }
        if (stepa != stepb)
            break;
        stepa++;
        la = la->next;
        lb = L;
        stepb = 1;
    }
    if (la!=NULL)
    {
        *seq = stepb;
        return TRUE;
    }
    return FALSE;
}

鏂规硶浜岋細浣跨敤蹇參鎸囬拡鑻ユ槸鏈夌幆閭d箞蹇寚閽堜細涓€鐩村湪鐜腑寰幆锛屽綋鎱㈡寚閽堣繘鍏ョ幆涓妭鐐瑰悗锛屼竴瀹氫細鍑虹幇蹇寚閽堝湪鎱㈡寚閽堝悗闈紙鎴栬€呯浉绛夛級鐨勬儏鍐碉紝灏卞彲浠ュ垽鏂槸鍚︽湁鐜紝涓嶈繃杩欑鏂规硶涓嶅鏄撹幏鍙栧埌鐜矾鑺傜偣浣嶇疆锛屾椂闂村鏉傚害鎸夌収鎱㈡寚閽堟潵绠楋紝涓?span style="color: #ff0000">O(n)

//蹇參鎸囬拡杩涜鍒ゆ柇
Status HasCircle02(List L)
{
    List high, low;
    high = low = L;
    while (low&&high&&high->next)
    {
        if (high->next)
            high = high->next->next;
        low = low++;
        if (high == low)
            return OK;
    }
    return FALSE;
}

鏂规硶涓夛細鍒ゆ柇鍦板潃鐨勫ぇ灏?/h3>
1.鏍堢殑鍦板潃鏄敱楂樺悜浣庡闀跨殑.
2.鍫嗗緱鍦板潃澧為暱鏂瑰悜鏄敱浣庡埌楂樺悜涓婂闀跨殑

鎴戜滑鍒涘缓閾捐〃鏃讹紝涓€鑸槸浣跨敤鍫嗗尯杩涜锛屾墍浠ヤ竴鑸満鍣ㄩ兘鏄湴鍧€鍚戜笂澧為暱锛岃嫢鏄湁鐜紝鍒欏湴鍧€浼氬噺灏忥紝鎴戜滑鍙互浣跨敤涓€涓寚閽堬紝鎴栬€呬竴涓揩鎸囬拡锛屽皢姣忔鐨勭粨鐐瑰湴鍧€姣旇緝锛岃繖鏍锋椂闂村鏉傚害涓?span style="color: #ff0000">O(n/2),鑻ユ槸鐜冻澶熷ぇ锛屾垜浠缃殑鎸囬拡澧為暱姝ラ暱澶熷ぇ锛屼篃浼氫紭鍖栨洿澶氥€?/h3>

涓嶈繃鏈?strong>闄愬埗锛屽氨鏄垜浠垱寤虹殑閾捐〃闇€瑕佸湴鍧€澧為暱鏄崟鍚戠殑锛屽氨鏄彧鑳戒娇鐢ㄥ熬鎻掓硶鎴栬€呭ご鎻掓硶锛屼笉鑳戒娇鐢ㄤ腑闂存彃鍏ユ垨鑰呰仈鍚堜娇鐢?/h3>
//鍦板潃瀛楄妭杩涜鍒ゆ柇锛屼负浜嗚繖绉嶆柟娉曞疄鐜帮紝涓婇潰鏃犺鏄垱寤虹洿閾捐〃杩樻槸寰幆閾捐〃閮芥槸浣跨敤鐨勫熬鎻掓硶
Status HasCircle03(List L)
{
    List high=L;
    int MaxAddr = 0;
    while (high&&high->next)
    {
        if (high->next)
        {
            high = high->next->next;
            if (MaxAddr < high)
                MaxAddr = high;
            else
                break;
        }
    }
    if (high&&high->next)
        return TRUE;
    return FALSE;
}
鎶€鏈垎浜浘鐗? id=
//鍒ゆ柇鍫嗗闀挎柟鍚?/span>
int StackGrow()
{
    int *a,*b;
    int flag;
    a = (int *)malloc(sizeof(int));
    b = (int *)malloc(sizeof(int));
    if (a > b)
        flag = 0;
    else
        flag = 1;
    free(a);
    free(b);
    return flag;
}
浣跨敤涓€涓皬渚嬪瓙鏉ュ垽鏂爢鐨勫闀挎柟鍚?/span>

鍏朵粬鏂规硶杩橀渶瑕佸啀缁х画鍥為【鐭ヨ瘑鍚庢墠鏈夋€濊矾.....

鍏ㄩ儴瀹炵幇浠g爜

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef int Status;

typedef struct Node
{
    ElemType data;
    struct Node* next;
}Node;

typedef struct Node* List;

//鍒涘缓涓€涓垪琛紝鏈夋棤鐜?鑻ユ槸鏈夌幆锛屽皢鍗曢摼琛ㄥ拰寰幆閾捐〃鍚堝苟鍗冲彲
Status InitList(List* L, int flag, int* sep);
//鍒涘缓涓€涓崟閾捐〃
Status CreateList(List* L, int n);
//鍒涘缓涓€涓惊鐜摼琛?/span>
Status CreateCList(List* L, int n);

//寮€濮嬭繘琛屽垽鏂槸鍚︽湁鐜?//涓ゅ眰寰幆杩涜鍒ゆ柇
Status HasCircle01(List L,int *seq);
//蹇參鎸囬拡杩涜鍒ゆ柇
Status HasCircle02(List L);
//鍦板潃瀛楄妭杩涜鍒ゆ柇锛屼负浜嗚繖绉嶆柟娉曞疄鐜帮紝涓婇潰鏃犺鏄垱寤虹洿閾捐〃杩樻槸寰幆閾捐〃閮芥槸浣跨敤鐨勫熬鎻掓硶
Status HasCircle03(List L);


//鐢ㄦ潵鎵撳嵃閾捐〃
void PrintList(List L,int flag, int seq);


int main()
{
    List L = NULL;
    int seq = 0;    //鍒嗗壊鐐?/span>
    int flag = 1;

    printf("create cList?(1/0):");
    scanf("%d", &flag);

    if (!InitList(&L, flag, &seq))    //鐜板湪L鎸囧悜绗竴涓粨鐐?/span>
        return 0;
    PrintList(L,flag,seq);

    if (HasCircle01(L, &seq))
        printf("has Circle:%d
", seq);
    else
        printf("no Circle
");

    if (HasCircle02(L))
        printf("has Circle
");
    else
        printf("no Circle
");

    if (HasCircle03(L))
        printf("has Circle
");
    else
        printf("no Circle
");


    system("pause");
    return 0;
}


//鍒涘缓涓€涓垪琛紝鏈夋棤鐜?鑻ユ槸鏈夌幆锛屽皢鍗曢摼琛ㄥ拰寰幆閾捐〃鍚堝苟鍗冲彲,杩欓噷鍗曢摼琛ㄥ拰寰幆閾捐〃閮芥病鏈夊ご缁撶偣
Status InitList(List* L, int flag,int* sep)
{
    int n;
    List SL,CL;
    List q;
    
    srand(time(0));
    
    printf("please enter the length of list:");
    scanf("%d", &n);
    *sep = n;
    if (!CreateList(&SL, n))    //閾捐〃鍒涘缓澶辫触锛岀洿鎺ラ€€鍑?/span>
        return ERROR;
    if (flag)    //鍒涘缓涓€涓湁鐜摼琛?/span>
    {
        printf("please enter the length of Clist:");
        scanf("%d", &n);
        if (!CreateCList(&CL, n)) //CL鏄惊鐜摼琛ㄥご鎸囬拡
            return ERROR;
        q = SL;
        for (n = 1; n < *sep; n++)
            q = q->next;    //鐩存帴鎸囧悜鍗曢摼琛ㄧ殑鏈熬锛屼笅闈㈠紑濮嬪悎骞?/span>
        q->next = CL;
    }
    *L = SL;
    return OK;
}
//鍒涘缓涓€涓崟閾捐〃
Status CreateList(List* L, int n)
{
    int i;
    List q,p;
    if (n < 1)
        return ERROR;

    *L = (List)malloc(sizeof(Node));
    (*L)->data = rand() % 100;
    q = *L;

    for (i = 1; i < n; i++)
    {
        p = (List)malloc(sizeof(Node));
        p->data = rand() % 100;
        q->next = p;
        q = p;
    }
    q->next = NULL;
    return OK;
}

//鍒涘缓涓€涓惊鐜摼琛?/span>
Status CreateCList(List* L, int n)
{
    List q,p;    
    ElemType item;

    if (n < 1)
        return ERROR;

    *L = (List)malloc(sizeof(Node));
    if (*L == NULL)
        return ERROR;

    (*L)->data = rand() % 100;
    p = *L;
    p->next = p;    //褰㈡垚鍥炵幆

    for (int i = 1; i < n;i++)
    {
        //鐢熸垚鏂扮殑鑺傜偣锛屾牴鎹熬鎸囬拡娣诲姞鑺傜偣,骞跺疄鏃舵洿鏂板熬鎸囬拡銆傛敞鎰忚繖閲屾暟鎹彃鍏ユ槸灏炬彃娉?/span>
        q = (List)malloc(sizeof(Node));
        q->data = rand()%100;
        q->next = p->next;
        p->next = q;
        p = q;
    }
    return OK;
}


//涓ゅ眰寰幆杩涜鍒ゆ柇
Status HasCircle01(List L,int *seq)
{
    List la, lb;
    int stepa, stepb;
    la = lb = L;    //la鍦ㄥ灞傚惊鐜紝lb鍦ㄥ唴灞傚惊鐜?/span>
    stepa = stepb = 1;
    while (la)
    {
        while (lb!=la)
        {
            lb = lb->next;
            stepb++;
        }
        if (stepa != stepb)
            break;
        stepa++;
        la = la->next;
        lb = L;
        stepb = 1;
    }
    if (la!=NULL)
    {
        *seq = stepb;
        return TRUE;
    }
    return FALSE;
}

//蹇參鎸囬拡杩涜鍒ゆ柇
Status HasCircle02(List L)
{
    List high, low;
    high = low = L;
    while (low&&high&&high->next)
    {
        if (high->next)
            high = high->next->next;
        low = low++;
        if (high == low)
            return OK;
    }
    return FALSE;
}

//鍦板潃瀛楄妭杩涜鍒ゆ柇锛屼负浜嗚繖绉嶆柟娉曞疄鐜帮紝涓婇潰鏃犺鏄垱寤虹洿閾捐〃杩樻槸寰幆閾捐〃閮芥槸浣跨敤鐨勫熬鎻掓硶
Status HasCircle03(List L)
{
    List high=L;
    int MaxAddr = 0;
    while (high&&high->next)
    {
        if (high->next)
        {
            high = high->next->next;
            if (MaxAddr < high)
                MaxAddr = high;
            else
                break;
        }
    }
    if (high&&high->next)
        return TRUE;
    return FALSE;
}


//鐢ㄦ潵鎵撳嵃閾捐〃
void PrintList(List L, int flag, int seq)
{
    List CHead;
    List q = L;    //鑾峰彇澶存寚閽?/span>
    int i;

    if (!flag)
    {
        while (q)
        {
            printf("%d ", q->data);
            q = q->next;
        }
    }
    else
    {
        for (i = 1; i <= seq; i++)
        {
            printf("%d ", q->data);
            q = q->next;
        }
        //for寰幆閫€鍑哄氨杩涘叆浜嗗惊鐜摼琛ㄨ寖鍥村唴
        printf("-|- ");
        CHead = q;
        while (q->next != CHead)
        {
            printf("%d ", q->data);
            q = q->next;
        }
        printf("%d", q->data);
    }
    printf("
");
}

娴嬭瘯缁撴灉

鎶€鏈垎浜浘鐗? src=鎶€鏈垎浜浘鐗? src=鎶€鏈垎浜浘鐗? src=

 

以上是关于鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖的主要内容,如果未能解决你的问题,请参考以下文章

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶅甫浣犵湅鍝堝笇绠楁硶涔嬬編

鍖诲鑷劧璇█澶勭悊鐩稿叧璧勬簮鏁寸悊

绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪

python鍐呯疆鏁版嵁缁撴瀯

鏁版嵁缁撴瀯-C璇█瀹炵幇-閾炬爤

鏁版嵁缁撴瀯鐭ヨ瘑妗嗘灦