【发布时间】:2022-12-16 01:11:57
【问题描述】:
while (tempcounter > 0){
ticket *new=(ticket*)malloc(sizeof(ticket));
ticket *old=*current;
fscanf(f, "%s %s %s %s", temps, tempt, tempn, tempi);
new->visitor.SrNo=temps;
new->visitor.ticketNo=tempt;
new->visitor.Name=tempn;
new->visitor.ID=tempi;
new->nextPtr=NULL;
if (*current == NULL){
*current=new;
printf("sucess\n");
}
else {
while (old->nextPtr != NULL) {
old = old->nextPtr;
}
old->nextPtr = new;
}
tempcounter--;
}
当从文件中读取时,第一个数据循环将被第二个数据循环覆盖,当我在另一个循环中打印当前头部时,它只会打印最后一个插入的元素。有谁知道为什么?
【问题讨论】:
-
您需要edit并出示minimal reproducible example。您未显示的代码中的其他地方很可能存在问题。
标签: c file linked-list