我必须为作业做 2 个链表,将用户给出的整数放在第一个,然后将第一个到第二个列表的每个整数的结果 = x^3。在下面的代码中,我试图打印我放在第一个列表中的内容,并使用 scanf 读取。我还不明白为什么我不能用这种方式打印。你能解释一下吗?提前致谢!!问题是我只打印最后一个元素和 0...:s
代码:
#include <stdio.h>
#include <stdlib.h>
struct L1
{
int x;
struct L1 *next1;
};
struct L2
{
int x,i,v;
struct L2 *next2;
};
int main()
{
int i,N;
struct L1 *root1;
struct L2 *root2;
struct L1 *conductor1;
struct L2 *conductor2;
root1 = malloc(sizeof(struct L1));
root2 = malloc(sizeof(struct L2));
root1->next1=0;
conductor1 = root1;
printf("Dwste arithmo N");
scanf("%d",&N);
printf("\nDwse arithmo");
scanf("%d",&conductor1->x);
printf("%d",conductor1->x);
for(i=0; i<N; i++)
{
conductor1->next1 = malloc(sizeof(struct L1));
printf("\nDwste arithmo");
scanf("%d",&conductor1->x);
conductor1->next1=0;
}
conductor1 = root1;
while (conductor1 != NULL)
{
printf("\n%d",conductor1->x);
conductor1=conductor1->next1;
}
return 0;
}