I don't get any error; the code works but it's showing only first element and doesn't show others. I want to show all of the numbers in file with linked list. What is my mistake?
The numbers in numbers.txt
:
9 2 3
4 2 3
1 2 9
2 4 8
7 5 9
2 4 7
Here's my code:
#include <stdio.h>
#include <stdlib.h>
struct NUMBERS{
int katsayi;
int taban;
int us;
NUMBERS *next;
};
int main(){
NUMBERS *temp=NULL;
NUMBERS *list=NULL; // to keep first element
NUMBERS *head=NULL; // temp for the first element
FILE *fp=fopen("numbers.txt","r+");
while(!feof(fp)){
temp=(NUMBERS*)malloc(sizeof(NUMBERS));
fscanf(fp,"%d %d %d",&temp->katsayi,&temp->taban,&temp->us);
temp->next=NULL;
if(list==NULL){
list=temp;
}
else{
head=list;
while(head->next!=NULL){
head=head->next;
}
head=temp;
}
}
head=list;
while(head!=NULL){
printf("%d %d %d",head->katsayi,head->taban,head->us);
head=head->next;
}
return 0;
}