大家好,我有这个代码。我必须从记录文件中创建一个列表,其中我有数据列( position, time, flags)
。一切都很顺利但是当我在std
输出上打印时,列表中有1个额外的零元素,列表又回到了前面。有人可以向我解释原因吗?
非常感谢大家!
这是我从输出中得到的一个例子:
::(0.0,0.000000000000000,OK)
::(14000.0,314.000023350630840,OK)
::(21000.0,314.000035023289740,OK)
::(736000.0,314.001227515303469,OK)
::(647000.0,314.001079063932764,NOISE)
::(70000.0,314.000116745763364,OK)
::(487000.0,314.000812207375702,OK)
::(81000.0,314.000135099892645,OK)
::(917000.0,314.001529387085043,NOISE)
::(871000.0,314.001452666758723,OK)
::(380000.0,314.000633767941338,OK)
::(578000.0,314.000964003751733,OK)
::(289000.0,314.000481997603799,OK)
::(597000.0,314.000995684957161,OK)
::(480000.0,314.000800553021577,OK)
::(264000.0,314.000440315509820,OK)
::(408000.0,314.000680456580596,OK)
::(603000.0,314.001005693768377,OK)
::(757000.0,314.001262536680713,OK)
::(369000.0,314.000615422503131,OK)
::(458000.0,314.000763851368049,OK)
以下是代码:
typedef enum { NOTORD=0, TIME=1, POSITION=2 } ord_t; //list order: 0 not ordered, 1 ordered by time, 2 ordered by position
typedef enum { OK=0, NOISE=2 } flag_t; //good/bad record
typedef struct elem {
/** position record*/
double position;
/** time record */
double time;
/** flag noise/good */
flag_t flag;
/** pointer to next element */
struct elem * next;
} elem_t;
typedef struct {
/** pointer to headof list */
elem_t * head;
/** nnumber of element in list */
int nelem;
/** indicate the type of order of the list */
ord_t ord;
} lista_t;
lista_t * new_lista ( void ) { //list creation
lista_t * l ;
if ( ( l = malloc (sizeof ( lista_t ) ) ) == NULL ) {
return NULL;
}
l->head = NULL ;
l->nelem = 0 ;
l->ord = NOTORD;
return l;
}
elem_t* leggi_registrazione (FILE* fd) { //read records from file
elem_t * puntolista;
if (!feof(fd))
{
puntolista=malloc(sizeof(elem_t));
fscanf(fd, "%lf", &(puntolista->position));
fscanf(fd, "%lf", &(puntolista->time));
fscanf(fd, "%d", (int *)&(puntolista->flag));
puntolista->next= NULL;
return puntolista;}
else {return NULL;}
}
static void stampa_lista_r ( FILE * f, elem_t * h ) { //list print funct to use in stampa_lista
if ( h == NULL ) {
return ;
}
if ( h->flag == OK ) fprintf(f,"::(%.1f,%.15f,OK)\n",h->position, h- >time);
if ( h->flag == NOISE ) fprintf(f,"::(%.1f,%.15f,NOISE)\n",h->position, h->time);
stampa_lista_r(f, h->next);
}
void stampa_lista ( FILE * f, lista_t * l ) { //list print function
if ( l == NULL ) return ;
if ( l-> head == NULL ) {
fprintf(stdout,"Lista vuota.\n");
return ;
}
stampa_lista_r (f,l->head);
putchar('\n');
return ;
}
int inserisci ( lista_t * l , elem_t* pelem) { //element insert in list
elem_t *prec;
elem_t *corr;
elem_t *aux;
int trovato = 0;
if (l->ord==NOTORD) //not ordered list
{
//insert at the beginning
aux = malloc(sizeof(elem_t));
aux->position = pelem->position;
aux->time = pelem->time;
aux->flag = pelem->flag;
aux->next = l->head;
l->head = aux;
return 0;
}
if (l->ord==TIME) //time ordered list
{
prec=malloc(sizeof(elem_t));
corr=malloc(sizeof(elem_t));
prec = l->head;
corr = prec -> next;
trovato = 0;
while (corr !=NULL && !trovato)
{
if ((corr -> time) >= (pelem->time))
trovato = 1;
else
{prec = prec -> next;
corr = corr -> next;}
}
aux = malloc(sizeof(elem_t)); //alloc and insert in list
aux->position = (pelem->position);
aux->time = (pelem->time);
aux->flag = (pelem->flag);
prec -> next = aux;
aux -> next = corr;
return 0;
}
if (l->ord==POSITION) //position ordered list
{
prec=malloc(sizeof(elem_t));
corr=malloc(sizeof(elem_t));
prec = l->head;
corr = prec -> next;
trovato = 0;
while (corr !=NULL && !trovato)
{
if ((corr -> position) >= (pelem->position))
trovato = 1;
else
{prec = prec -> next;
corr = corr -> next;}
}
aux = malloc(sizeof(elem_t)); //alloc and insert in list
aux->position = (pelem->position);
aux->time = (pelem->time);
aux->flag = (pelem->flag);
prec -> next = aux;
aux -> next = corr;
return 0;
}
else return -1;
}
int main (void)
{
FILE* f; // pointer to record file
lista_t * ll;
elem_t* pelem;
int err;
char buf[N+2];
f = fopen (FILE1,"r");
if ( f == NULL ) {
perror("fopen");
exit(EXIT_FAILURE);
}
/* delete first line in external file(it's a comment) */
if ( fgets(buf,N+2,f) == NULL ) exit(EXIT_FAILURE);
if (buf[0] != '#') exit(EXIT_FAILURE);
/* read records and insert in new list */
ll = new_lista();
while ( ( pelem = leggi_registrazione(f) ) != NULL ) {
/* insert in list in reading order */
err = inserisci (ll,pelem);
if ( err == -1 ) return EXIT_FAILURE;
}
/* results print */
stampa_lista(stdout,ll) ;
return 0;}