cv7
This commit is contained in:
parent
491beb49ed
commit
896caa5d3e
@ -1,33 +0,0 @@
|
||||
#ifndef BINARY_SEARCH_H
|
||||
#define BINARY_SEARCH_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const int *binary_search(int value, const int *arr, size_t length) {
|
||||
if (length == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t left = 0;
|
||||
size_t right = length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
size_t middle = left + (right - left) / 2;
|
||||
|
||||
if (arr[middle] == value) {
|
||||
return &arr[middle];
|
||||
} else if (arr[middle] > value) {
|
||||
if (middle == 0) {
|
||||
break;
|
||||
}
|
||||
right = middle - 1;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -14,19 +14,15 @@ struct tree* read_tree(int* counter) {
|
||||
char buffer[SIZE];
|
||||
memset(buffer, 0, SIZE);
|
||||
char* r = fgets(buffer, SIZE, stdin);
|
||||
if(r == NULL){
|
||||
printf("Chybna databaza\n");
|
||||
return 0;
|
||||
}
|
||||
if (buffer[0] == '\n') {
|
||||
return NULL;
|
||||
}
|
||||
struct tree* tree = calloc(1, sizeof(struct tree));
|
||||
if(tree == NULL){
|
||||
printf("Chybna databaza\n");
|
||||
exit(0);
|
||||
}
|
||||
tree->value = malloc(strlen(buffer) + 1);
|
||||
if(tree->value == NULL){
|
||||
printf("Chybna databaza\n");
|
||||
exit(0);
|
||||
}
|
||||
strcpy(tree->value, buffer);
|
||||
if (buffer[0] == '*') {
|
||||
(*counter)++;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user