我試圖列印三角形中的元素(函式print_trie),但是在列印陳述句中沒有顯示任何東西。
#include <stdio.h>/span>
#include <stdlib.h>
#define CHAR_SIZE 26 //定義字符大小
struct Trie {
char data;
int isLeaf; //1 當節點是葉子節點時。
struct Trie* character[CHAR_SIZE];
};
struct Trie* getNewTrieNode() {
struct Trie* node =(struct Trie*)malloc(sizeof (struct Trie) )。
node->isLeaf = 0;
for (int i = 0; i < CHAR_SIZE; i ) {
node->character[i] = NULL;
}
return node。
}
void insert(struct Trie* head, char* str) {
struct Trie* curr = head; //從根節點開始
while (*str) {
//>如果路徑不存在,則創建一個新的節點。
if (curr->character[*str - 'a'] == NULL) {
curr->character[*str - 'a'] = getNewTrieNode();
}
//進入下一個節點。
curr = curr->字符[*str - 'a']。
//移到下一個字符。
str ;
}
curr->isLeaf = 1; //標記當前節點為葉子。
}
//這里是我無法列印trie內容的代碼。
void print_trie( struct Trie* root) {
if (!root)
return;
struct Trie* temp = root;
for (int i=0; i< CHAR_SIZE; i ) {
if(temp->character[i]! ='')
printf("%c"/span>,temp->character[i])。
print_trie(temp->character[i])。
}
}
int main() {
struct Trie* head = getNewTrieNode() 。
char word[10] 。
printf("
輸入要插入的單詞。")。)
scanf("%s"/span>,word)。
insert(head,word)。
printf("Print the nodes in the trie")。
print_trie(head)。
return 0;
}
uj5u.com熱心網友回復:
torstenvl已經正確地指出,printf("%c",temp->character[i]);不起作用,因為character[]/code>是一個指標陣列。由于該陣列的每個元素都對應于某個小寫字母,你可以很容易地檢索到該字符:
printf("%c"/span>, i 'a')。
當然,只要輸入的字串最多包括9個小寫字母,這整個復雜的存盤方法就能發揮作用。
uj5u.com熱心網友回復:
struct Trie* character[CHAR_SIZE] 。
這里character被宣告為一個指標陣列,而不是字符陣列
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319332.html
標籤:
