Я создаю дерево переменных, но у меня ошибка, похоже, в функции insert или print_wevr.
Когда я запускаю программу, я получаю бесконечный цикл.
Как мне это сделать?
/*5 - VarTrees*/
/*var_trees.c*/
#include <stdio.h>
#include <stdlib.h>
#include "var_trees.h"
/*Defining the struture of variable tree.
* The nodes has tree fields:
info: an generic information
first: the first child-node
next: the next sibling-node
*/
struct var_tree{
void* info;
Var_Tree* first;
Var_Tree* next;
};
/*Create the tree*/
Var_Tree* create(void* info)
{
Var_Tree* t = (Var_Tree*) malloc(sizeof(Var_Tree));
t->info = info;
t->first = NULL;
t->next = NULL;
return t;
}
/*Insert a node*/
void insert(Var_Tree* t,Var_Tree* st)
{
st->next = t->first;
t->first = st;
}
/*go_through the tree*/
void go(Var_Tree* t, void (*cb)(void*))
{
Var_Tree* p;
cb(t->info);
for(p = t->first; p != NULL; p = p->next)
go(t,cb);
printf(">");
}
/*Remove a node*/
//void remov(Var_Tree* t, void* info);
/*5 - VarTrees*/
/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include "var_trees.h"
Var_Tree* create_int(int info)
{
return create(&info);
}
void print_int(void* info)
{
int* t = (int*) info;
printf("<%d",*t);
}
int main(void)
{
Var_Tree* a = create_int(4);
Var_Tree* b = create_int(3);
Var_Tree* c = create_int(23);
Var_Tree* d = create_int(1);
Var_Tree* e = create_int(2);
insert(a,b);
go(a,print_tree);
}
Функция create_int
- это функция для создания узла с информацией о поле как int.
print_int
- это функция обратного вызова, которая печатает целое число («<» создает тип текстовой записи для деревьев). </p>