codice:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct nodo *crealista();
struct nodo2 *crealista2();
void printlista(struct nodo *p);
void printlista2(struct nodo2 *p);
struct nodo {
int dato;
struct nodo *next;
};
struct nodo2 {
int dato;
struct nodo2 *next;
};
main()
{
struct nodo *start;
struct nodo2 *start2;
start = crealista();
printlista(start);
start2 = crealista2();
printlista2(start2);
system("pause");
return 0;
}
struct nodo *crealista()
{
struct nodo *p,*start,*last;
int n,i,x;
srand(time(NULL));
start = NULL;
printf("Quanti nodi ha la lista?");
scanf("%d",&n);
for(i=0;i<n;i++){
x = rand() % 9+1;
p = (struct nodo *)malloc(sizeof(struct nodo));
if (i==0)
start = p;
else
last->next = p;
p->dato = x;
p->next = NULL;
last = p;
}
return (start);
}
struct nodo2 *crealista2()
{
struct nodo2 *p2,*start2,*last2;
struct nodo *p;
int i,x2;
start2 = NULL;
while(p!==NULL){
x2=p->dato;
for(i=0;i<x2;i++){
p2=(struct nodo2*)malloc(sizeof(struct nodo2));
if(i==0)
start=p2;
else
last2->next=p2;
p2->dato = x2;
p2->next = NULL;
last2 = p2;
}
p = p->next;
}
return(start2);
}
void printlista(struct nodo *p)
{
while(p != NULL){
printf("%d ---> ",p->dato);
p = p->next;
}
printf("NULL");
return;
}
Cosi va bene?