【发布时间】:2020-08-21 06:25:17
【问题描述】:
我正在尝试在链表的末尾插入一些节点,但这段代码有问题。
在这里,我正在尝试创建一个循环,并且在此循环的帮助下,我希望用户输入列表的所有数字,但我想我遗漏了一些东西。
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
void insert(struct node** ref)
{ int n,i=0 ;
printf("how many numers:\n");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
struct node* last = *ref;
printf("enter the number:\n");
scanf("%d",&(temp->data));
fflush(stdin);
temp->next = NULL;
if(*ref==NULL)
{
*ref = temp;
return;
}
while(last->next != NULL)
last = last->next;
last->next = temp;
}
return;
}
int main()
{
struct node* head=NULL;
insert(&head);
return 0;
}
【问题讨论】:
标签: c linked-list singly-linked-list insertion function-definition