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-10.99
/
parte2.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-01-29
|
4KB
|
190 lines
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct cell * mset;
typedef unsigned char bool;
typedef int elem;
typedef struct cell {
elem el;
int n;
mset next;
}cell;
mset makelem();
mset empty();
mset insert(elem,mset);
void print(mset);
mset delete(elem,mset);
int main()
{
mset s = empty();
int m;
FILE *fp;
char nome_f[20];
printf("\nInserire il nome del file ");
scanf("%s",nome_f);
fp=fopen(nome_f,"r");
while (fscanf(fp,"%d",&m) && (m!=-1))
{
s=insert(m,s);
}
printf("\nIl multi-insieme e': ");
print(s);
printf("\nFornisci valore: ");
scanf("%d",&m);
s=delete(m,s);
printf("\nEcco il nuovo multi-insieme: ");
print(s);
}
mset makelem()
{
return (mset) malloc(sizeof(cell));
}
mset empty()
{
return NULL;
}
mset insert(elem el,mset s)
{
mset prescorri,scorri, aux;
/* cerco la posizione (avanzo fino a trovare un elemento >= el o fine
* lista)*/
for (prescorri = empty(), scorri = s; scorri != empty() &&
(scorri ->el < el); prescorri =scorri, scorri = scorri->next);
if (scorri != empty()) /* ho trovato elemento >= el */
{
if (scorri -> el > el) /* el non e' presente, quindi lo devo
* aggiungere */
{
if (prescorri != empty()) /* l'inserimento deve essere
* effettuato all'interno della
* lista */
{aux = makelem();
aux ->el = el;
aux ->n =1;
prescorri ->next = aux;
aux ->next = scorri;
return s;}
else /* inserimento in prima
* posizione */
{ aux = makelem();
aux ->el = el;
aux->n =1;
aux->next =s;
return aux;}
}
else /* elemento presente, aumento il numero di occorrenze */
{ (scorri->n)++;
return s;
}
}
else /* la lista e' vuota o devo inserire in ultima posizione */
{
if (prescorri!= empty()) /* inserimento in ultima posizione */
{ aux = makelem();
aux ->el = el;
aux ->n = 1;
prescorri ->next = aux;
aux ->next = scorri;
return s;
}
else /* la lista era vuota */
{aux = makelem();
aux ->el = el;
aux ->n = 1;
aux->next =s;
return aux; }
}
}
void print(mset s)
{
int i;
printf("{ ");
for (; s!=empty() ; s = s ->next)
{
for(i=0;i<s->n;i++)
printf("%d ",s->el);
}
printf("}\n");
}
mset delete(elem el,mset s)
{
mset prescorri,scorri,aux ; /* prescorri punta alla locazione precedente a quella
* che verra' cancellata */
/* cerco l'elemento */
for (prescorri = empty(), scorri = s; scorri!=empty() && scorri ->el <
el; prescorri =scorri, scorri = scorri->next);
if (scorri!=empty() && (scorri->el == el))
{
if (prescorri==empty())
{ /* l'elemento da cancellare e' in prima
* posizione */
if(scorri->n == 1)
{aux = s;
s = s->next ;
free(aux);
}
else (scorri->n)--;
}
else /* l'elemento non e' in prima posizione */
{
if(scorri->n == 1)
{prescorri->next = scorri ->next;
free(scorri);
scorri = prescorri ->next; }
else (scorri->n)--;
}
}
/* restituisco la lista di partenza; */
return s;
}