单链表到循环链表的转换

发布于 2024-10-20 23:56:44 字数 1321 浏览 1 评论 0原文

这是我为循环链表编写的代码的链接。代码也粘贴在下面。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆萌少年 2024-10-27 23:56:44

这看起来是错误的:

tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;

它应该是(如果您在列表末尾添加新节点并希望它成为一个循环列表,就像我在这里假设的那样):

tail->next=temp;
temp->next=head;
tail=temp;

无论如何,这是一个小错误:只是一个冗余分配。

真正严重的问题在于:

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");
}

首先,你想要完成什么?不清楚,所以不容易建议你如何纠正它;无论如何,最明显的错误是,如果列表实际上一个循环列表,那么循环将永远持续下去,因为不会发生任何退出条件(没有一个指针会永远变成NULL)。

This looks wrong:

tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;

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):

tail->next=temp;
temp->next=head;
tail=temp;

It's a minor error, anyway: only a redundant assignment.

The really serious trouble is here:

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");
}

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).

杀お生予夺 2024-10-27 23:56:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文