gpt4 book ai didi

无法在链表中存储元素

转载 作者:行者123 更新时间:2023-11-30 18:33:40 25 4
gpt4 key购买 nike

我的目标是创建一个链接列表并将元素存储在该列表中。

struct node
{
int a;
struct node *b;
} p,*temp,*head;


void create ( struct node *temp)
{
char c;

temp = malloc (sizeof(struct node));

printf("enter data\n");

scanf(" %d",&temp->a);

printf("do you want to insert another node y/n\n");

scanf("%s", &c);
if (c=='y')
{
create(temp->b);
}
else if ( c=='n')
{
temp->b= NULL;
temp=&p;
return;
}
}

void traverse ( struct node *head)
{
while(head != NULL)
{
printf("%d ",head->a);
head=head->b;
}
}

int main ()
{
int i,j,k,l,m,n;

do{
if(i==1)
{
printf("enter data\n");
scanf("%d",&p.a);

create (p.b);
}
else if ( i==2)
traverse(temp);
}
while(i!=3);

printf("%d",temp->a);
}

一旦存储了元素,我就无法恢复它们。当我尝试遍历列表时,它只提供列表的第一个元素,而没有其他内容。

最佳答案

主要部分

 do {
if(i==1)
{
...
}
else if ( i==2)
traverse(temp);
}
while(i!=3);

一定是这样的

 do {
if (scanf("%d", &i) != 1)
break;
if(i==1)
{
...
}
else if ( i==2)
traverse(temp);
}
while(i!=3);

了解用户想要什么(我没有在您的代码中初始化)

创建

  scanf("%s", &c);

错误,因为cchar而不是字符串

不要混合读取intchar,因为读取字符时会读取换行符和空格,所以读取c的字符串,例如

char c[2];

...
scanf("%1s", &c);
if (*c == 'y')
...
else if (c == 'n')
...

else 分支中的 return 是无用的,如果答案不是“y”或“n”,您什么也不做,因此您不会设置 temps,可能您只需检查“y”和所有其他答案是否必须被视为“n”,或者您需要再次询问选择

create中,您分配了局部变量temps,这对main中的p.b没有影响,您例如需要获取一个节点**

in main 使用了 temp 但从未在其他地方设置,并且变量 j,k,l,m,n 毫无用处。您还在 main 中请求数据,而您也在 create 中请求数据,但不能在 main 中完成。您管理变量的方式不允许您修改/打印列表

我鼓励您尽可能不要使用全局变量,并且不要像对 temphead 那样对全局变量和局部变量使用相同的名称因为这对代码的读者没有帮助

<小时/>

解决问题的提案:

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

struct node
{
int a;
struct node * b;
};

/* flush input up to the end of the line */
void flush()
{
int c;

while ((c = getchar()) != '\n') {
if (c == EOF)
exit(-1);
}
}

void create (struct node ** l)
{
/* go to the end of the list */
while (*l != NULL)
l = &(*l)->b;

for (;;) {
char c[2];
int v;

printf("enter data\n");

if (scanf("%d", &v) != 1) {
puts("invalid value");
flush();
}
else {
*l = malloc (sizeof(struct node));
(*l)->a = v;
(*l)->b = NULL;
l = &(*l)->b;

for (;;) {
printf("do you want to insert another node y/n\n");
scanf("%1s", c);

if (*c == 'y')
break;
else if (*c == 'n')
return;
}
}
}
}

void traverse ( struct node *head)
{
while(head != NULL)
{
printf("%d ",head->a);
head = head->b;
}
putchar('\n');
}

int main ()
{
int i;
struct node *head = NULL;

for (;;) {
puts("enter choice : 1 to create new node, 2 to print list, 3 to exit");

if (scanf("%d", &i) != 1)
flush();

switch(i) {
case 1:
create(&head);
break;
case 2:
traverse(head);
break;
case 3:
return 0;
default:
break;
}
}
}

编译和执行:

/tmp % gcc -pedantic -Wextra -Wall t.c
/tmp % ./a.out
enter choice : 1 to create new node, 2 to print list, 3 to exit
2

enter choice : 1 to create new node, 2 to print list, 3 to exit
1
enter data
11
do you want to insert another node y/n
y
enter data
22
do you want to insert another node y/n
n
enter choice : 1 to create new node, 2 to print list, 3 to exit
2
11 22
enter choice : 1 to create new node, 2 to print list, 3 to exit
1
enter data
3
do you want to insert another node y/n
n
enter choice : 1 to create new node, 2 to print list, 3 to exit
2
11 22 3
enter choice : 1 to create new node, 2 to print list, 3 to exit
4
enter choice : 1 to create new node, 2 to print list, 3 to exit
3

我鼓励您添加免费的列表

关于无法在链表中存储元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55392510/

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