鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/while' title='while'>while
鍑虹幇 bubuko erro 浣嶇疆 澶辫触 malloc == 鎬濊矾锛堜竴锛夊悎骞朵袱涓惊鐜摼琛?/h2>
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>
鏂规硶涓€锛氫娇鐢ㄤ袱涓寚閽堬紝寰幆宓屽,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;
}
//鍒ゆ柇鍫嗗闀挎柟鍚?/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("
");
}
娴嬭瘯缁撴灉
1.鏍堢殑鍦板潃鏄敱楂樺悜浣庡闀跨殑. 2.鍫嗗緱鍦板潃澧為暱鏂瑰悜鏄敱浣庡埌楂樺悜涓婂闀跨殑
涓嶈繃鏈?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;
}
//鍒ゆ柇鍫嗗闀挎柟鍚?/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("
");
}
娴嬭瘯缁撴灉
//鍦板潃瀛楄妭杩涜鍒ゆ柇锛屼负浜嗚繖绉嶆柟娉曞疄鐜帮紝涓婇潰鏃犺鏄垱寤虹洿閾捐〃杩樻槸寰幆閾捐〃閮芥槸浣跨敤鐨勫熬鎻掓硶 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; }
//鍒ゆ柇鍫嗗闀挎柟鍚?/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; }
#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(" "); }
以上是关于鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖的主要内容,如果未能解决你的问题,请参考以下文章