Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct cvor_s {
  5.     struct cvor_s* left;
  6.     struct cvor_s* right;  
  7.     char c;
  8.     int value;
  9. } cvor;
  10.  
  11. cvor *insert(cvor *root,char c) {
  12.     cvor *novi;
  13.     int korijenJeNov = 0;
  14.     if (root == NULL) {
  15.         korijenJeNov = 1;
  16.         root = (cvor *) malloc(sizeof(cvor));
  17.         root->c = c;
  18.         root->value = 1;
  19.         root->right = NULL;
  20.         root->left = NULL;
  21.     }
  22.     if (root->c == c && korijenJeNov == 0) {
  23.         root->value += 1;
  24.     }
  25.     if (root->c < c) {
  26.         root->right = insert(root->right,c);
  27.     }
  28.     if (root->c > c) {
  29.         root->left = insert(root->left,c);
  30.     }
  31.     return root;
  32. }
  33.  
  34. void inorder(cvor *root) {
  35.     if (root == NULL) return;
  36.     inorder(root->left);
  37.     printf("Broj ponavljanja znaka \'%c\': %d.\n",root->c,root->value);
  38.     inorder(root->right);
  39. }
  40.  
  41. int main (void) {
  42.     FILE *file;
  43.     char znak;
  44.     cvor *root = NULL;
  45.     file = fopen("mirko.txt","r");
  46.     if (file == NULL) {
  47.         printf("lazanje");
  48.     }
  49.     while ((znak = fgetc(file)) != EOF) {
  50.         root = insert(root,znak);
  51.     }
  52.     inorder(root);
  53.     fclose(file);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement