new file: lists/double_linked_list.c
new file: lists/ejudge/joseph.c new file: lists/ejudge/joseph_full.c new file: lists/ejudge/list3.c new file: lists/ejudge/list3_full.c new file: lists/list.c new file: trees/tree1.c new file: trees/tree2.c new file: trees/tree2_full.c new file: trees/tree4_full.c
This commit is contained in:
		
							parent
							
								
									6c827dcff8
								
							
						
					
					
						commit
						6161b74ffc
					
				
					 10 changed files with 1280 additions and 0 deletions
				
			
		
							
								
								
									
										208
									
								
								list_and_trees/lists/double_linked_list.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										208
									
								
								list_and_trees/lists/double_linked_list.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,208 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| struct Node { | ||||
| 	struct Node * next; | ||||
| 	struct Node * prev; | ||||
| 	Data data; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| void list_init(struct Node * list); | ||||
| void list_insert(struct Node * list, struct Node * t); | ||||
| void list_insert_before(struct Node * list, struct Node * t); | ||||
| void list_remove(struct Node * t); | ||||
| struct Node * list_push_front(struct Node * list, Data d); | ||||
| struct Node * list_push_back(struct Node * list, Data d); | ||||
| Data list_pop_front(struct Node * list); | ||||
| Data list_pop_back(struct Node * list); | ||||
| Data list_delete(struct Node * t); | ||||
| void list_print (struct Node * list); | ||||
| Data list_sum (struct Node * list); | ||||
| int list_is_empty(struct Node * list); | ||||
| int list_clear(struct Node * list); | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param); | ||||
| void print_it(Data d, void * parameter); | ||||
| void sum(Data a, void * s); | ||||
| 
 | ||||
| void list_init(struct Node * list) { | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	list->data = -1; | ||||
| } | ||||
| 
 | ||||
| void list_print (struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		printf("Empty list\n"); | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next != list) { | ||||
| 			printf("%d ", ptr->data); | ||||
| 			//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 			ptr = ptr->next; | ||||
| 		} | ||||
| 		printf("%d\n", ptr->data); | ||||
| 		//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 	} | ||||
| }  | ||||
| 
 | ||||
| void list_insert(struct Node * list, struct Node * t) { | ||||
| 	t->next = list->next; | ||||
| 	t->prev = list; | ||||
| 	if (t->next != list) { | ||||
| 		t->next->prev = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->next = list; | ||||
| 		list->prev = t; | ||||
| 	} | ||||
| 	list->next = t; | ||||
| } | ||||
| 
 | ||||
| void list_insert_before(struct Node * list, struct Node * t) { | ||||
| 	t->next = list; | ||||
| 	t->prev = list->prev; | ||||
| 	if (t->prev != list) { | ||||
| 		t->prev->next = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->prev = list; | ||||
| 		list->next = t; | ||||
| 	} | ||||
| 	list->prev = t; | ||||
| } | ||||
| 
 | ||||
