i am getting lost with all he variables in each step
Code:#include <stdio.h> #include <stdlib.h> typedef struct node { int value; int explored; struct node *left; struct node *right; struct node *next; } Node; void add(Node **ptree, Node *n){ if (*ptree == NULL) *ptree = n; else if (n->value<(*ptree)->value) add(&(*ptree)->left, n); else add(&(*ptree)->right, n); } void push(Node **plist, Node *n){ if (n){ n->next = *plist; *plist = n; } } Node* pop(Node **plist){ Node *n = NULL; if (*plist){ n = *plist; *plist = (*plist)->next; } return n; } void what(Node *tree){ Node *list = NULL; Node *n; if (tree){ push(&list, tree); while (n = pop(&list)){ if (!(n->explored)){ n->explored = 1; push(&list, n->right); push(&list, n); push(&list, n->left); } else printf("%d ", n->value); } } } Node *create(int num){ Node *n; n = (Node *)malloc(sizeof(Node)); n->value = num; n->explored = 0; n->left = n->right = n->next = NULL; return n; } void main(){ Node *tree = NULL; add(&tree, create(4)); add(&tree, create(6)); add(&tree, create(5)); add(&tree, create(7)); add(&tree, create(3)); add(&tree, create(2)); add(&tree, create(1)); what(tree); }




Reply With Quote