C 未打印第一个(输入的)节点

C 未打印第一个(输入的)节点,c,data-structures,linked-list,C,Data Structures,Linked List,在我的链表程序中,第一次输入的数据不会被打印出来,这是我第一次使用指针到指针的指针制作任何程序。另外,请告诉我,我是否以正确的方式使用了指向指针的指针 #include<stdio.h> #include<stdlib.h> struct node { int i; struct node *next; }; void arrange(struct node **x_head,struct node **x_temp) { (*x_temp)

在我的链表程序中,第一次输入的数据不会被打印出来,这是我第一次使用指针到指针的指针制作任何程序。另外,请告诉我,我是否以正确的方式使用了指向指针的指针

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

struct node
{
    int i;
    struct node *next; 
};

void arrange(struct node **x_head,struct node **x_temp)
{
    (*x_temp)->next=*x_head;
    *x_head=*x_temp;
}

int main()
{
    struct node *head, *temp;
    char c;
    printf("Do you want to enter data? Y/N ");
    scanf(" %c",&c);
    if((c=='Y')||(c=='y'))
    {
        head=malloc(sizeof(struct node));
        printf("Enter your data: ");
        scanf(" %d",&head->i);
        head->next=NULL;
        printf("Do you want to enter data? Y/N ");
        scanf(" %c",&c);
    }
    while((c=='Y')||(c=='y'))
    {
        temp=malloc(sizeof(struct node));
        temp->next=NULL;
        printf("Enter your data: ");
        scanf(" %d",&temp->i);
        arrange(&head,&temp);
        printf("Do you want to enter data? Y/N ");
        scanf(" %c",&c);
    }
    temp=head;
    while(temp->next!=NULL)
    {
        printf("%d ",temp->i);
        temp=temp->next;
    }
    return 0;
}
#包括
#包括
结构节点
{
int i;
结构节点*下一步;
};
无效排列(结构节点**x_头,结构节点**x_临时)
{
(*x_-temp)->next=*x_头;
*x_头=*x_温度;
}
int main()
{
结构节点*head,*temp;
字符c;
printf(“是否要输入数据?是/否”);
scanf(“%c”、&c);
if((c=='Y')| |(c=='Y'))
{
head=malloc(sizeof(结构节点));
printf(“输入数据:”);
scanf(“%d”和头->i);
head->next=NULL;
printf(“是否要输入数据?是/否”);
scanf(“%c”、&c);
}
while((c=='Y')| |(c=='Y'))
{
temp=malloc(sizeof(struct node));
temp->next=NULL;
printf(“输入数据:”);
scanf(“%d”,&temp->i);
安排(头部和温度);
printf(“是否要输入数据?是/否”);
scanf(“%c”、&c);
}
温度=水头;
while(临时->下一步!=NULL)
{
printf(“%d”,temp->i);
温度=温度->下一步;
}
返回0;
}

在头部插入节点,这样在打印时就不会错过列表中的第一个节点,而是最后一个节点,因为要测试当前节点之后的节点是否为空:

         while (temp->next!=NULL) ...
请注意,如果列表为空,则此操作将失败(或者崩溃)。将当前节点设置为空:

         temp=head;

         while (temp != NULL) {
             printf("%d ",temp->i);
             temp = temp->next;
         }

(请注意,在头部插入节点时,头部为null时不需要特殊情况;只需将新节点的
next
设置为olod头部,该头部为null。)

可能是因为您正在用temp覆盖头部指针,这样就失去了对链接列表顶部的引用。它是有效的:它只遗漏了我修复的最后一个元素。