2016-12-01 1 views
0

überprüfen und aktualisieren Ich versuche, eine meiner Aufgaben zu beenden, und ich habe einige Probleme. Ich muss ein Programm erstellen, das struct verwendet, um eine Linkliste zu erstellen, in der ich Wörter hinzufügen muss. Wenn das Wort bereits in der verknüpften Liste ist, muss ich nur die Frequenz aktualisieren.wie ein Wort in der Linkliste hinzuzufügen, während auch die Häufigkeit des Wortes in c

ich dies bereits haben:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct words Words; 
struct words{ 
    char *word; 
    int freq; 
    Words *next; 
}; 

/* 
Inserts a copy of newWord into the list, in lexicographical order. If newWord is already 
in the list, increment the freq member of the node. The function returns a pointer to 
the list. 
*/ 
Words *addWord(Words *headPtr, char* newWord){ 
Words *current = headPtr; 
if(headPtr == NULL) 
{ 
    current->word = newWord; 
    current->freq = 1; 
} 
else 
{ 
    while(current != NULL) 
     if(strcmp(headPtr->word, newWord)) 
     { 
      current->freq++; 
      return headPtr; 
     } 
     else 
     { 
      current->word = newWord; 
      current->freq = 1; 
     } 
} 
return headPtr; 
} 

//prints the words in the list, along with the frequency of each word 
void printWords(Words *headPtr){ 

    while(headPtr != NULL) 
    { 
     printf("%s: %d", headPtr->word, headPtr->freq); 
     headPtr = headPtr->next; 
    } 
} 

//frees the entire list. Note: Words **headPtr since the headPtr NULL upon return 
void deleteList(Words **headPtr){ 
    Words *current = *headPtr; 
    Words *next; 
    while(current != NULL) 
    { 
     next = current->next; 
     free(current); 
     current = next; 
    } 
    *headPtr = NULL; 
} 

int main(){ 
    char word[20]; 
    Words *list = NULL; 
    scanf("%s", word); 
    while(!feof(stdin)){ 
     list = addWord(list, word); 
     scanf("%s", word); 
    } 
    printWords(list); 
    deleteList(&list); 
} 
+0

In Klammern auf Ihre Blöcke, wenn sie mehr als eine Zeile haben, sonst sieht es verwirrend, den Compiler sicher versteht sofort, aber wenn nur Compiler Code lesen sollen, dann gibt es keinen Grund, die Funktion 'printWords()' aufzurufen. –

Antwort

1

Es gibt einige Probleme in Ihrem Code. Siehe Kommentare in den Code eingebettet:

Words *addWord(Words *headPtr, char* newWord){ 
    Words *current = (Words*) malloc(sizeof(Words)); // Don't malloc here. 
                 // You don't know yet 
                 // whether you need 
                 // a new node or you 
                 // you just need to 
                 // update freq 

    if(current == NULL)     // If current is NULL you have 
              // serious problems, i.e. you 
              // are out of memory. 
              // Did you really intended to do: 
              // if (headPtr == NULL) 

    { 
     current->word = newWord; 
     *current->next = (*headPtr); 
     (*headPtr) = *current;   // I'm not sure what you try here 
              // but it seems strange 
    } 
    else 
    { 
     while(current != NULL) 
      if(strcmp(headPtr->word, newWord)) // This is not the way to compare 
               // strings. Two strings compare 
               // when "strcmp" returns 0. 
               // 
               // Further you don't want to 
               // use headPtr here. 

      { 
       current->freq++; // Use of uninitialized value 
       return;   // Missing argument to return 
      } 
      else 
      { 
       current->word = newWord;  // Use of uninitialized value 
       *current->next = (*headPtr); // Use of uninitialized value 
       (*headPtr) = *current; 
      } 
    } 

    // Missing return 

} 

Hier einige Code starten mit:

#define WORD_SIZE 20 

struct words{ 
    char word[WORD_SIZE]; // Use a char array 
    int freq; 
    Words *next; 
}; 

Words *addWord(Words *headPtr, char* newWord) 
{ 
    Words *current = headPtr; // Make a copy of headPtr 
    Words* new; 

    if ((current == NULL) || (strcmp(current->word, newWord) > 0)) 
    { 
     // Insert in front of list 
     new = malloc(sizeof(Words)); // Allocate memory 
     if (new == NULL) 
     { 
      // oh, dear - out of memory - print an error message and exit 
      exit(1); 
     } 

     strncpy(new->word, newWord, WORD_SIZE); // Make sure not to overflow 
                // the buffer, so use strncpy 
     (new->word)[WORD_SIZE-1] = '\0'; // Make sure to zero terminate 
     new->freq = 1; 
     new->next = headPtr; 

     return new; 
    } 

    while(1) 
    { 
     int cmp = strcmp(current->word, newWord); 
     if(cmp == 0) 
     { 
      current->freq++; 
      return headPtr; 
     } 

     if(cmp < 0) 
     { 
      if ((current->next == NULL) || (strcmp(current->next->word, newWord) > 0)) 
      { 
       // Add code to insert node after current 

       return headPtr; 
      } 
     } 
     else 
     { 
      // This should never happen... 
      printf("BAD CODE 1\n"); 
      exit(1); 
     } 

     current = current->next; 
    } 
} 
+0

Was soll ich genauer machen? –

+0

@StefanCiprianIuga - Welcher Teil verursacht Ihnen Ärger? – 4386427

+0

das Programm hört einfach auf zu arbeiten und gibt den nicht antwortenden Fehler. Ich habe es ein bisschen geändert, wo Sie gesagt haben, aber ich kann es nicht funktionieren –

Verwandte Themen