单链表到循环链表的转换
这是我为循环链表编写的代码的链接。代码也粘贴在下面。
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail, *temp,*sp,*fp;
void add(int value);
void iterative_reverse();
void print_list();
void findcycle();
int main()
{
head=(mynode *)0;
add(1);
add(2);
add(3);
//print_list();
findcycle();
return(0);
}
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->value=value;
temp->next=(mynode *)0;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;
}
}
void findcycle()
{
if (head == NULL || head->next == NULL)
printf("null");
sp=head;
fp=head->next;
while (fp != NULL && fp->next != NULL)
{
if ((fp == sp) || (fp->next == sp))
printf("Cycle");
sp = sp->next;
fp = fp->next->next;
}
printf("Not a Cycle");
}
void print_list()
{
for(temp=head; temp!=tail; temp=temp->next)
printf("[%d]->",(temp->value));
}
我最初将其编写为单行,然后更改了一些指针以使其成为循环。我犯了一些我无法跟踪的错误,因此超时。请建议。
多谢。
Here is the link to the code I wrote for circular linked list. The code is pasted below also.
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail, *temp,*sp,*fp;
void add(int value);
void iterative_reverse();
void print_list();
void findcycle();
int main()
{
head=(mynode *)0;
add(1);
add(2);
add(3);
//print_list();
findcycle();
return(0);
}
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->value=value;
temp->next=(mynode *)0;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;
}
}
void findcycle()
{
if (head == NULL || head->next == NULL)
printf("null");
sp=head;
fp=head->next;
while (fp != NULL && fp->next != NULL)
{
if ((fp == sp) || (fp->next == sp))
printf("Cycle");
sp = sp->next;
fp = fp->next->next;
}
printf("Not a Cycle");
}
void print_list()
{
for(temp=head; temp!=tail; temp=temp->next)
printf("[%d]->",(temp->value));
}
I had initially written it for single and then changed few pointers to make it circular. I am doing some mistake in it which I am not able to track and hence getting a Timeout. Please suggest.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来是错误的:
它应该是(如果您在列表末尾添加新节点并希望它成为一个循环列表,就像我在这里假设的那样):
无论如何,这是一个小错误:只是一个冗余分配。
真正严重的问题在于:
首先,你想要完成什么?不清楚,所以不容易建议你如何纠正它;无论如何,最明显的错误是,如果列表实际上是一个循环列表,那么循环将永远持续下去,因为不会发生任何退出条件(没有一个指针会永远变成NULL)。
This looks wrong:
It should be (if you're adding the new node at the end of the list and want it to be a circular list, like I'm assuming here):
It's a minor error, anyway: only a redundant assignment.
The really serious trouble is here:
First of all, what are you trying to accomplish? It isn't clear, so it isn't easy to suggest you how to correct it; the most obvious bug, anyway, is that if the list actually is a circular one, then the loop will go on forever, as there is no exit condition that can ever happen (no one of the pointers will ever become NULL).
当
findcycle
找到一个循环时,它不会退出:它只是继续运行。 (同样,当它获得包含 0 或 1 个元素的列表时。)我不保证这是代码中的唯一错误,但足以使其不起作用。When
findcycle
finds a cycle, it doesn't exit: it just keeps going. (Likewise when it gets a list with 0 or 1 elements.) I do not guarantee that this is the only error in your code, but it suffices to make it not work.