Я читаю из файлового хранилища данные в список;
Я хочу добавить в начало списка, как я прочитал.
Кажется, вставка работает, но моя функция печати вызывает возврат -1.
Это мой код:
#include <stdio.h>
#include <stdlib.h>
Это мой список
typedef struct node{
int x,y,v;
struct node* next;
}node;
Это моя вставка:
node* insert(node* L, int x, int y, int v){
node* new= (node*)malloc(sizeof(node*));
new->x = x;
new->y = y;
new->v = v;
new->next=NULL;
if(L==NULL){
L=new;
}
else{
new->next=L;
L=new;
}
return L;
}
Проблема, кажется, здесь:
void printList(node* L){
node* c=NULL;
c=L;
while(c != NULL){
printf("x=%d, y=%d, v=%d\n", c->x, c->y, c->v);
c=c->next;
}
}
Основное:
int main(int argc, char* argv[]){
FILE* in;
int h, w;
int x, y, v;
in = fopen(argv[1], "r");
if(in == NULL )
{
puts ( "cannot open file" ) ;
exit(0) ;
}
fscanf (in, "%d,%d\n", &h, &w);
printf("%d,%d\n", h, w);
node* L=NULL;
while( !feof (in) ){
fscanf (in, "%d,%d,%d\n", &x, &y, &v);
L=insert(L, x, y, v);
//printf("x=%d, y=%d, v=%d\n", L->x, L->y, L->v);
//printf("%d,%d,%d\n", x, y, v);
}
printList(L);
return 0;
}
Что не так, пожалуйста?