Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- typedef struct node_t {
- char * word;
- int count;
- struct node_t * left;
- struct node_t * right;
- } Node;
- void addword(char * word, Node * node);
- Node * node_new(char * word);
- void printnodes(Node * node);
- int main()
- {
- Node * head = node_new("ala");
- addword("ala", head);
- addword("ma", head);
- addword("kota", head);
- addword("sierotka", head);
- addword("ala", head);
- addword("ma", head);
- addword("rysia", head);
- printnodes(head);
- return 0;
- }
- void addword(char * word, Node * node)
- {
- if(strcmp(node->word, word) == 0)
- {
- node->count++;
- }
- else if(strcmp(node->word, word) > 0)
- {
- if(node->right == NULL)
- node->right = node_new(word);
- else
- addword(word, node->right);
- }
- else if(strcmp(node->word, word) < 0)
- {
- if(node->left == NULL)
- node->left = node_new(word);
- else
- addword(word, node->left);
- }
- }
- Node * node_new(char * word)
- {
- Node * node = malloc(sizeof(Node));
- node->word = malloc(strlen(word) + 1);
- strcpy(node->word, word);
- node->count = 1;
- node->left = NULL;
- node->right = NULL;
- assert(node != NULL);
- return node;
- }
- void printnodes(Node * node)
- {
- if(node != NULL)
- {
- printf("%-10s %10d\n", node->word, node->count);
- printnodes(node->left);
- printnodes(node->right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment