09-03-2008, 15:44
|
|
|
|
חבר מתאריך: 08.10.02
הודעות: 827
|
|
בתגובה להודעה מספר 1 שנכתבה על ידי Dark Knight שמתחילה ב "לאור השקט בפורום, עוד חידה - רשימה מקושרת"
אני לא מכיר אבל אני אנסה את מזלי:
קוד:
bool checkcir(node *start) {
unsigned long size = 0, i;
node *r, *current;
for (current = start; current != NULL; current = current->next) {
for (r = start, i = 0; i < size; r = r->next, i++) {
if (r == current) return true;
}
size++;
}
return false;
}
ובדקתי את זה עם הקוד הבא:
קוד:
// testinked.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
struct node {
int x;
node *next;
};
bool checkcir(node *start) {
unsigned long size = 0, i;
node *r, *current;
for (current = start; current != NULL; current = current->next) {
for (r = start, i = 0; i < size; r = r->next, i++) {
if (r == current) return true;
}
size++;
}
return false;
}
int main(int argc, char* argv[])
{
node a[256];
//5->4->1->5->4->1....
a[1].next = &a[5];
a[5].next = &a[4];
a[4].next = &a[1];
printf("%d\r\n", checkcir(&a[5]) ? 1 : 0);
//4->1->5->4->1->5....
a[1].next = &a[5];
a[5].next = &a[4];
a[4].next = &a[1];
printf("%d\r\n", checkcir(&a[4]) ? 1 : 0);
//1->5->4->1->5->4->1....
a[1].next = &a[5];
a[5].next = &a[4];
a[4].next = &a[1];
printf("%d\r\n", checkcir(&a[1]) ? 1 : 0);
//5->4->1->NULL
a[5].next = &a[4];
a[4].next = &a[1];
a[1].next = NULL;
printf("%d\r\n", checkcir(&a[5]) ? 1 : 0);
//1->2->3->4->5->2->3->4->5->2....
a[1].next = &a[2];
a[2].next = &a[3];
a[3].next = &a[4];
a[4].next = &a[5];
a[5].next = &a[2];
printf("%d\r\n", checkcir(&a[2]) ? 1 : 0);
//1->1->1->1->...
a[1].next = &a[1];
printf("%d\r\n", checkcir(&a[1]) ? 1 : 0);
return 0;
}
_____________________________________
Error: Keyboard not found
Press F1 to continue
נערך לאחרונה ע"י maind בתאריך 09-03-2008 בשעה 15:48.
|