gpt4 book ai didi

c - 如何解决以下代码中的逻辑错误

转载 作者:行者123 更新时间:2023-11-30 19:00:52 26 4
gpt4 key购买 nike

我在执行以下代码时遇到问题

#include<stdio.h>
#include<stdlib.h>


struct linked
{
int val;
struct linked *before;
struct linked *next;
};

void get(struct linked *var);
void printforward(struct linked *var);
void printbackward(struct linked *var);



int main()
{
struct linked *ptr,*temp;
ptr=(struct linked*)malloc(sizeof(struct linked));
if(ptr==NULL)
{
printf("NOT ENOUGH MEMOREY");
exit(2);
}
ptr->before=NULL;
get(ptr);
printf("\nForward:\n");
printforward(ptr);
for(temp=ptr;temp->next;temp=temp->next)
{
temp->next->before=(struct linked*)temp;
}
printf("\nBackward:\n");
printbackward(temp->before);
}


void get(struct linked *var)
{
printf("Enter the number (-99) to quit:");
scanf("%d",&var->val);
if(var->val==-99)
{
var->next=NULL;
return;
}
else
{
var->next=(struct linked*)malloc(sizeof(struct linked));
if(var->next==NULL)
{
printf("NOT ENOUGH MEMOREY");
exit(2);
}
get(var->next);
}
}

void printforward(struct linked *var)
{
if(var->next==NULL)
{
return;
}
else
{
printf("\n%d",var->val);
printforward(var->next);
}
}

void printbackward(struct linked *var)
{
if(var->before==NULL)
{
printf("\n%d",var->val);
return;
}
else
{
printf("\n%d",var->val);
printforward(var->before);
}
}

输出:

 Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:6
Enter the number (-99) to quit:7
Enter the number (-99) to quit:8
Enter the number (-99) to quit:9
Enter the number (-99) to quit:0
Enter the number (-99) to quit:-99

Forward:

1
2
3
4
5
6
7
8
9
0
Backward:

0
9
0
Process returned 0 (0x0) execution time : 22.297 s
Press any key to continue.

预期输出:

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:6
Enter the number (-99) to quit:7
Enter the number (-99) to quit:8
Enter the number (-99) to quit:9
Enter the number (-99) to quit:0
Enter the number (-99) to quit:-99

Forward:

1
2
3
4
5
6
7
8
9
0
Backward:
0
9
8
7
6
5
4
3
2
1

让我知道代码中有什么问题,我正在学习c语言的链表,我想编写双向链表的代码,但是我在上面的问题中有一个逻辑错误,我不明白为什么上面的代码不起作用,所以请求消除疑问。

最佳答案

为什么你要通过递归来做到这一点,我不知道,但本着这种精神,请考虑这一点。

  • 您正在从 printbackward 调用 printforward,这是没有意义的。
  • 你的 Actor 阵容没有帮助,事实上,他们掩盖了真正的问题。在 C 中,既不需要也不建议进行内存分配、like-pointer 或往返于 void-pointer 的转换。 Read here for why

所有四个操作都可以递归完成,事实上,您甚至不需要“before”指针,但我仍然保留了它。您可以:

  • 建立您的 list
  • 转发打印您的列表
  • 向后打印您的列表
  • 清理您的列表

...全部使用递归(我将您想要的原因作为不同的问题)。

<小时/>

代码

#include <stdio.h>
#include <stdlib.h>

struct linked
{
int val;
struct linked *before;
struct linked *next;
};

struct linked *get(struct linked *before);
void printforward(struct linked const *var);
void printbackward(struct linked const *var);
void cleanup(struct linked *lst);

int main()
{
struct linked *ptr = get(NULL);

printf("Forward: ");
printforward(ptr);
fputc('\n', stdout);

printf("Backward: ");
printbackward(ptr);
fputc('\n', stdout);

cleanup(ptr);
}


struct linked *get(struct linked *before)
{
printf("Enter the number (-99) to quit:");
int value = -99;
if (scanf("%d", &value) != 1 || value == -99)
return NULL;

struct linked *p = malloc(sizeof *p);
if (p != NULL)
{
p->before = before;
p->val = value;
p->next = get(p);
}
return p;
}

void printforward(struct linked const *var)
{
if (var)
{
printf("%d ", var->val);
printforward(var->next);
}
}

void printbackward(struct linked const *var)
{
if (var)
{
printbackward(var->next);
printf("%d ", var->val);
}
}

void cleanup(struct linked *lst)
{
if (!lst)
return;

cleanup(lst->next);
free(lst);
}

控制台

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:-99
Forward: 1 2 3 4 5
Backward: 5 4 3 2 1

关于c - 如何解决以下代码中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com