Create and print a tree in C -


i trying create program creates tree words in file , same words sorted. here code have:

#include <stdio.h> #include <stdlib.h> #include <string.h>  #define size    30   typedef struct node *tree; typedef struct node {    tree left;    tree right;    struct dictionary {       char *word;       char *sortedword;    } value; } node;   void swap(char *a,char *b){         char tmp;         tmp = *a;         *a = *b;         *b = tmp; }   char * bubble_sort(char *word){    char *ptr = word;    int n = strlen(word);          int i,j;          for(i=0;i<n-1;i++)                 for(j=0;j<n-i-1;j++)                         if(*(ptr+j)>*(ptr+j+1))                                 swap((ptr+j),(ptr+j+1));    return word; }  char *removenewlines(char *word){    char *newstring = malloc(size * sizeof(char));    char ch;    int currentletter = 0;    int len = strlen(word);     while (currentletter < len){       ch = word[currentletter];       if (ch != '\n'){          newstring[currentletter] = ch;       }       else {          newstring[currentletter] = '\0';       }       currentletter++;    }    return newstring; }   void createtree(tree memory){ //   file *fp = fopen("words.txt","r"); //   file *fp = fopen("words_no_duplicates1.txt","r");    file *fp = fopen("words_no_duplicates2.txt","r");    char *word = (char *)malloc(size * sizeof(char));     if(fp == null)    {       perror("error opening file");       exit(1);    }     // create top node of tree    node seednode;    memory = &seednode;    // store seed in tree    memory->left = null;    memory->right = null;     fgets (word,size,fp);     memory->value.word = (char *)malloc(size * sizeof(char));    memory->value.word = strcpy(memory->value.word,removenewlines(word)); //   printf("%s\n",memory->value.word);     memory->value.sortedword = (char *)malloc(size * sizeof(char));    memory->value.sortedword = strcpy(memory->value.sortedword,bubble_sort(removenewlines(word))); //   printf("%s\n",memory->value.sortedword); //   printf("\n");     while ( fgets (word,size,fp) != null){       memory->left = null;       memory->right = null;        memory->value.word = (char *)malloc(size * sizeof(char));       memory->value.word = strcpy(memory->value.word,removenewlines(word));         memory->value.sortedword = (char *)malloc(size * sizeof(char));       memory->value.sortedword = strcpy(memory->value.sortedword,bubble_sort(removenewlines(word)));   //      printf("%s\t %s\n",memory->value.word,memory->value.sortedword); //      printf("%s\n",memory->value.word); //      printf("%s\n",memory->value.sortedword); //      printf("\n");    }     fclose(fp); }  void printtree(tree memory){    if (memory != null){       printtree(memory->left);       printf("%s",memory->value.word);       printf("%s",memory->value.sortedword);       printf("\n");       printtree(memory->right);    } }   int main(){    node seednode;    tree memory = &seednode;    createtree(memory);    printtree(memory);     return 0; } 

if uncomment print statements in createtree function, looks ok , prints want. if try use printtree function ends "segmentation fault". go wrong?

you can test random file contains 1 word per line.

#

after joachim pileborg reply, changed code follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>  #define size    30  typedef struct node *tree; typedef struct node {    tree left;    tree right;    struct dictionary {       char *word;       char *sortedword;    } value; } node;  node seednode; tree memory = &seednode;   void swap(char *a,char *b){         char tmp;         tmp = *a;         *a = *b;         *b = tmp; }   char * bubble_sort(char *word){    char *ptr = word;    int n = strlen(word);          int i,j;          for(i=0;i<n-1;i++)                 for(j=0;j<n-i-1;j++)                         if(*(ptr+j)>*(ptr+j+1))                                 swap((ptr+j),(ptr+j+1));    return word; }  char *removenewlines(char *word){    char *newstring = malloc(size * sizeof(char));    char ch;    int currentletter = 0;    int len = strlen(word);     while (currentletter < len){       ch = word[currentletter];       if (ch != '\n'){          newstring[currentletter] = ch;       }       else {          newstring[currentletter] = '\0';       }       currentletter++;    }    return newstring; }   tree * createtree(tree *memory){ //   file *fp = fopen("words.txt","r");    file *fp = fopen("words_no_duplicates1.txt","r");    char *word = (char *)malloc(size * sizeof(char));     if(fp == null)    {       perror("error opening file");       exit(1);    }     // create top node of tree    //node seednode;     // store seed in tree    (*memory)->left = null;    (*memory)->right = null;     fgets (word,size,fp);     (*memory)->value.word = (char *)malloc(size * sizeof(char));    (*memory)->value.word = strcpy((*memory)->value.word,removenewlines(word)); //   printf("%s\n",memory->value.word);     (*memory)->value.sortedword = (char *)malloc(size * sizeof(char));    (*memory)->value.sortedword = strcpy((*memory)->value.sortedword,bubble_sort(removenewlines(word))); //   printf("%s\n",memory->value.sortedword); //   printf("\n");     while ( fgets (word,size,fp) != null){       (*memory)->left = null;       (*memory)->right = null;        (*memory)->value.word = (char *)malloc(size * sizeof(char));       (*memory)->value.word = strcpy((*memory)->value.word,removenewlines(word));         (*memory)->value.sortedword = (char *)malloc(size * sizeof(char));       (*memory)->value.sortedword = strcpy((*memory)->value.sortedword,bubble_sort(removenewlines(word)));   //      printf("%s\t %s\n",memory->value.word,memory->value.sortedword); //      printf("%s\n",memory->value.word); //      printf("%s\n",memory->value.sortedword); //      printf("\n");    }     fclose(fp);     return memory; }   void printtree(tree memory){    if (memory != null){       printtree(memory->left);       printf("%s\n",memory->value.word);       printf("%s\n",memory->value.sortedword);       printf("\n");       printtree(memory->right);    } }   int main(){     createtree(&memory);    printtree(memory);     return 0; } 

although createtree function looks strange me, after executing code have printed last word, , sorted, file words_no_duplicates1.txt (these values memory->value.word, , memory->value.sortedword) trees memory->left , memory->right remains empty. can please help?

there many problems code, point out couple stood out me.

in createtree function have these 2 lines

node seednode; memory = &seednode; 

those troublesome because first argument memory passed by value meaning value passed when calling function copied , have inside function local variable copy of value. changing variable not change original variable used when calling function. can solve either returning pointer or emulating passing by reference.

the second problem assignment make point local variable. local variables go out of scope once function returns, , not exist more. keeping , dereferencing pointer (non-existent) variable lead undefined behavior, , crashes. can solved either not doing assignment @ all, , rely on pointer pass argument, or dynamically allocating memory node.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -