Domanda

Sono abbastanza nuovo per C e sto cercando di attuare un albero binario in C che memorizzare un numero e una stringa e poi stampare per es.

1 : Bread
2 : WashingUpLiquid
etc.

Il codice che ho finora è:

#include <stdio.h>
#include <stdlib.h>
#define LENGTH 300

struct node {
 int data;
 char * definition;
 struct node *left;
 struct node *right;
};

struct node *node_insert(struct node *p, int value, char * word);

void print_preorder(struct node *p);

int main(void) {
  int i = 0;
  int d = 0;
  char def[LENGTH];
  struct node *root = NULL; 

  for(i = 0; i < 2; i++)
  {
    printf("Please enter a number: \n");
    scanf("%d", &d);
    printf("Please enter a definition for this word:\n");
    scanf("%s", def);
    root = node_insert(root, d, def);
    printf("%s\n", def);
  }

  printf("preorder : ");
  print_preorder(root);
  printf("\n");

  return 0;
}

struct node *node_insert(struct node *p, int value, char * word) {
  struct node *tmp_one = NULL;
  struct node *tmp_two = NULL;

  if(p == NULL) {
    p = (struct node *)malloc(sizeof(struct node));
    p->data = value;
    p->definition = word;
    p->left = p->right = NULL;
  }
  else {
    tmp_one = p;
    while(tmp_one != NULL) {
      tmp_two = tmp_one;
      if(tmp_one->data > value)
        tmp_one = tmp_one->left;
      else
        tmp_one = tmp_one->right;
    }

    if(tmp_two->data > value) {
      tmp_two->left = (struct node *)malloc(sizeof(struct node));
      tmp_two = tmp_two->left;
      tmp_two->data = value;
      tmp_two->definition = word;
      tmp_two->left = tmp_two->right = NULL;
    }
    else {
      tmp_two->right = (struct node *)malloc(sizeof(struct node)); 
      tmp_two = tmp_two->right;
      tmp_two->data = value;
      tmp_two->definition = word;
      tmp_two->left = tmp_two->right = NULL;
    }
  }

  return(p);
}

void print_preorder(struct node *p) {
  if(p != NULL) {
    printf("%d : %s\n", p->data, p->definition);
    print_preorder(p->left);
    print_preorder(p->right);
  }
}

Al momento sembra funzionare per le ints ma la parte descrizione stampa solo per l'ultimo inserito. Suppongo che abbia qualcosa a che fare con i puntatori sulla matrice char ma non ho avuto fortuna a farla funzionare. Tutte le idee o consigli?

È stato utile?

Soluzione

Il problema è che si sta utilizzando lo stesso buffer per la stringa. Si noti la tua struct è in possesso di un puntatore ad un char, e si sta passando stesso array char come tale puntatore ogni volta.

Quando si chiama scanf sul buffer, si stanno modificando i dati a cui punta, non il puntatore stesso.

Per risolvere questo problema, prima di assegnarlo verso una struttura, è possibile utilizzare strdup. Quindi le righe di codice diventerebbero

tmp_*->definition = strdup(word);

Tenete a mente che la matrice char restituito da strdup deve essere liberato una volta che hai finito con esso, altrimenti dovrete una perdita.

Altri suggerimenti

Si sta sempre facendo uno scanf nella definizione e quindi passaggio che alla vostra routine inserto che solo consente di risparmiare il puntatore def. Quindi, dal momento che tutte le voci puntano al buffer def, tutti rimandano a ciò che era l'ultima stringa è memorizzato in quel buffer.

È necessario copiare la stringa e inserire un puntatore alla copia nel nodo albero binario.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top