FraFut
Nuovo Utente
- Messaggi
- 6
- Reazioni
- 0
- Punteggio
- 22
Salve dovre creare qusto programma in linguaggio C
Scrivere la funzione C che riceve in ingresso due liste collegate con puntatori L1 ed L2 di valori float, e due valori float, target1 e target2 ed opera nel modo seguente:
vorrei capire dove sbaglio, grazie di eventuali risposte.
il codice che ho scritto è questo:
Scrivere la funzione C che riceve in ingresso due liste collegate con puntatori L1 ed L2 di valori float, e due valori float, target1 e target2 ed opera nel modo seguente:
- Cerca gli elementi in L1 con valore uguale a target1, e gli elementi in L2 con valore uguale a target2;
- Scollega da L1 gli elementi con valore uguale a target1 e li collega in L2 al posto degli elementi con valore uguale a L2; fa la cosa opposta per gli elementi di L2 uguali a target2.
vorrei capire dove sbaglio, grazie di eventuali risposte.
il codice che ho scritto è questo:
C:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef unsigned short int Boolean;
struct list{
float value;
struct list *next_ptr;
};
void init(struct list **ptrptr);
Boolean PreInsert(struct list **ptrptr,float value);
void visit(struct list *ptr);
Boolean Search(struct list *l, float target);
void CompliteList(struct list **l1,struct list **l2,float target1,float target2);
void CompliteList2(struct list **l1,struct list **l2,float target1,float target2);
int main() {
struct list *L1;
struct list *L2;
float target1 = 2;
float target2 = 3;
init(&L1);
//inserimento dati Lista 1
PreInsert(&L1, 4);
PreInsert(&L1, 9);
PreInsert(&L1, 2);
PreInsert(&L1, 9);
PreInsert(&L1, 2);
//stampa elemneti prima lista
visit(L1);
init(&L2);
//inserimento elementi lista 2
PreInsert(&L2, 8);
PreInsert(&L2, 1);
PreInsert(&L2, 3);
PreInsert(&L2, 5);
PreInsert(&L2,3);
//sampa lista 2
visit(L2);
printf("\n");
if (Search(L1, target1) == TRUE) {
printf("\nil valore cercato e' presente nella lista 1\n");
} else {
printf("\nil valore carcato non e' presente nella lista 1\n");
}
if (Search(L2, target2) == TRUE) {
printf("\nil valore cercato e' presente nella lista 2\n");
} else {
printf("\nil valore cercato non e' presente nella lista 2\n");
}
//scambio valori
CompliteList(&L1,&L2,target1,target2);
CompliteList2(&L1,&L2,target1,target2);
//sampa ellementi definitivi
visit(L1);
visit(L2);
}
void init(struct list **ptrptr)
{
*ptrptr=NULL;
}
//inserimento valori
Boolean PreInsert(struct list **ptrptr,float value)
{
struct list *tmpPtr;
tmpPtr = (struct list *)malloc(sizeof(struct list));
if ( tmpPtr != NULL ) {
tmpPtr->value = value;
tmpPtr->next_ptr = *ptrptr;
*ptrptr = tmpPtr;
return TRUE;
}
else
return FALSE;
}
//stampa lista
void visit(struct list * ptr)
{
printf("\nList : ");
while ( ptr != NULL ) {
printf("[ %f ] ", ptr->value);
ptr = ptr->next_ptr;
}
}
//esercizio 5
Boolean Search(struct list *l, float target)
{
while (l!=NULL)
{
if(l->value==target)
return TRUE;
l=l->next_ptr;
}
return FALSE;
}
void CompliteList(struct list **l1,struct list **l2,float target1,float target2)
{
struct list *tmp1=NULL,*tmp2=NULL;
while (*l1!=NULL )
{
if((*l1)->value == target1){
//scollega l'elemento della lista 1
tmp1=*l1;
*l1=(*l1)->next_ptr;
//scollega l'elemneto della lista 2;
tmp2 = *l2;
*l2 = (*l2)->next_ptr;
//collega l'elelmento della lista 1 nella lista 2
*l2=tmp1;
(*l2)->next_ptr = tmp2;
} else {
l1 = &((*l1)->next_ptr);
}
}
}
void CompliteList2(struct list **l1,struct list **l2,float target1,float target2)
{
struct list *tmp1=NULL,*tmp2=NULL;
while (*l2!=NULL)
{
if((*l2)->value == target2) { //scollega l'elemento della lista 2
tmp2 = *l2;
*l2 = (*l2)->next_ptr;
//scollega l'elemneto dell alista 1;
tmp1 = *l1;
*l1 = (*l1)->next_ptr;
//collega l'elelmento nella lista 1
*l1=tmp2;
(*l1)->next_ptr=tmp1;
} else
l2 = &((*l2)->next_ptr);
}
}
Ultima modifica da un moderatore: