下面是我的代码,尝试创建10个整数的链表。然后遍历列表,将偶数减半,将奇数加倍。 我在网上四处看看,以了解如何使用函数创建列表。我解释说要在下面的代码中编写该函数:“ void createList()”。代码可以很好地编译,但是当我运行它时,我只会得到以下输出:
原始列表:
更新列表:
有人知道问题的根源吗?是来自createList函数还是显示函数?甚至其他地方?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node
{
int data;
struct node* next;
};
void freeList();
void update();
void createList();
void display();
struct node* root = NULL;
int main(void)
{
srand(time(NULL));
createList((rand() % 10) + 1);
createList();
createList();
createList();
createList();
createList();
createList();
createList();
createList();
createList();
printf("Original list:");
display();
update();
printf("\nUpdated list:");
display();
freeList();
return 0;
}
void createList()
{
struct node* tmp;
tmp = malloc(sizeof(struct node));
tmp->data = (rand() % 10) + 1;
tmp->next = NULL;
if (root == NULL)
{
root = tmp;
}
else
{
struct node* p;
p = root;
while (p->next != NULL)
{
p = p->next;
}
p->next = tmp;
}
}
void display()
{
struct node* tmp;
tmp = root;
if (tmp = NULL)
{
printf("list is empty.\n");
}
else
{
while(tmp != NULL)
{
printf("\t%d", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
}
void freeList()
{
struct node* tmp;
tmp = root;
if (tmp = NULL)
{
printf("list is empty.\n");
}
else
{
while (tmp != NULL)
{
free(tmp);
tmp = tmp->next;
}
printf("\n");
}
}
void update()
{
struct node* tmp;
tmp = root;
if (tmp = NULL)
{
printf("list is empty.\n");
}
else
{
while (tmp != NULL)
{
if (root->data % 2 == 0)
{
root->data = root->data / 2;
}
else
{
root->data = root->data * 2;
}
printf("\t%d", root->data);
root = root->next;
}
}
}
答案 0 :(得分:2)
首先,您有许多
if (tmp = NULL)
应该是
if (tmp == NULL)
否则,您总是将tmp重置为NULL,并且永远不会输入“ if”。
第二,在update()中,您处理“ root”而不是“ tmp”,正确的是
while (tmp != NULL)
{
if (tmp->data % 2 == 0)
{
tmp->data = tmp->data / 2;
}
else
{
tmp->data = tmp->data * 2;
}
printf("\t%d", tmp->data);
tmp = tmp->next;
}
所以实际上您已经很接近了。 ps .:首次呼叫
createList((rand() % 10) + 1)
不需要参数
答案 1 :(得分:1)
在display
和其他地方
if (tmp = NULL)
这显然是不正确的,因为它将NULL
分配给tmp
使其看起来像一个零长度列表。
当我使用clang编译您的程序时,我得到的输出是这样:
jeremyp@Magenta:jeremyp% cc foo.c
foo.c:75:13: warning: using the result of an assignment as a condition without
parentheses [-Wparentheses]
if (tmp = NULL)
~~~~^~~~~~
foo.c:75:13: note: place parentheses around the assignment to silence this
warning
if (tmp = NULL)
^
( )
foo.c:75:13: note: use '==' to turn this assignment into an equality comparison
if (tmp = NULL)
^
==
foo.c:96:13: warning: using the result of an assignment as a condition without
parentheses [-Wparentheses]
if (tmp = NULL)
~~~~^~~~~~
foo.c:96:13: note: place parentheses around the assignment to silence this
warning
if (tmp = NULL)
^
( )
foo.c:96:13: note: use '==' to turn this assignment into an equality comparison
if (tmp = NULL)
^
==
foo.c:116:13: warning: using the result of an assignment as a condition without
parentheses [-Wparentheses]
if (tmp = NULL)
~~~~^~~~~~
foo.c:116:13: note: place parentheses around the assignment to silence this
warning
if (tmp = NULL)
^
( )
foo.c:116:13: note: use '==' to turn this assignment into an equality comparison
if (tmp = NULL)
^
==
3 warnings generated.
您可能正在使用clang或gcc,它们都将显示与上述类似的警告。我的建议是,即使发出可执行文件,也不要忽略警告。
。如果您的编译器未对问题代码发出警告(即使使用-Wall
开关),请将其扔掉以获得更好的代码。我有多年的经验,发现忽略警告会导致很多额外的工作和调试上的挫败感。
如果您使用的是Visual Studio(不幸!),它也会对此发出警告。显然it is a level 4 warning,所以您需要开关/W4
。我没有访问Visual Studio的权限,很遗憾,我无法对其进行验证。