the input file is as follows
3
Drop_Box 10 need to drop box with secure box emergency drop urgent
Carpet_maintenance 7 grease remove immediately cover floor laundry man
Smoke_alarm 8 please floor scene urgently scene battery scene box
there are only 3 lines the top number indicates how many chapters there are and then each line contains the title of the chapter (which goes into the linked list) and the number of words to add to the tree and then the words follow
Code:
CODE
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tree_node //Declaration of binary tree
{
char word[20];
int w_freq;
struct tree_node *left;
struct tree_node *right;
};
struct list_node //Declaration of linked list
{
char title[50];
int num_words;
int height;
struct list_node *next;
struct tree_node *tree_link;
};
struct list_node *list;
struct tree_node *tree;
FILE *fp;
struct list_node *add_chapter(struct list_node *list);
struct tree_node *add_words(struct tree_node* tree, char[]);
void print_tree(struct tree_node *tree);
void print_list(struct list_node *list);
int main(void)
{
char file_name[50];
int num_of_words;
int num_of_chapters;
char word[20];
printf("Enter the name of the file: "); //Prompts the user to enter the name of the file
scanf("%s", &file_name);
fp = fopen(file_name, "r");
if(fp == 0)
{
printf ("\nCould not open file!\n");
return 0;
}
fscanf(fp, "%d", &num_of_chapters);
while(num_of_chapters > 0)
{
list = add_chapter(list);
fscanf(fp,"%d", &num_of_words);
while(num_of_words > 0)
{
fscanf(fp, "%s", word);
tree = add_words(list -> tree_link, word);
num_of_words--;
}
num_of_chapters--;
}
print_list(list);
}
struct list_node *add_chapter(struct list_node *list)
{
struct list_node *p_new = NULL;
struct list_node *p_cur = NULL;
struct list_node *p_pre = NULL;
char title[50];
p_new = (struct list_node *)malloc(sizeof(struct list_node));
fscanf(fp, "%s", title);
strcpy(p_new -> title, title);
p_new -> next = NULL;
p_cur = list;
if(list == NULL)
{
return p_new;
}
if(strcmp(p_cur -> title, title) > 0)
{
p_new -> next = p_cur;
p_cur = p_new;
return p_cur;
}
while(p_cur -> next != NULL)
{
p_pre = p_cur;
p_cur= p_cur -> next;
if(strcmp(p_cur -> title, title) > 0)
{
p_new -> next = p_pre -> next;
p_pre -> next = p_new;
return list;
}
}
p_cur -> next = p_new;
return list;
}
struct tree_node *add_words(struct tree_node* tree,char word[])
{
struct tree_node *p_new = NULL;
p_new = tree;
if(p_new == NULL)
{
p_new = (struct tree_node *)malloc(sizeof(struct tree_node));
strcpy(p_new -> word, word);
p_new -> left = NULL;
p_new -> right = NULL;
}
else if(strcmp(p_new -> word, word) > 0)
p_new -> left = add_words(p_new -> left, word);
else
p_new -> right = add_words(p_new -> right, word);
return p_new;
}
void print_list(struct list_node *list)
{
struct list_node *p_cur = NULL;
p_cur = list;
while(p_cur != NULL)
{
printf("%s", p_cur -> title);
p_cur = p_cur -> next;
print_tree(tree);
}
}
void print_tree(struct tree_node *tree)
{
if (tree !=NULL)
{
printf(" %s", tree -> word);
print_tree(tree -> left);
print_tree(tree -> right);
}
}
#include<string.h>
#include<stdlib.h>
struct tree_node //Declaration of binary tree
{
char word[20];
int w_freq;
struct tree_node *left;
struct tree_node *right;
};
struct list_node //Declaration of linked list
{
char title[50];
int num_words;
int height;
struct list_node *next;
struct tree_node *tree_link;
};
struct list_node *list;
struct tree_node *tree;
FILE *fp;
struct list_node *add_chapter(struct list_node *list);
struct tree_node *add_words(struct tree_node* tree, char[]);
void print_tree(struct tree_node *tree);
void print_list(struct list_node *list);
int main(void)
{
char file_name[50];
int num_of_words;
int num_of_chapters;
char word[20];
printf("Enter the name of the file: "); //Prompts the user to enter the name of the file
scanf("%s", &file_name);
fp = fopen(file_name, "r");
if(fp == 0)
{
printf ("\nCould not open file!\n");
return 0;
}
fscanf(fp, "%d", &num_of_chapters);
while(num_of_chapters > 0)
{
list = add_chapter(list);
fscanf(fp,"%d", &num_of_words);
while(num_of_words > 0)
{
fscanf(fp, "%s", word);
tree = add_words(list -> tree_link, word);
num_of_words--;
}
num_of_chapters--;
}
print_list(list);
}
struct list_node *add_chapter(struct list_node *list)
{
struct list_node *p_new = NULL;
struct list_node *p_cur = NULL;
struct list_node *p_pre = NULL;
char title[50];
p_new = (struct list_node *)malloc(sizeof(struct list_node));
fscanf(fp, "%s", title);
strcpy(p_new -> title, title);
p_new -> next = NULL;
p_cur = list;
if(list == NULL)
{
return p_new;
}
if(strcmp(p_cur -> title, title) > 0)
{
p_new -> next = p_cur;
p_cur = p_new;
return p_cur;
}
while(p_cur -> next != NULL)
{
p_pre = p_cur;
p_cur= p_cur -> next;
if(strcmp(p_cur -> title, title) > 0)
{
p_new -> next = p_pre -> next;
p_pre -> next = p_new;
return list;
}
}
p_cur -> next = p_new;
return list;
}
struct tree_node *add_words(struct tree_node* tree,char word[])
{
struct tree_node *p_new = NULL;
p_new = tree;
if(p_new == NULL)
{
p_new = (struct tree_node *)malloc(sizeof(struct tree_node));
strcpy(p_new -> word, word);
p_new -> left = NULL;
p_new -> right = NULL;
}
else if(strcmp(p_new -> word, word) > 0)
p_new -> left = add_words(p_new -> left, word);
else
p_new -> right = add_words(p_new -> right, word);
return p_new;
}
void print_list(struct list_node *list)
{
struct list_node *p_cur = NULL;
p_cur = list;
while(p_cur != NULL)
{
printf("%s", p_cur -> title);
p_cur = p_cur -> next;
print_tree(tree);
}
}
void print_tree(struct tree_node *tree)
{
if (tree !=NULL)
{
printf(" %s", tree -> word);
print_tree(tree -> left);
print_tree(tree -> right);
}
}
my problem is it's crashing giving me a status access violation and I'm not sure why. I'm having trouble linking each node of the linked list to the binary trees that correspond to them.
if anyone has any suggestions I'd appreciate it