| void list_remove(struct Node * t) { | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_front(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = ptr; | ||||
| 	oth->prev = list; | ||||
| 	list->next = oth; | ||||
| 	ptr->prev = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_back(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = list; | ||||
| 	oth->prev = ptr; | ||||
| 	list->prev = oth; | ||||
| 	ptr->next = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| int list_is_empty(struct Node * list) { | ||||
| 	if (list == list->next) { | ||||
| 		return 1; | ||||
| 	} | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| int list_clear(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	 | ||||
| 	struct Node *n; | ||||
| 	while (ptr->next != list) { | ||||
| 		n = ptr->next; | ||||
| 		free(ptr); | ||||
| 		ptr = n; | ||||
| 	} | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_front(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	Data val = ptr->data; | ||||
| 	list->next = ptr->next; | ||||
| 	ptr->next->prev = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_back(struct Node * list) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	Data val = ptr->data; | ||||
| 	list->prev = ptr->prev; | ||||
| 	ptr->prev->next = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_delete(struct Node * t) { | ||||
| 	if (t == t->next) { | ||||
| 		return -1; | ||||
| 	} | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| 	Data val = t->data; | ||||
| 	free(t); | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	while (ptr->next != list) { | ||||
| 		func(ptr->data, param); | ||||
| 		ptr = ptr->next; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void sum(Data a, void * s) { | ||||
| 	*(Data *)s += a; | ||||
| } | ||||
| 
 | ||||
| void print_it(Data d, void * param) { | ||||
| 	FILE * fd = param; | ||||
| 	fprintf(fd, "%d ", d); | ||||
| } | ||||
| 
 | ||||
| Data list_sum (struct Node * list) { | ||||
| 	int s = 0; | ||||
| 	list_for_each(list, sum, &s); | ||||
| 	return s; | ||||
| } | ||||
| 
 | ||||
| int list_size(struct Node *list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		return 0; | ||||
| 	} | ||||
| 	int len = 1; | ||||
| 
 | ||||
| 	while (ptr->next != list) { | ||||
| 		ptr = ptr->next; | ||||
| 		++len; | ||||
| 	} | ||||
| 	return len; | ||||
| } | ||||
| /*int main() {
 | ||||
| struct Node x[10]; | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 
 | ||||
| 	int i; | ||||
| 	list_init(a); | ||||
| 
 | ||||
| 	 | ||||
| 	for(i=0; i<10; i++) { | ||||
| 		x[i].data = i; | ||||
| 		list_insert(a, &x[i]); | ||||
| 	} | ||||
| 	list_print(a); | ||||
| 	//while(list_delete(a->next));
 | ||||
| 	list_clear(a); | ||||
| 	free(a); | ||||
| }*/ | ||||
							
								
								
									
										37
									
								
								list_and_trees/lists/ejudge/joseph.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								list_and_trees/lists/ejudge/joseph.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | |||
| #include "../double_linked_list.c" | ||||
| 
 | ||||
| int main() { | ||||
| 	int i; | ||||
| 	int N ,M; | ||||
| 	scanf("%d", &N); | ||||
| 	scanf("%d", &M); | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 	struct Node * res = malloc(sizeof(struct Node)); | ||||
| 	list_init(a); | ||||
| 	list_init(res); | ||||
| 	 | ||||
| 	for(i=1; i<=N; i++) { | ||||
| 		list_push_back(a, i); | ||||
| 	} | ||||
| 	struct Node *ptr = a->next; | ||||
| 	struct Node *next = NULL; | ||||
| 
 | ||||
| 	while (ptr->next != ptr) { | ||||
| 		for (i = 0; i < M-1; ++i) { | ||||
| 			ptr = ptr->next; | ||||
| 			if (ptr == a) { | ||||
| 				ptr = ptr->next; | ||||
| 			} | ||||
| 		} | ||||
| 		if (list_size(a) <= 3) { | ||||
| 			list_push_back(res, ptr->data); | ||||
| 		} | ||||
| 		next = ptr->next; | ||||
| 		list_delete(ptr); | ||||
| 		ptr = next; | ||||
| 	} | ||||
| 	list_print(res); | ||||
| 	while(list_delete(a->next) != -1); | ||||
| 	while(list_delete(res->next) != -1); | ||||
| 	free(a); free(res); | ||||
| } | ||||
							
								
								
									
										258
									
								
								list_and_trees/lists/ejudge/joseph_full.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										258
									
								
								list_and_trees/lists/ejudge/joseph_full.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,258 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| struct Node { | ||||
| 	struct Node * next; | ||||
| 	struct Node * prev; | ||||
| 	Data data; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| void list_init(struct Node * list); | ||||
| void list_insert(struct Node * list, struct Node * t); | ||||
| void list_insert_before(struct Node * list, struct Node * t); | ||||
| void list_remove(struct Node * t); | ||||
| struct Node * list_push_front(struct Node * list, Data d); | ||||
| struct Node * list_push_back(struct Node * list, Data d); | ||||
| Data list_pop_front(struct Node * list); | ||||
| Data list_pop_back(struct Node * list); | ||||
| Data list_delete(struct Node * t); | ||||
| void list_print (struct Node * list); | ||||
| Data list_sum (struct Node * list); | ||||
| int list_is_empty(struct Node * list); | ||||
| int list_clear(struct Node * list); | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param); | ||||
| void print_it(Data d, void * parameter); | ||||
| void sum(Data a, void * s); | ||||
| 
 | ||||
| void list_init(struct Node * list) { | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	list->data = -1; | ||||
| } | ||||
| 
 | ||||
| void list_print (struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		printf("Empty list\n"); | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next != list) { | ||||
| 			printf("%d ", ptr->data); | ||||
| 			//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 			ptr = ptr->next; | ||||
| 		} | ||||
| 		printf("%d\n", ptr->data); | ||||
| 		//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 	} | ||||
| }  | ||||
| 
 | ||||
| void list_insert(struct Node * list, struct Node * t) { | ||||
| 	t->next = list->next; | ||||
| 	t->prev = list; | ||||
| 	if (t->next != list) { | ||||
| 		t->next->prev = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->next = list; | ||||
| 		list->prev = t; | ||||
| 	} | ||||
| 	list->next = t; | ||||
| } | ||||
| 
 | ||||
| void list_insert_before(struct Node * list, struct Node * t) { | ||||
| 	t->next = list; | ||||
| 	t->prev = list->prev; | ||||
| 	if (t->prev != list) { | ||||
| 		t->prev->next = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->prev = list; | ||||
| 		list->next = t; | ||||
| 	} | ||||
| 	list->prev = t; | ||||
| } | ||||
| 
 | ||||
| void list_remove(struct Node * t) { | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_front(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = ptr; | ||||
| 	oth->prev = list; | ||||
| 	list->next = oth; | ||||
| 	ptr->prev = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_back(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = list; | ||||
| 	oth->prev = ptr; | ||||
| 	list->prev = oth; | ||||
| 	ptr->next = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| int list_is_empty(struct Node * list) { | ||||
| 	if (list == list->next) { | ||||
| 		return 1; | ||||
| 	} | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| int list_clear(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	 | ||||
| 	struct Node *n; | ||||
| 	while (ptr->next != list) { | ||||
| 		n = ptr->next; | ||||
| 		free(ptr); | ||||
| 		ptr = n; | ||||
| 	} | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_front(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	Data val = ptr->data; | ||||
| 	list->next = ptr->next; | ||||
| 	ptr->next->prev = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_back(struct Node * list) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	Data val = ptr->data; | ||||
| 	list->prev = ptr->prev; | ||||
| 	ptr->prev->next = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_delete(struct Node * t) { | ||||
| 	if (t == t->next) { | ||||
| 		return -1; | ||||
| 	} | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| 	Data val = t->data; | ||||
| 	free(t); | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	while (ptr->next != list) { | ||||
| 		func(ptr->data, param); | ||||
| 		ptr = ptr->next; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void sum(Data a, void * s) { | ||||
| 	*(Data *)s += a; | ||||
| } | ||||
| 
 | ||||
| void print_it(Data d, void * param) { | ||||
| 	FILE * fd = param; | ||||
| 	fprintf(fd, "%d ", d); | ||||
| } | ||||
| 
 | ||||
| Data list_sum (struct Node * list) { | ||||
| 	int s = 0; | ||||
| 	list_for_each(list, sum, &s); | ||||
| 	return s; | ||||
| } | ||||
| 
 | ||||
| int list_size(struct Node *list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		return 0; | ||||
| 	} | ||||
| 	int len = 1; | ||||
| 
 | ||||
| 	while (ptr->next != list) { | ||||
| 		ptr = ptr->next; | ||||
| 		++len; | ||||
| 	} | ||||
| 	return len; | ||||
| } | ||||
| /*int main() {
 | ||||
| struct Node x[10]; | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 
 | ||||
| 	int i; | ||||
| 	list_init(a); | ||||
| 
 | ||||
| 	 | ||||
| 	for(i=0; i<10; i++) { | ||||
| 		x[i].data = i; | ||||
| 		list_insert(a, &x[i]); | ||||
| 	} | ||||
| 	list_print(a); | ||||
| 	//while(list_delete(a->next));
 | ||||
| 	list_clear(a); | ||||
| 	free(a); | ||||
| }*/ | ||||
| 
 | ||||
| int main() { | ||||
| 	int i; | ||||
| 	int N ,M; | ||||
| 	scanf("%d", &N); | ||||
| 	scanf("%d", &M); | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 	struct Node * res = malloc(sizeof(struct Node)); | ||||
| 	list_init(a); | ||||
| 	list_init(res); | ||||
| 	 | ||||
| 	for(i=1; i<=N; i++) { | ||||
| 		list_push_back(a, i); | ||||
| 	} | ||||
| 	struct Node *ptr = a->next; | ||||
| 	struct Node *next = NULL; | ||||
| 	 | ||||
| 	int size = N; | ||||
| 	while (ptr->next != ptr && ptr->prev != ptr) { | ||||
| 		for (i = 0; i < M-1; ++i) { | ||||
| 			if (ptr->next == a) { | ||||
| 				ptr = ptr->next->next; | ||||
| 			} | ||||
| 			else { | ||||
| 				ptr = ptr->next; | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if (size <=  3) { | ||||
| 			list_push_back(res, ptr->data); | ||||
| 		} | ||||
| 
 | ||||
| 		next = ptr->next; | ||||
| 		list_delete(ptr); | ||||
| 	 | ||||
| 		if (next == a) { | ||||
| 			ptr = next->next; | ||||
| 		} | ||||
| 		else { | ||||
| 			ptr = next; | ||||
| 		} | ||||
| 		--size; | ||||
| 	} | ||||
| 
 | ||||
| 	list_print(res); | ||||
| 
 | ||||
| 	while(list_delete(a->next) != -1); | ||||
| 	while(list_delete(res->next) != -1); | ||||
| 	free(a); free(res); | ||||
| } | ||||
							
								
								
									
										54
									
								
								list_and_trees/lists/ejudge/list3.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								list_and_trees/lists/ejudge/list3.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| #include "../double_linked_list.c" | ||||
| #define MAX 1000000 | ||||
| 
 | ||||
| int main() { | ||||
| 	int i, k, l; | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 	struct Node * b = malloc(sizeof(struct Node));	 | ||||
| 	list_init(a); | ||||
| 	list_init(b); | ||||
| 	int A[5], B[5]; | ||||
| 
 | ||||
| 	for (i = 0; i < 5; ++i) { | ||||
| 		scanf("%d", &A[i]); | ||||
| 		list_push_back(a, A[i]); | ||||
| 	} | ||||
| 	 | ||||
| 	for (i = 0; i < 5; ++i) { | ||||
| 		scanf("%d", &B[i]); | ||||
| 		list_push_back(b, B[i]); | ||||
| 	} | ||||
| 
 | ||||
| 	for (i = 0;; ++i) { | ||||
| 		if (i == MAX) { | ||||
| 			printf("botva\n"); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		if (list_size(a) == 0) { | ||||
| 			printf("second %d\n", i); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		if (list_size(b) == 0) { | ||||
| 			printf("first %d\n", i); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		k = list_pop_front(a); | ||||
| 		l = list_pop_front(b); | ||||
| 		if (k == 10 || l == 0) { | ||||
| 			list_push_back(b, k); | ||||
| 			list_push_back(b, l);		 | ||||
| 		} | ||||
| 		else if (k == 0 || l == 10) { | ||||
| 			list_push_back(a, k); | ||||
| 			list_push_back(a, l); | ||||
| 		} | ||||
| 		else if (k > l) { | ||||
| 			list_push_back(a, l); | ||||
| 			list_push_back(a, k); | ||||
| 		} | ||||
| 		else { | ||||
| 			list_push_back(b, k); | ||||
| 			list_push_back(b, l);	 | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										253
									
								
								list_and_trees/lists/ejudge/list3_full.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										253
									
								
								list_and_trees/lists/ejudge/list3_full.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,253 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| struct Node { | ||||
| 	struct Node * next; | ||||
| 	struct Node * prev; | ||||
| 	Data data; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| void list_init(struct Node * list); | ||||
| void list_insert(struct Node * list, struct Node * t); | ||||
| void list_insert_before(struct Node * list, struct Node * t); | ||||
| void list_remove(struct Node * t); | ||||
| struct Node * list_push_front(struct Node * list, Data d); | ||||
| struct Node * list_push_back(struct Node * list, Data d); | ||||
| Data list_pop_front(struct Node * list); | ||||
| Data list_pop_back(struct Node * list); | ||||
| Data list_delete(struct Node * t); | ||||
| void list_print (struct Node * list); | ||||
| Data list_sum (struct Node * list); | ||||
| int list_is_empty(struct Node * list); | ||||
| int list_clear(struct Node * list); | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param); | ||||
| void print_it(Data d, void * parameter); | ||||
| void sum(Data a, void * s); | ||||
| 
 | ||||
| void list_init(struct Node * list) { | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	list->data = -1; | ||||
| } | ||||
| 
 | ||||
| void list_print (struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		printf("Empty list\n"); | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next != list) { | ||||
| 			printf("%d ", ptr->data); | ||||
| 			//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 			ptr = ptr->next; | ||||
| 		} | ||||
| 		printf("%d\n", ptr->data); | ||||
| 		//printf("%d [%p](prev is %p, next is %p)\n", ptr->data, ptr, ptr->prev, ptr->next);
 | ||||
| 	} | ||||
| }  | ||||
| 
 | ||||
| void list_insert(struct Node * list, struct Node * t) { | ||||
| 	t->next = list->next; | ||||
| 	t->prev = list; | ||||
| 	if (t->next != list) { | ||||
| 		t->next->prev = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->next = list; | ||||
| 		list->prev = t; | ||||
| 	} | ||||
| 	list->next = t; | ||||
| } | ||||
| 
 | ||||
| void list_insert_before(struct Node * list, struct Node * t) { | ||||
| 	t->next = list; | ||||
| 	t->prev = list->prev; | ||||
| 	if (t->prev != list) { | ||||
| 		t->prev->next = t; | ||||
| 	} | ||||
| 	else { | ||||
| 		t->prev = list; | ||||
| 		list->next = t; | ||||
| 	} | ||||
| 	list->prev = t; | ||||
| } | ||||
| 
 | ||||
| void list_remove(struct Node * t) { | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_front(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = ptr; | ||||
| 	oth->prev = list; | ||||
| 	list->next = oth; | ||||
| 	ptr->prev = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| struct Node * list_push_back(struct Node * list, Data d) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 	oth->data = d; | ||||
| 	oth->next = list; | ||||
| 	oth->prev = ptr; | ||||
| 	list->prev = oth; | ||||
| 	ptr->next = oth; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| int list_is_empty(struct Node * list) { | ||||
| 	if (list == list->next) { | ||||
| 		return 1; | ||||
| 	} | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| int list_clear(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	 | ||||
| 	struct Node *n; | ||||
| 	while (ptr->next != list) { | ||||
| 		n = ptr->next; | ||||
| 		free(ptr); | ||||
| 		ptr = n; | ||||
| 	}	 | ||||
| 	list->next = list; | ||||
| 	list->prev = list; | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_front(struct Node * list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	Data val = ptr->data; | ||||
| 	list->next = ptr->next; | ||||
| 	ptr->next->prev = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_pop_back(struct Node * list) { | ||||
| 	struct Node *ptr = list->prev; | ||||
| 	Data val = ptr->data; | ||||
| 	list->prev = ptr->prev; | ||||
| 	ptr->prev->next = list; | ||||
| 	free(ptr); | ||||
| 
 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_delete(struct Node * t) { | ||||
| 	if (t->next == t) { | ||||
| 		return -1; | ||||
| 	} | ||||
| 	t->prev->next = t->next; | ||||
| 	t->next->prev = t->prev; | ||||
| 	Data val = t->data; | ||||
| 	free(t); | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| void list_for_each(struct Node * list, void (*func)(Data d, void * param), void * param) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	while (ptr->next != list) { | ||||
| 		func(ptr->data, param); | ||||
| 		ptr = ptr->next; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void sum(Data a, void * s) { | ||||
| 	*(Data *)s += a; | ||||
| } | ||||
| 
 | ||||
| void print_it(Data d, void * param) { | ||||
| 	FILE * fd = param; | ||||
| 	fprintf(fd, "%d ", d); | ||||
| } | ||||
| 
 | ||||
| Data list_sum (struct Node * list) { | ||||
| 	int s = 0; | ||||
| 	list_for_each(list, sum, &s); | ||||
| 	return s; | ||||
| } | ||||
| 
 | ||||
| int list_size(struct Node *list) { | ||||
| 	struct Node *ptr = list->next; | ||||
| 	if (ptr == list) { | ||||
| 		return 0; | ||||
| 	} | ||||
| 	int len = 1; | ||||
| 
 | ||||
| 	while (ptr->next != list) { | ||||
| 		ptr = ptr->next; | ||||
| 		++len; | ||||
| 	} | ||||
| 	return len; | ||||
| } | ||||
| 
 | ||||
| #define MAX 1000000 | ||||
| 
 | ||||
| int main() { | ||||
| 	int i, k, l; | ||||
| 	struct Node * a = malloc(sizeof(struct Node)); | ||||
| 	struct Node * b = malloc(sizeof(struct Node));	 | ||||
| 	list_init(a); | ||||
| 	list_init(b); | ||||
| 	int A[5], B[5]; | ||||
| 
 | ||||
| 	for (i = 0; i < 5; ++i) { | ||||
| 		scanf("%d", &A[i]); | ||||
| 		list_push_back(a, A[i]); | ||||
| 	} | ||||
| 	 | ||||
| 	for (i = 0; i < 5; ++i) { | ||||
| 		scanf("%d", &B[i]); | ||||
| 		list_push_back(b, B[i]); | ||||
| 	} | ||||
| 	 | ||||
| 	for (i = 0;; ++i) { | ||||
| 		if (i == MAX) { | ||||
| 			printf("botva\n"); | ||||
| 			while(list_delete(a->next) != -1); | ||||
| 			while(list_delete(b->next) != -1); | ||||
| 			free(a); free(b); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		if (list_size(a) == 0) { | ||||
| 			printf("second %d\n", i); | ||||
| 			while(list_delete(b->next) != -1); | ||||
| 			free(a); free(b); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		if (list_size(b) == 0) { | ||||
| 			printf("first %d\n", i); | ||||
| 			while(list_delete(a->next) != -1); | ||||
| 			free(a); free(b); | ||||
| 			return 0; | ||||
| 		} | ||||
| 		k = list_pop_front(a); | ||||
| 		l = list_pop_front(b); | ||||
| 		 | ||||
| 		if ((k == 9) && (l == 0)) { | ||||
| 			list_push_back(b, k); | ||||
| 			list_push_back(b, l);		 | ||||
| 		} | ||||
| 		else if ((k == 0) && (l == 9)) { | ||||
| 			list_push_back(a, k); | ||||
| 			list_push_back(a, l); | ||||
| 		} | ||||
| 		else if (k > l) { | ||||
| 			list_push_back(a, k); | ||||
| 			list_push_back(a, l); | ||||
| 		} | ||||
| 		else { | ||||
| 			list_push_back(b, k); | ||||
| 			list_push_back(b, l);	 | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										185
									
								
								list_and_trees/lists/list.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								list_and_trees/lists/list.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,185 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| struct Node { | ||||
| 	Data val; | ||||
| 	struct Node * next; | ||||
| }; | ||||
| struct List { | ||||
| 		struct Node * head; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| struct List * list_create (); | ||||
| void list_add_first (struct List * list, Data x); | ||||
| void list_add_last (struct List * list, Data x); | ||||
| Data list_remove_first (struct List * list);  | ||||
| Data list_remove_last (struct List * list); | ||||
| Data list_get_first (struct List * list); | ||||
| Data list_get_last (struct List * list); | ||||
| void list_print (struct List * list); | ||||
| int list_size(struct List * list); | ||||
| void list_clear(struct List * list); | ||||
| void list_destroy (struct List * list); | ||||
| 
 | ||||
| struct List * list_create () { | ||||
| 	struct List *list = malloc(sizeof(struct List)); | ||||
| 	list->head = NULL; | ||||
| 	return list; | ||||
| } | ||||
| 
 | ||||
| void list_add_last (struct List * list, Data x) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	if (ptr == NULL) { | ||||
| 		struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 		oth->val = x; | ||||
| 		oth->next = NULL; | ||||
| 		list->head = oth; | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next != NULL) { | ||||
| 			ptr = ptr->next;  | ||||
| 		} | ||||
| 		struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 		oth->val = x; | ||||
| 		oth->next = NULL; | ||||
| 		ptr->next = oth;	 | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void list_add_first (struct List * list, Data x) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	if (ptr == NULL) { | ||||
| 		struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 		oth->val = x; | ||||
| 		oth->next = NULL; | ||||
| 		list->head = oth; | ||||
| 	} | ||||
| 	else { | ||||
| 		struct Node *oth = malloc(sizeof(struct Node)); | ||||
| 		oth->val = x; | ||||
| 		oth->next = ptr; | ||||
| 		list->head = oth;	 | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void list_print(struct List * list) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	if (ptr == NULL) { | ||||
| 		printf("Empty list\n"); | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next != NULL) { | ||||
| 			printf("%d ", ptr->val); | ||||
| 			ptr = ptr->next; | ||||
| 		} | ||||
| 		printf("%d\n", ptr->val); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| Data list_remove_first (struct List * list) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	Data val = ptr->val; | ||||
| 	if (ptr->next == NULL) { | ||||
| 		list->head = NULL; | ||||
| 	} | ||||
| 	else { | ||||
| 		list->head = list->head->next; | ||||
| 	} | ||||
| 	free(ptr); | ||||
| 	return val; | ||||
| }  | ||||
| 
 | ||||
| Data list_remove_last (struct List * list) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	Data val; | ||||
| 	if (ptr->next == NULL) { | ||||
| 		list->head = NULL; | ||||
| 		val = ptr->val; | ||||
| 		free(ptr); | ||||
| 	} | ||||
| 	else { | ||||
| 		while (ptr->next->next != NULL) { | ||||
| 			ptr = ptr->next; | ||||
| 		}	 | ||||
| 
 | ||||
| 		struct Node* lastNode = ptr->next; | ||||
| 		val = lastNode->val; | ||||
| 		ptr->next = NULL; | ||||
| 		free(lastNode); | ||||
| 	} | ||||
| 	 | ||||
| 	return val; | ||||
| } | ||||
| 
 | ||||
| Data list_get_first (struct List * list) { | ||||
| 	return list->head->val; | ||||
| } | ||||
| 
 | ||||
| Data list_get_last (struct List * list) { | ||||
| 	struct Node *ptr = list->head;	 | ||||
| 	while (ptr->next != NULL) { | ||||
| 		ptr = ptr->next; | ||||
| 	} | ||||
| 	return ptr->val; | ||||
| } | ||||
| int list_size(struct List * list) { | ||||
| 	struct Node *ptr = list->head; | ||||
| 	if (ptr == NULL) { | ||||
| 		return 0; | ||||
| 	} | ||||
| 	int len = 1; | ||||
| 	while (ptr->next != NULL) { | ||||
| 		ptr = ptr->next; | ||||
| 		++len; | ||||
| 	} | ||||
| 	return len; | ||||
| } | ||||
| 
 | ||||
| void list_clear(struct List * list) {	 | ||||
| 	struct Node *ptr = list->head; | ||||
| 	 | ||||
| 	if (ptr == NULL) { | ||||
| 		free(ptr); | ||||
| 	} | ||||
| 	else { | ||||
| 		struct Node *next; | ||||
| 		while (ptr->next != NULL) { | ||||
| 			next = ptr->next; | ||||
| 			free(ptr); | ||||
| 			ptr = next; | ||||
| 		} | ||||
| 	} | ||||
| 	list->head = NULL; | ||||
| } | ||||
| 
 | ||||
| void list_destroy (struct List * list) { | ||||
| 	list_clear(list); | ||||
| 	free(list); | ||||
| } | ||||
| int main() { | ||||
| 	struct List * list = list_create(); | ||||
| 	//printf("%d\n", list_size(list));
 | ||||
| 	list_add_last(list, 5); | ||||
| 	list_add_last(list, 6); | ||||
| 	list_add_first(list, 3); | ||||
| 	list_add_last(list, 8); | ||||
| 	list_add_last(list, 9); | ||||
| 	list_print(list); | ||||
| 	printf("Removed - %d\n", list_remove_first(list)); | ||||
| 	list_print(list); | ||||
| 	printf("Removed - %d\n", list_remove_last(list)); | ||||
| 	list_print(list); | ||||
| 	printf("%d\n", list_get_first(list)); | ||||
| 	printf("%d\n", list_get_last(list)); | ||||
| 	printf("%d\n", list_size(list)); | ||||
| 	//list_clear(list);
 | ||||
| 	list_print(list); | ||||
| 	list_destroy(list); | ||||
| 	list_print(list); | ||||
| 	/*list = list_create();
 | ||||
| 	list_add_first(list, 5); | ||||
| 	list_print(list); | ||||
| 	list_destroy(list);*/ | ||||
| } | ||||
							
								
								
									
										83
									
								
								list_and_trees/trees/tree1.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								list_and_trees/trees/tree1.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,83 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| 
 | ||||
| struct Node { | ||||
| 	Data val;            // данные в узле
 | ||||
| 	struct Node * left;  // левый ребенок
 | ||||
| 	struct Node * right; // правый ребенок
 | ||||
| }; | ||||
| 
 | ||||
| struct Node * tree_add (struct Node * tree, Data x) {	 | ||||
| 	if (tree == NULL) { | ||||
| 		struct Node * data = malloc(sizeof(struct Node)); | ||||
| 		data->left = NULL; data->right = NULL; | ||||
| 		data->val = x; | ||||
| 		//printf("Init tree - %d[%p]\n", data->val, data);
 | ||||
| 		return data; | ||||
| 	} | ||||
| 
 | ||||
| 	if (x < tree->val) { | ||||
| 		if (tree->left == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->left = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val, tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->left, x); | ||||
| 		} | ||||
| 	} | ||||
| 	if (x > tree->val) { | ||||
| 		if (tree->right == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->right = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val ,tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->right, x); | ||||
| 		} | ||||
| 	} | ||||
| 	return tree; | ||||
| 	 | ||||
| } | ||||
| 
 | ||||
| void tree_print (struct Node * tree) { | ||||
| 	if (tree != NULL) { | ||||
| 		tree_print(tree->left); | ||||
| 		printf("%d ", tree->val); | ||||
| 		tree_print(tree->right); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void tree_destroy (struct Node * tree) { | ||||
| 	if (tree->left != NULL) { | ||||
| 		tree_destroy(tree->left); | ||||
| 	} | ||||
| 	if(tree->right != NULL) { | ||||
| 		tree_destroy(tree->right); | ||||
| 	} | ||||
| 	free(tree); | ||||
| } | ||||
| 
 | ||||
| /*int main() {
 | ||||
| 	struct Node * tree = NULL; | ||||
| 	tree = tree_add(tree, 7); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree = tree_add(tree, 3); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree = tree_add(tree, 2); | ||||
| 	tree = tree_add(tree, 1); | ||||
| 	tree = tree_add(tree, 9); | ||||
| 	tree = tree_add(tree, 5); | ||||
| 	tree = tree_add(tree, 4); | ||||
| 	tree = tree_add(tree, 6); | ||||
| 	tree = tree_add(tree, 8); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree_print(tree); | ||||
| 	tree_destroy(tree); | ||||
| } */ | ||||
							
								
								
									
										13
									
								
								list_and_trees/trees/tree2.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								list_and_trees/trees/tree2.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| #include "tree1.c" | ||||
| 
 | ||||
| int main() { | ||||
| 	int i; | ||||
| 	struct Node * tree = NULL; | ||||
| 	scanf("%d", &i); | ||||
| 	while (i) { | ||||
| 		tree = tree_add(tree, i); | ||||
| 		scanf("%d", &i);	 | ||||
| 	} | ||||
| 	tree_print(tree); | ||||
| 	tree_destroy(tree);	 | ||||
| } | ||||
							
								
								
									
										95
									
								
								list_and_trees/trees/tree2_full.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								list_and_trees/trees/tree2_full.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,95 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| 
 | ||||
| struct Node { | ||||
| 	Data val;            // данные в узле
 | ||||
| 	struct Node * left;  // левый ребенок
 | ||||
| 	struct Node * right; // правый ребенок
 | ||||
| }; | ||||
| 
 | ||||
| struct Node * tree_add (struct Node * tree, Data x) {	 | ||||
| 	if (tree == NULL) { | ||||
| 		struct Node * data = malloc(sizeof(struct Node)); | ||||
| 		data->left = NULL; data->right = NULL; | ||||
| 		data->val = x; | ||||
| 		//printf("Init tree - %d[%p]\n", data->val, data);
 | ||||
| 		return data; | ||||
| 	} | ||||
| 
 | ||||
| 	if (x < tree->val) { | ||||
| 		if (tree->left == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->left = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val, tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->left, x); | ||||
| 		} | ||||
| 	} | ||||
| 	if (x > tree->val) { | ||||
| 		if (tree->right == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->right = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val ,tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->right, x); | ||||
| 		} | ||||
| 	} | ||||
| 	return tree; | ||||
| 	 | ||||
| } | ||||
| 
 | ||||
| void tree_print (struct Node * tree) { | ||||
| 	if (tree != NULL) { | ||||
| 		tree_print(tree->left); | ||||
| 		printf("%d ", tree->val); | ||||
| 		tree_print(tree->right); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void tree_destroy (struct Node * tree) { | ||||
| 	if (tree->left != NULL) { | ||||
| 		tree_destroy(tree->left); | ||||
| 	} | ||||
| 	if(tree->right != NULL) { | ||||
| 		tree_destroy(tree->right); | ||||
| 	} | ||||
| 	free(tree); | ||||
| } | ||||
| 
 | ||||
| /*int main() {
 | ||||
| 	struct Node * tree = NULL; | ||||
| 	tree = tree_add(tree, 7); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree = tree_add(tree, 3); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree = tree_add(tree, 2); | ||||
| 	tree = tree_add(tree, 1); | ||||
| 	tree = tree_add(tree, 9); | ||||
| 	tree = tree_add(tree, 5); | ||||
| 	tree = tree_add(tree, 4); | ||||
| 	tree = tree_add(tree, 6); | ||||
| 	tree = tree_add(tree, 8); | ||||
| 	//printf("Tree - [%p]\n", tree);
 | ||||
| 	tree_print(tree); | ||||
| 	tree_destroy(tree); | ||||
| } */ | ||||
| 
 | ||||
| int main() { | ||||
| 	int i; | ||||
| 	struct Node * tree = NULL; | ||||
| 	scanf("%d", &i); | ||||
| 	while (i) { | ||||
| 		tree = tree_add(tree, i); | ||||
| 		scanf("%d", &i);	 | ||||
| 	} | ||||
| 	tree_print(tree); | ||||
| 	tree_destroy(tree);	 | ||||
| } | ||||
							
								
								
									
										94
									
								
								list_and_trees/trees/tree4_full.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								list_and_trees/trees/tree4_full.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,94 @@ | |||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| typedef int Data; | ||||
| 
 | ||||
| struct Node { | ||||
| 	Data val;            // данные в узле
 | ||||
| 	struct Node * left;  // левый ребенок
 | ||||
| 	struct Node * right; // правый ребенок
 | ||||
| }; | ||||
| 
 | ||||
| struct Node * tree_add (struct Node * tree, Data x) {	 | ||||
| 	if (tree == NULL) { | ||||
| 		struct Node * data = malloc(sizeof(struct Node)); | ||||
| 		data->left = NULL; data->right = NULL; | ||||
| 		data->val = x; | ||||
| 		//printf("Init tree - %d[%p]\n", data->val, data);
 | ||||
| 		return data; | ||||
| 	} | ||||
| 
 | ||||
| 	if (x < tree->val) { | ||||
| 		if (tree->left == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->left = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val, tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->left, x); | ||||
| 		} | ||||
| 	} | ||||
| 	if (x > tree->val) { | ||||
| 		if (tree->right == NULL) { | ||||
| 			struct Node * data = malloc(sizeof(struct Node)); | ||||
| 			data->left = NULL; data->right = NULL; | ||||
| 			data->val = x; | ||||
| 			tree->right = data; | ||||
| 			//printf("Added - %d [%p] to node %d[%p] left[%p], right[%p]\n", x, data, tree->val ,tree, tree->left, tree->right );
 | ||||
| 		} | ||||
| 		else { | ||||
| 			tree_add(tree->right, x); | ||||
| 		} | ||||
| 	} | ||||
| 	return tree; | ||||
| 	 | ||||
| } | ||||
| 
 | ||||
| void tree_print (struct Node * tree) { | ||||
| 	if (tree != NULL) { | ||||
| 		tree_print(tree->left); | ||||
| 		printf("%d ", tree->val); | ||||
| 		tree_print(tree->right); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void tree_add_leafs (struct Node * tree, struct Node * * res) { | ||||
| 	if (tree != NULL) { | ||||
| 		tree_add_leafs(tree->left, res); | ||||
| 		if (tree->left == NULL && tree->right == NULL) { | ||||
| 			*res = tree_add(*res, tree->val); | ||||
| 		} | ||||
| 		tree_add_leafs(tree->right, res); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void tree_destroy (struct Node * tree) { | ||||
| 	if (tree != NULL) { | ||||
| 		if (tree->left != NULL) { | ||||
| 			tree_destroy(tree->left); | ||||
| 		} | ||||
| 		if(tree->right != NULL) { | ||||
| 			tree_destroy(tree->right); | ||||
| 		} | ||||
| 		free(tree); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
| 	int i; | ||||
| 	struct Node * tree = NULL; | ||||
| 	struct Node * res = NULL; | ||||
| 	scanf("%d", &i); | ||||
| 	while (i) { | ||||
| 		tree = tree_add(tree, i); | ||||
| 		scanf("%d", &i);	 | ||||
| 	} | ||||
| 
 | ||||
| 	tree_add_leafs(tree, &res); | ||||
| 	 | ||||
| 	tree_print(res); | ||||
| 	tree_destroy(tree);	 | ||||
| 	tree_destroy(res); | ||||
| } | ||||
		Reference in a new issue