2010-02-22 10 views
6

Sto provando a creare un elenco collegato singolarmente da un file di testo di input per un compito. Sto provando a farlo un po 'alla volta, quindi so che il mio codice non è completo. Ho provato a creare il puntatore testa e semplicemente a stampare il suo valore e non riesco nemmeno a farlo funzionare, ma non sono sicuro del perché. Ho incluso le funzioni struct, my create list e print list. Non ho incluso il file aperto da quando quella parte funziona.Creazione di un elenco collegato singolarmente in C

typedef struct List 
{ 
    struct List *next; /* pointer to the next list node */ 
    char *str;   /* pointer to the string represented */ 
    int count;   /* # of occurrences of this string */ 
} LIST; 

LIST *CreateList(FILE *fp) 
{ 
    char input[LINE_LEN]; 
    LIST *root;    /* contains root of list    */ 
    size_t strSize;   
    LIST *newList;   /* used to allocate new list members */ 

    while (fscanf(fp, BUFFMT"s", input) != EOF) { 

     strSize = strlen(input) + 1; 

     /* create root node if no current root node */ 
     if (root == NULL) { 
      if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) { 
       printf("Out of memory..."); 
       exit(EXIT_FAILURE); 
      } 
      if ((char *)malloc(sizeof(strSize)) == NULL) { 
       printf("Not enough memory for %s", input); 
       exit(EXIT_FAILURE); 
      } 
       memcpy(newList->str, input, strSize); /*copy string */ 
       newList->count = START_COUNT; 
       newList->next = NULL; 
       root = newList; 
     } 
    } 
     return root; 
} 

/* Prints sinly linked list and returns head pointer */ 
LIST *PrintList(const LIST *head) 
{ 
    int count; 

    for (count = 1; head != NULL; head = head->next, head++) { 
     printf("%s %d", head->str, head->count); 
    }      
    return head;  /* does this actually return the start of head ptr, b/c I want to 
          return the start of the head ptr. */ 
} 
+1

Non si desidera avere 'head ++' in 'PrintList',' head = head-> next' già incrementa il puntatore. –

+1

lo hai chiesto due volte ..? http://stackoverflow.com/questions/2309618/single-linked-lists-in-c – lorenzog

risposta

2

root ha un valore non definito, pertanto non verrà inizializzato. La seconda linea di CreateList dovrebbe essere

LIST *root = NULL; 

Inoltre, più in basso c'è l'assegnazione a quanto pare per i dettagli della voce, ma a) il codice non riesce a catturare l'assegnazione e salvarlo ovunque, e b) le dimensioni di l'assegnazione dovrebbe essere strSize, non la lunghezza della variabile stessa. Ci sono diversi modi per risolvere il problema, ma il più semplice potrebbe essere:

newList->str = (char *)malloc(strSize); 
if (newList->str == NULL) 
1

Non dovresti incrementare testa dopo head = head->next nel ciclo for. PrintList restituirà NULL ogni volta poiché il ciclo non si interromperà fino a quando la testa è NULL. Perché hai bisogno di restituire il capo della lista che hai appena passato alla funzione comunque?

Edit:

LIST *current = head; 
while (current != NULL) { 
    printf("%s %d", current->str, current->count); 
    current = current->next; 
} 
+0

Proprio quello che vuole il mio professore. E il parametro const char * mi ha confuso su come avrei ripetuto il ciclo. – Crystal

+0

Dovresti creare un puntatore al nodo corrente e inizializzarlo con head. Quindi non toccare nuovamente la testa finché non la si ritorna. –

1

Il secondo malloc alloca la memoria, ma il suo valore di ritorno non è assegnato a nulla, in modo che la memoria allocata è perduto.

newList è allocato ma non inizializzato, quindi l'utilizzo di una memcpy per copiare la memoria su newList-> str fallirà poiché newList-> str non punta a nulla. Probabilmente volevi che il risultato del secondo malloc fosse assegnato a newList-> str, ma te lo sei dimenticato.

Problemi correlati