home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
testi-esami
/
labo-6.00
/
turno4
/
parte1.c
next >
Wrap
C/C++ Source or Header
|
2000-06-20
|
1KB
|
87 lines
#include <stdio.h>
#include <stdlib.h>
typedef struct cell* set;
struct cell{
float info;
set next;
};
set empty(void);
void insert(float,set *);
void print(set);
void main ()
{
float c;
char fname[20];
set s=empty();
FILE *fp=NULL;
printf ("Inserisci il nome del file di input: ");
scanf ("%s",fname);
if (fp=fopen(fname, "r"))
{
while ((fscanf(fp, "%f",&c)) && (c!=0))
insert (c, &s);
}
else
{
printf("\nErrore nell'apertura del file!\n");
exit(EXIT_FAILURE);
}
printf ("Insieme:\n");
print (s);
fclose (fp);
}
set empty()
{
return NULL;
}
void insert (float x, set *s)
{
set prec=NULL;
set corr=*s;
set aux;
aux=(set) malloc (sizeof(struct cell));
aux->info=x;
aux->next=NULL;
while((corr!=NULL) && (x<corr->info))
{
prec=corr;
corr=corr->next;
}
if ((prec==NULL)&&(corr==NULL || x!=corr->info)) /*inserimento in testa */
{
aux->next=*s;
*s=aux;
}
else if (corr==NULL) /* inserimento in coda */
{
prec->next=aux;
return;
}
else if (x!=corr->info) /* inserimento in mezzo */
{
prec->next=aux;
aux->next=corr;
}
}
void print (set s)
{
for (; s!=NULL; s=s->next)
printf ("%.2f\t",s->info);
printf("\n\n");
}