summaryrefslogtreecommitdiff
path: root/word.c
blob: e6570c781ceefe3ac8d85aa8ca996da5f9d0ea91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* This file is a part of WordExtract project
 *
 * Copyright (C) 2009 Borisov Alexandr
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word.h"
#include "dict.h"

inline char *wordcpy(char *word);
inline void free_word_record(Word *record);

int to_list(char *word)
{
 if (!is_in_dict(word, dict)) {
	words = add_word_record(words, word);
 }
 return 0;
}

Word *add_word_record(Word *root, char *word)
{
 int cond;

 if (root == NULL) {
	root = malloc(sizeof(Word));
	root->word = wordcpy(word);
	root->lsibl = root->rsibl = NULL;
 }
 else {
	cond = strcmp(word, root->word);
	if (cond > 0)
		root->rsibl = add_word_record(root->rsibl, word);
	else if (cond < 0)
		root->lsibl = add_word_record(root->lsibl, word);
 }
 return root;
}

void free_words(Word *root)
{
 if (root->lsibl != NULL)
	free_words(root->lsibl);
 if (root->rsibl != NULL)
	free_words(root->rsibl);
 free_word_record(root);
}

void print_words(Word *root)
{
 if (root != NULL) {
	print_words(root->lsibl);
	print_words(root->rsibl);
	printf("%s\n", root->word);
 }
}

inline void free_word_record(Word *record)
{
 free(record->word);
 free(record);
}

inline char *wordcpy(char *word)
{
 char *p;
 p = malloc(strlen(word)+1);
 if (p != NULL)
	strcpy(p, word);
 return p;
}