Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Printing the words contained in a file named file.txt
in alphabetical order and also their frequencies in to another file "wordcount.txt".
More...
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Data Structures | |
struct | Node |
Node, the basic data structure of the tree. More... | |
Functions | |
void | endProgramAbruptly (char *errorMessage) |
Ends program due to an error. | |
void | freeTreeMemory (struct Node *node) |
Frees memory when program is terminating. | |
char * | getPointerToWord (char *word) |
Stores word in memory. | |
void | closeFile (FILE *file) |
Closes the file after reading or writing. | |
struct Node * | allocateMemoryForNode () |
Reserves memory for new node. | |
void | writeContentOfTreeToFile (struct Node *node, FILE *file) |
Writes contents of tree to another file alphabetically. | |
struct Node * | addWordToTree (char *word, struct Node *currentNode) |
Adds word (node) to the correct position in tree. | |
struct Node * | readWordsInFileToTree (FILE *file, struct Node *root) |
Reads words from file to tree. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
Printing the words contained in a file named file.txt
in alphabetical order and also their frequencies in to another file "wordcount.txt".
Given a file (file.txt
) containing words (like a publication or a novel), where words are separated by a space, newline, or underscore. This program prints (writes or outputs) to another file (wordcount.txt
), the individual words contained in 'file.txt' with their frequencies (number of occurrences) each on a newline and in alphabetical order. This program uses the binary tree data structure to accomplish this task.
Adds word (node) to the correct position in tree.
word | word to be inserted in to the tree |
currentNode | node which is being compared |
< holds compare state
struct Node * allocateMemoryForNode | ( | ) |
Reserves memory for new node.
NULL
if memory is NOT reserved < pointer to the node
void closeFile | ( | FILE * | file | ) |
Closes the file after reading or writing.
file | pointer to the file to be closed |
void endProgramAbruptly | ( | char * | errorMessage | ) |
Ends program due to an error.
errorMessage | the error message to be printed |
void freeTreeMemory | ( | struct Node * | node | ) |
Frees memory when program is terminating.
node | pointer to current node |
char * getPointerToWord | ( | char * | word | ) |
Stores word in memory.
word | word to be stored in memory |
NULL
if the word is NOT stored < pointer to string
int main | ( | void | ) |
Reads words from file to tree.
file | file to be read from |
root | root node of tree |
< pointer to the input string
< temp storage of characters
< bool to mark the end of a word
< position in inputString to place the inputChar
|
static |
Self-test implementations.
< pointer to the root node
< pointer to the file
void writeContentOfTreeToFile | ( | struct Node * | node, |
FILE * | file | ||
) |
Writes contents of tree to another file alphabetically.
node | pointer to current node |
file | pointer to file |
< for word numbering in the write file