?? 字典樹實現源代碼.c
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 'z' - 'a' + 1
#define SLEN 11
typedef struct zidian {
struct zidian *next[MAX];
char s[SLEN];
int isword;
} *Link,node;
Link init(void) //創建一個新結點
{
Link root = (Link)malloc(sizeof(node));
int i;
for (i = 0; i < MAX; i++) root->next[i] = NULL;
root->isword = 0;
return root;
}
void insert(char path[], char s[],Link root) //插入一個單詞path[]表示插入路線 char表示插入單詞
{
Link t, p = root;
int i, j, n = strlen(path);
for (i = 0; i < n; i++)
{
if (p->next[path[i] - 'a'] == NULL)
{
t = (Link)malloc(sizeof(node));
for (j = 0; j < MAX; j++) t->next[j] = NULL;
t->isword = 0;
p->next[path[i] - 'a'] = t;
}
p = p->next[path[i] - 'a'];
}
p->isword = 1;
strcpy(p->s, s);
}
char *find(char path[], int delflag,Link root) //查找一個單詞 查到后作標記
{
Link p = root;
int i, n = strlen(path);
i = 0;
while (p && path[i]) p = p->next[path[i++] - 'a'];
if (p && p->isword) {
p->isword = delflag;
return p->s;
}
return NULL;
}
void del(Link root) //毀掉這棵樹
{
int i;
if (!root) return;
for (i = 0; i < MAX; i++)
if (root->next[i]) del(root->next[i]);
free(root->next[i]);
}
int main(void)
{
char line[256], s1[256], s2[256];
char *s;
Link root;
root = init();
while (gets(line)) {
if (sscanf(line, "%s %s", s1, s2) != 2) break;
insert(s2, s1,root); //插入單詞
}
while (scanf("%s", s1) == 1) {
s = find(s1, 1 ,root); ///找單詞
if (s == NULL) printf("eh\n");
else printf("%s\n", s);
}
return 0;
}
//////////////////////////////////////////////
字典樹存儲每個單詞到樹中 然后在樹中找單詞
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -