Ragazzi questo è il codice:
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct dati* numero;
struct dati
{
int numero;
numero next;
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
numero lsist_delete_node(numero x);
void search_node(numero* n);
void stampalista(numero n);
int cercanumero(int m,int array[50]);
numero lsist_insert_order(numero head,numero n);
int main(int argc, char *argv[]) {
srand(time(NULL));
FILE* fp;
int x; int l;
fp=fopen("numericasuali.txt","w");
int array[51];
for (x=0;x<50;x++)
{
int s=2 + rand() %51;
if (l=cercanumero(s,array)!=0)
array[x]=s;
else x--;
}
for (x=0;x<50;x++)
{
if(x==49)
fprintf(fp,"%d",array[x]);
else
fprintf(fp,"%d\n",array[x]);
}
fclose(fp);
fp=fopen("numericasuali.txt","r");
numero il=NULL; numero nodo=NULL;
while(!feof(fp))
{
nodo=malloc(sizeof(struct dati));
fscanf(fp,"%d\n",&nodo->numero);
nodo->next=NULL;
il=lsist_insert_order(il,nodo);
}
fclose(fp);
numero temp=il;
while(temp!=NULL)
{
int z=temp->numero;
if(temp->next->numero%z==0)
{
temp=lsist_delete_node(temp);
/*if(temp->next==NULL)
temp->next=NULL;
else{
numero tmp=temp->next;
temp->next=tmp->next;}*/
}
temp=temp->next;
}
stampalista(il);
system ("PAUSE");
return 0;
}
/*void search_node(numero* n)
{
int m=(*n)->numero;
while((*n)!=NULL)
{
if(m%(*n)->next->numero==0)
delete_node(&n);
}
}
*/
int cercanumero(int m,int array[50])
{
int x;int s;
for (x=0;x<50;x++)
{
if (array[x]==m ){
int s=0;
return s;}
}
int o=10;
return o;
}
numero lsist_insert_order(numero head,numero n){
/*Se la lista è vuota deve mettere il nodo n in testa*/
if(head==NULL)
return n; /*ritorna n come testa della lista*/
numero t=head; /*t puntatore di appoggio per scorrere la lista*/
/*Controlla se deve inserire il nodo n in testa alla lista*/
if (n->numero < t->numero) {
n->next=t; /*sposta il puntatore del nodo successivo di n alla vecchia testa*/
return n; /*ritorna n come testa della lista*/
}
/*Scorre la lista */
while(t->next!=NULL) { /*si ferma all'ultimo nodo*/
if (n->numero < t->next->numero) { /*confronta i campi item fra n e il nodo successivo a t*/
n->next=t->next; /*inserisce il nodo n */
t->next=n; /* dopo il nodo t */
return head; /*ritorna la testa*/
}
t=t->next; /*sposta t al nodo successivo per scorrere la lista*/
}
t->next=n; /*aggiunge n alla fine della lista*/
return head; /*ritorna la testa*/
}
void stampalista(numero n)
{
while(n!=NULL)
{
printf ("%d\n",n->numero);
n=n->next;
}
}
numero lsist_delete_node(numero x){
if (x->next==NULL) return x;
numero t=x->next; /*salvo il puntatore al nodo da togliere in t */
x->next=t->next; /*tolgo il nodo t dalla lista */
return x;
}
Ho messo in grassetto il punto dove mi crea problemi il codice. In pratica devo eliminare i multipli di ogni numero (crivello di Eratostene) e ho creato quell'agoritmo, solo che il programma mi crasha sia chiamando la funzione scritta alla fine e sia scrivendola direttamente nel while (l'ho messa tra commenti).
Un aiutino?