Originally Posted by
thebesthere231
of so i changes all u write up of quaote and the argc . what i need to change too that i missed ?
and still i have problem like i free more that i have ..
double free or corruption (!prev)
Aborted (core dumped)
I don't know. I didn't get any double frees. Did you change anything else in the meantime? I'm still not convinced that it's creating a tree structure like you want it to but that's something else entirely...
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define NUM_LETTERS 26
typedef struct node {
char letter;
long unsigned int count;
struct node* children[NUM_LETTERS];
} node;
int isCharLeagal(int c);
node* newNode(void);
void buildTree(node* head);
node* addword(node* head, node* pointNode, int character);
int bigWordLength(node* n, int counter);
void freeLevelsOnTree(node* head);
void printWord(node* head,char strWord[], int levelTree);
void printWordReverse(node* head,char strWord[], int levelTree);
FILE *fp; /*** Delete this; I had it just for testing ***/
int main(int argc, char* argv[])
{
fp = fopen("input.txt", "r"); /*** Delete this; I had it just for testing ***/
if (!fp) { /*** Delete this; I had it just for testing ***/
perror("fopen"); /*** Delete this; I had it just for testing ***/
} /*** Delete this; I had it just for testing ***/
char* r = "r";
node* head = newNode();
buildTree(head);
int biggestWordLength=bigWordLength(head, 0);
char* word = (char*)malloc(biggestWordLength + 1);
if( ((argc>1) && strcmp(argv[1],r)==0) ) {
printWordReverse(head,word,0);
} else {
printWord(head,word,0);
}
free(word);
freeLevelsOnTree(head);
fclose(fp); /*** Delete this; I had it just for testing ***/
return 0;
}
node* newNode(void)
{
node* n = NULL;
n = (node*)malloc(sizeof(node));
if (n == NULL) {
printf("error-malloc not allocate memory");
return NULL;
}
n->count = 0;
int i;
for (i = 0; i < NUM_LETTERS; i++) {
n->children[i] = NULL;
}
return n;
}
void buildTree(node* head)
{
int character;
node* pointNode = head;
if (!pointNode) {
printf("No point node\n");
return;
}
do {
character = fgetc(fp); //getchar(); /*** Change back to character = getchar() ***/
if(character == EOF) {
if (pointNode)
pointNode->count++;
return;
}
character = tolower(character); //Turns a big letter into a small letter lib <ctype.h>
// if(!( character>='a' && character<='z') )
// {continue;}
if (!isCharLeagal(character)) continue;
if (character == '\n' || character == ' ' || character == '\t') {
if ( (pointNode != NULL) && (pointNode != head) ) {
pointNode->count++;
pointNode= NULL;
}
continue;
}
if(pointNode ==NULL) {
pointNode = head;
}
pointNode = addword(head, pointNode, character);
} while(character!=EOF);
}
node* addword(node* head, node* pointNode,int character)
{
int i = (((int)character)-((int)'a')) ; //calculate the place of the index
if( (pointNode->children[i]) == NULL ) {
pointNode->children[i] = newNode();
pointNode->children[i]->letter = character;
}
pointNode = pointNode->children[i];
return pointNode;
}
void freeLevelsOnTree(node* head)
{
node* nPointer = head;
for (size_t i=0; i<NUM_LETTERS; i++) {
if(nPointer->children[i] !=NULL) {
freeLevelsOnTree(nPointer->children[i]);
}
}
free(nPointer);
}
int bigWordLength(node* n, int counter)
{
int i;
int length = counter;
for(i = 0; i < NUM_LETTERS; i++) {
if(n->children[i]!=NULL) {
int length2 = bigWordLength(n->children[i], counter + 1);
if(length2 > length) {
length = length2;
}
}
}
return length;
}
void printWord(node* head, char strWord[], int levelTree)
{
node* nPointer = head;
if (nPointer == NULL) {
return;
}
if (nPointer->count > 0) {
strWord[levelTree] = '\0';
printf("%s\t %ld \n",strWord,nPointer->count);
}
int i;
for(i = 0; i < NUM_LETTERS; i++) {
if(nPointer->children[i] != NULL) {
strWord[levelTree]=nPointer->children[i]->letter;
printWord(nPointer->children[i], strWord,levelTree + 1);
}
}
}
void printWordReverse(node* head, char strWord[], int levelTree)
{
node* nPointer2 = head;
if(nPointer2 == NULL) {
return;
}
if(nPointer2->count > 0) {
strWord[levelTree] = '\0';
printf("%s\t %ld \n",strWord,nPointer2->count);
}
int i;
for(i = NUM_LETTERS-1; i >= 0; i--) {
if(nPointer2->children[i]!= NULL) {
strWord[levelTree] = nPointer2->children[i]->letter;
printWordReverse(nPointer2->children[i], strWord, levelTree + 1);
}
}
}
int isCharLeagal(int c)
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '\n' || c == ' ' || c == '\t')) {
return 1;
}
return 0;
}
Code:
$ valgrind --tool=memcheck --leak-check=full ./test
==1850== Memcheck, a memory error detector
==1850== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1850== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==1850== Command: ./test
==1850==
another 1
hello 1
is 1
sentence 1
this 1
world 1
==1850==
==1850== HEAP SUMMARY:
==1850== in use at exit: 0 bytes in 0 blocks
==1850== total heap usage: 37 allocs, 37 frees, 85,553 bytes allocated
==1850==
==1850== All heap blocks were freed -- no leaks are possible