Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 13 su 13
  1. #11
    codice:
     
    /*
    ### Copyright (c) 2004 Luca Francesca
    ### This script is provided under the terms of the GNU General Public License
    ### (see http://www.gnu.org/licenses/gpl.html for the full license text.)
    */
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include "Common.h"
    
    typedef struct _ListNode *List;
    typedef int base_type;
    
    struct _ListNode
    {
    	base_type data;
    	List next;
    };
    
    List Init(base_type val);
    List Add(base_type val, List aux);
    void Delete(List aux);
    void Show(List P);
    
    int main(int argc, char *argv[])
    {
    	srand(time(NULL));
    	List root = Init(10);
    	int i = 0, ival = 0;
    	for(i; i < 10;++i)
    	{
    		ival = i + (rand()%10);
    		root = Add(ival, root);
    	}
    	Show(root);
    	Delete(root);
    	SAFE_DELETE(root)
      ExitFunction();
      return 0;
    }
    
    List Init(base_type val)
    {
    	List tmp;
    	tmp = malloc(sizeof(struct _ListNode));
    	tmp->data = val;
    	tmp->next = NULL;
    	return tmp;
    }
    List Add(base_type val, List aux)
    {
    	if(aux == NULL)
    	{
    		aux = malloc(sizeof(struct _ListNode));
    		aux->data = val;
    		aux->next = NULL;
    	}
    	else
    		aux->next = Add(val, aux->next);
    	return aux;
    		
    }
    
    void Show(List aux)
    {
         while(aux != NULL)
         {
         	fprintf(stdout, "%d \n", aux->data);
         	aux = aux->next;
         }
    }
    void Delete(List aux)
    {
    	while(aux != NULL)
    	{
    		free(aux->next);
    		free(aux);
    	}
    	aux = NULL;
    }
    Prendi questo come spunto magari
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #12
    Utente di HTML.it
    Registrato dal
    Oct 2003
    Messaggi
    1,258
    Grazie Lucas, però nel caso che mi hai postato non c'è una struttura interna alla lista, quindi non può esserci il problema che ho avuto io

  3. #13
    Utente di HTML.it
    Registrato dal
    Oct 2003
    Messaggi
    1,258
    ho risolto il problema facendo una malloc anche del record interno alla struttura. Ci ho pensato alla fine, ma era l'unica soluzione.


    codice:
    //inserisce nella lista il nodo al posto giusto
    status allocanodo(lista *r_L, record *d){
     record *r;
     *r_L = (lista)malloc(sizeof(struct nodo));
     if(*r_L==NULL) return ERROR;
     else {
        r = (record *) malloc(sizeof(record));
       *r = *d;
       (*r_L)->elemento= r;
       (*r_L)->next=NULL;
     }
     return OK; 
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.