Ho ancora un problema sulle liste,con 2 liste se volessi fare
l1 1->2->3->4->6->7
l2 8->9->10->11->12
la lista risultante sarà tutti gli elementi in pos compresa tra 2 valori,es
2->9->3->10->4->11
ma nella funzione lascio sicuramente dietro qualcosa perchè si blocca...
codice:
#include <stdlib.h>
#include <stdio.h>
typedef struct nod {
int data;
struct nod *next;
struct nod *prev;
} node;
node *newnode(void)
{
return (node *)malloc(sizeof(node));
}
/* Dato un intero n>0, costruisce la lista di nodi da 1 ad n */
node *buildlis()
{
int x;int z=0;
node *lis, *p, *last;
printf("nuovo numero da inserire in lista:\n");
scanf("%d", &x);
if (x==00000)
lis= NULL; /* caso di lista vuota */
else
{
last=newnode();
lis = last;
last->data = x;
last->next = NULL;
printf("nuovo numero da inserire in lista (00000 per terminare):\n");
scanf("%d", &x);
z=z+1;
while (x!=00000)
{
p=newnode();
p->data = x;
p->next = NULL;
last->next = p;
last = p;
printf("nuovo numero da inserire in lista (00000 per terminare):\n");
scanf("%d", &x);
z=z+1;
}
}
if (z>0) printf("Il valore e' %d\n",z);
if(z==0) printf("|||||-----lista vuota-----|||||\n");
return(lis);
}
/* Stampa degli elementi di una lista */
void printlis(node *lis)
{
node* tmp;
tmp = lis;
int somma=0;float media=0;int conteggio=0;
while (tmp != NULL) {
printf(">>>> %d\n", tmp->data) ;
tmp = tmp->next;
}
}
void myFree ( node* top )
{
node* tmp;
tmp = top;
while ( top->next != NULL ) {
tmp = top->next;
free(top);
top = tmp;
}
}
int conteggio(node *lis)
{
int acc=0;
while (lis != NULL)
{
acc=acc+1;
lis=lis->next;
}
return(acc);
}
node* moltiplica(node *l1,node *l2)
{
node *p,*head,*tail;
head=NULL;int n=0;int pos=1;
if((l1==NULL) && (l2==NULL)) return NULL;
while ((l1!=NULL) && (l2!=NULL)){
if(pos>2){
if (n%2!=0) {
p=newnode();
p->data=l1->data;
p->next=NULL;
if(head==NULL){head=p,tail=p;}
else{tail->next=p;tail=tail->next;}
l1=l1->next;
n++;
}
if (n%2==0) {
p=newnode();
p->data=l2->data;
p->next=NULL;
if(head==NULL){head=p,tail=p;}
else{tail->next=p;tail=tail->next;}
l2=l2->next;
n++;
}
pos++;}
}
return head;}
int main()
{
node* head;node* head2;node *dup;
printf ("-----LISTA A-----\n");
head = buildlis ( );
printf ("-----LISTA B-----\n");
head2 = buildlis ( );
printf ("\n-----STAMPA LISTA A-----\n");
printlis(head);
printf ("\n-----STAMPA LISTA B-----\n");
printlis(head2);
printf ("\n-----CONTROLLO-----\n");
dup=moltiplica(head,head2);
printlis(dup);
if(head==NULL) printf ("\nNESSUN RISULTATO ESPRIME I VALORI VOLUTI\n");
free ( head );
return 0;
}