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
/
turno3
/
parte3.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-06-20
|
2KB
|
123 lines
#include <stdio.h>
#include <stdlib.h>
typedef struct cell* set;
struct cell{
int info;
set next;
};
set empty(void);
void insert(int,set *);
void print(set);
int member(int,set);
int subset(set,set);
void main ()
{
int c;
char fname[20];
set s1=empty(),s2=empty();
FILE *fp=NULL;
printf ("Inserisci il nome del file di input: ");
scanf ("%s",fname);
if(fp =fopen(fname,"r"))
{
while(fscanf(fp,"%d",&c) && c!=-1)
insert(c,&s1);
while(fscanf(fp,"%d",&c) && c!=-1)
insert(c,&s2);
}
else
{
printf("\nErrore nell'apertura del file!\n");
exit(EXIT_FAILURE);
}
printf("\nInsieme 1: ");
print(s1);
printf("\nInsieme 2: ");
print(s2);
if (subset(s1,s2))
printf ("\nIl primo insieme e' contenuto/uguale al secondo\n");
else
printf ("\nIl primo insieme non e' contenuto/uguale al secondo\n");
fclose (fp);
}
set empty ()
{
return NULL;
}
void insert (int 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 ("%d\t",s->info);
printf("\n\n");
}
int member (int x, set s) /*l'implementazione piu' semplice richiedeva l'uso di una funzione member */
{
for (;s!=NULL;s=s->next)
{
if (x==s->info)
return 1;
}
return 0;
}
int subset (set s1, set s2)
{
for (;s1!=NULL;s1=s1->next)
{
if (!member(s1->info,s2))
return 0;
}
return 1;
}