Lista linkata in C
Salve a tutti avrei bisogno di una mano per trovare l'errore presente nel programma. L'esercizio chiede di sommare degli interi utilizzando le liste linkate.
Qualcuno potrebbe aiutarmi? grazie in anticipo
#include<stdio.h>
#include<stdlib.h>
struct elemento{
int valore;
struct elemento*next;
};
struct elemento*inserisci_elemento(struct elemento*punt_lista,int valoredainserire)
{
struct elemento*llist;
if(punt_lista==NULL)
{
punt_lista=(struct elemento*)malloc(sizeof(struct elemento));
punt_lista->valore=valoredainserire;
punt_lista->next=NULL;
return punt_lista;
}
llist=punt_lista;
while(llist->next!=NULL){
llist=llist->next;
}
llist->next=(struct elemento*)malloc(sizeof(struct elemento));
llist->next->valore=valoredainserire;
llist->next->next=NULL;
return punt_lista;
}
int somma_elementi_lista(struct elemento*punt_lista)
{ struct elemento*llist;
int sum=0;
int valoredainserire;
if(punt_lista==NULL) {
return sum;}
llist=punt_lista;
while (llist!=NULL){
sum +=somma_elementi_lista(llist);
llist=llist->next;
}
return sum;
}
int main(void){
int valore=0;
int input=1;
int sum=0;
struct elemento*first=NULL;
while(input!=0){
printf("\n-- MENU SELECTION --\n");
printf("0) QUIT\n");
printf("1) INSERISCI VALORI\n");
printf("2) SOMMA VALORI\n");
printf("SCELTA-->");
scanf("%d",&input);
switch(input) {
case 0:
default:
printf("GOODBYE...\n");
input =0;
break;
case 1:
printf("INSERISCI VALORI\n");
scanf("%d",valore);
inserisci_elemento(first,valore);
break;
case 2:
sum=somma_elementi_lista(first);
printf("la somma dei valori e': %d",sum);
break;
}
}
free(first);
system("pause");
return(0);
}
Qualcuno potrebbe aiutarmi? grazie in anticipo
Risposte
Che errore ti restituisce?
E' un errore in esecuzione, o in compilazione?
E' un errore in esecuzione, o in compilazione?