danny2005
19-06-2007, 15:17
Un processo P conta il numero di servizi che ogni utente gli invia. Il processo P preleva le richieste da una stack, le elabora ed inserisce i risultati in una lista dinamica. Le richieste nello stack e gli elementi nella lista hanno le seguenti strutture:
struct richiesta
{
int codice_servizio;
char cognome [20];
}
struct elemento
{
char cognome [20];
int num_servizi;
}
Il processo P gestisce le richieste nel seguente modo:
- verifica che il codice_servizio della richiesta abbia un valore compreso tra 1 e 1024, altrimenti scarta la richiesta;
- verifica che nella lista, che è ordinata rispetto al campo cognome esista un elemento con lo stesso cognome presente nella richiesta.ùSe tale elemento è presente, ne incrementa di 1 unità la variabile num_servizi, altrimenti inserisce un nuovo elemento nella lista in maniera ordinata con il valore di num_servizi pari a 1.
Ecco come ci ho messo mano....
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct richiesta
{
int codice_servizio;
char cognome [20];
struct richiesta *next;
};
struct elemento
{
char cognome[20];
int num_servizi;
struct elemento *next;
};
PROTOTIPI DI FUNZIONE
struct elemento * preleva_richiesta_da_stack(struct richiesta **, struct richiesta **);
int elabora_elemento_stack (struct richiesta *);
void in_lista_ordinata (struct elemento **, struct elemento **);
int main ()
{
struct richiesta * testa_stack=NULL;
struct elemento *testa_lista=NULL;
struct elemento *elem=NULL;
struct richiesta * y=NULL;
int codice, esci;
while(!esci)
{
esci=1;
elem= preleva_richiesta_da_stack(&testa_stack, &y);
codice= elabora_elemento_stack(y);
if (codice >=1 && codice <= 1024)
{
esci=0;
in_lista_ordinata(&testa_lista, &elem);
}
}
retrurn 0;
}
DEFINIZIONE DELLE FUNZIONI
struct elemento * preleva_richiesta_da_stack(struct richiesta **head, struct richiesta ** query)
{
struct elemento *new;
new= (struct elemento *)malloc(sizeof(struct elemento));
if (*head ==NULL)
printf("Errore stack vuota!\n");
else
{
*query=*head;
*head= (*head)-> next;
}
strcpy(new->cognome, (*query)->cognome);
new->num_servizi= (*query)->codice_servizio;
return new;
}
int elabora_elemento_stack(struct richiesta *punt)
{
return (punt->codice_servizio);
}
void in_lista_ordinata(struct elemento **head, struct elemento ** element)
{ struct elemento *prec, *curr, *new;
new=(struct elemento *)malloc(sizeof(struct elemento));
strcpy (new->cognome, (*element)->cognome);
new->num_servizi= (*element)->num_servizi;
prec=NULL;
curr=*head;
while(curr != NULL && strcmp( curr->cognome, new->cognome)<=0)
{
[ho bisogno di allocare lo spazio per il puntatore new e ricopiare li dentro il tutto, oppure confronto direttamente *element con curr? io eviterei di usare new, voi che dite?]
if(strcmp(curr->cognome, new->cognome)==0)
(curr->num_servizi)++;
else
{
prec=curr;
curr=curr->next;
}
}
if(prec==NULL)
{ new-<next=*head;
*head=new;
new->num_servizi =1;
}
else
{
prec->next=new;
new->next=curr;
new->num_servizi =1;
}
}
senza new ci sarebbe *element nei vari passaggi
struct richiesta
{
int codice_servizio;
char cognome [20];
}
struct elemento
{
char cognome [20];
int num_servizi;
}
Il processo P gestisce le richieste nel seguente modo:
- verifica che il codice_servizio della richiesta abbia un valore compreso tra 1 e 1024, altrimenti scarta la richiesta;
- verifica che nella lista, che è ordinata rispetto al campo cognome esista un elemento con lo stesso cognome presente nella richiesta.ùSe tale elemento è presente, ne incrementa di 1 unità la variabile num_servizi, altrimenti inserisce un nuovo elemento nella lista in maniera ordinata con il valore di num_servizi pari a 1.
Ecco come ci ho messo mano....
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct richiesta
{
int codice_servizio;
char cognome [20];
struct richiesta *next;
};
struct elemento
{
char cognome[20];
int num_servizi;
struct elemento *next;
};
PROTOTIPI DI FUNZIONE
struct elemento * preleva_richiesta_da_stack(struct richiesta **, struct richiesta **);
int elabora_elemento_stack (struct richiesta *);
void in_lista_ordinata (struct elemento **, struct elemento **);
int main ()
{
struct richiesta * testa_stack=NULL;
struct elemento *testa_lista=NULL;
struct elemento *elem=NULL;
struct richiesta * y=NULL;
int codice, esci;
while(!esci)
{
esci=1;
elem= preleva_richiesta_da_stack(&testa_stack, &y);
codice= elabora_elemento_stack(y);
if (codice >=1 && codice <= 1024)
{
esci=0;
in_lista_ordinata(&testa_lista, &elem);
}
}
retrurn 0;
}
DEFINIZIONE DELLE FUNZIONI
struct elemento * preleva_richiesta_da_stack(struct richiesta **head, struct richiesta ** query)
{
struct elemento *new;
new= (struct elemento *)malloc(sizeof(struct elemento));
if (*head ==NULL)
printf("Errore stack vuota!\n");
else
{
*query=*head;
*head= (*head)-> next;
}
strcpy(new->cognome, (*query)->cognome);
new->num_servizi= (*query)->codice_servizio;
return new;
}
int elabora_elemento_stack(struct richiesta *punt)
{
return (punt->codice_servizio);
}
void in_lista_ordinata(struct elemento **head, struct elemento ** element)
{ struct elemento *prec, *curr, *new;
new=(struct elemento *)malloc(sizeof(struct elemento));
strcpy (new->cognome, (*element)->cognome);
new->num_servizi= (*element)->num_servizi;
prec=NULL;
curr=*head;
while(curr != NULL && strcmp( curr->cognome, new->cognome)<=0)
{
[ho bisogno di allocare lo spazio per il puntatore new e ricopiare li dentro il tutto, oppure confronto direttamente *element con curr? io eviterei di usare new, voi che dite?]
if(strcmp(curr->cognome, new->cognome)==0)
(curr->num_servizi)++;
else
{
prec=curr;
curr=curr->next;
}
}
if(prec==NULL)
{ new-<next=*head;
*head=new;
new->num_servizi =1;
}
else
{
prec->next=new;
new->next=curr;
new->num_servizi =1;
}
}
senza new ci sarebbe *element nei vari passaggi