Можно ли как-то изменить условие для enum на тоже самое для char? - C (СИ)
Формулировка задачи:
Есть bstInsert, PKL и bstRemove
все это для
А нужно тоже самое для
Листинг программы
- int main(void)
- {
- int i, maxBFS;
- char cmd[255], arg;
- BstNode *root = NULL;
- do
- {
- printf("Введите команду (h - справка):\n");
- scanf("%s", cmd);
- if (cmd[0] == '+')
- {
- scanf(" %c", &arg);
- if (arg >= 'A' && arg <= 'Z')
- {
- bstInsert(&root, arg - 'A');
- printf("Узел %c вставлен\n", arg);
- }
- else
- printf("Ошибка. Введена недопустимая буква\n");
- }
- else if (cmd[0] == '-')
- {
- scanf(" %c", &arg);
- if (arg >= 'A' && arg <= 'Z')
- {
- if (bstRemove(&root, arg - 'A'))
- printf("Узел %c удален\n", arg);
- else
- printf("Узел %c не найден\n", arg);
- }
- else
- printf("Ошибка. Введена недопустимая буква\n");
- }
- else if (cmd[0] == 'p')
- {
- PKL(&root, 0);
- }
- else if (cmd[0] == 't')
- {printf("HI\n");}
- else if (cmd[0] == 'h')
- {
- printf("================================\n");
- printf("Список команд:\n");
- printf("+ CHAR - вставить узел CHAR (A, B, ..., Z) в двоичное дерево\n");
- printf("- CHAR - удалить узел CHAR из двоичного дерева\n");
- printf("p - распечатать двоичное дерево\n");
- printf("t - выполнить задание над двоичным деревом\n");
- printf("q - завершить программу\n");
- printf("================================\n");
- }
- else if (cmd[0] != 'q')
- {
- printf("Неизвестная команда\n");
- }
- }
- while (cmd[0] != 'q');
- bstDestroy(&root);
- return 0;
- }
- void PKL(BstNode **node, const int level)
- {
- if (*node == NULL)
- {
- printf("Дерево пусто\n");
- return;
- }
- if ((*node)->_right != NULL)
- PKL(&(*node)->_right, level + 1);
- printf("%*s%c\n", level * 2, "", (*node)->_key + 'A');
- if ((*node)->_left != NULL)
- PKL(&(*node)->_left, level + 1);
- }
Листинг программы
- BstNode *bstInsert(BstNode **node, const kLetters key)
- {
- if (*node == NULL)
- {
- *node = (BstNode *)malloc(sizeof(BstNode));
- (*node)->_key = key;
- (*node)->_left = NULL;
- (*node)->_right = NULL;
- return *node;
- }
- else if ((*node)->_key == key)
- return *node;
- else if (key < (*node)->_key)
- return bstInsert(&(*node)->_left, key);
- else
- return bstInsert(&(*node)->_right, key);
- }
- int bstRemove(BstNode **node, const kLetters key)
- {
- BstNode *repl = NULL, *parent = NULL, *tmp = *node;
- while (tmp != NULL && tmp->_key != key)
- {
- parent = tmp;
- if (key < tmp->_key)
- tmp = tmp->_left;
- else
- tmp = tmp->_right;
- }
- if (tmp == NULL)
- return 0;
- if (tmp->_left != NULL && tmp->_right == NULL)
- {
- if (parent != NULL)
- {
- if (parent->_left == tmp)
- parent->_left = tmp->_left;
- else
- parent->_right = tmp->_left;
- }
- else
- *node = tmp->_left;
- free(tmp);
- tmp = NULL;
- }
- else if (tmp->_left == NULL && tmp->_right != NULL)
- {
- if (parent != NULL)
- {
- if (parent->_left == tmp)
- parent->_left = tmp->_right;
- else
- parent->_right = tmp->_right;
- }
- else
- *node = tmp->_right;
- free(tmp);
- tmp = NULL;
- }
- else if (tmp->_left != NULL && tmp->_right != NULL)
- {
- repl = tmp->_right;
- if (repl->_left == NULL)
- tmp->_right = repl->_right;
- else
- {
- while (repl->_left != NULL)
- {
- parent = repl;
- repl = repl->_left;
- }
- parent->_left = repl->_right;
- }
- tmp->_key = repl->_key;
- free(repl);
- repl = NULL;
- }
- else
- {
- if (parent != NULL)
- {
- if (parent->_left == tmp)
- parent->_left = NULL;
- else
- parent->_right = NULL;
- }
- else
- *node = NULL;
- free(tmp);
- tmp = NULL;
- }
- return 1;
- }
- void bstDestroy(BstNode **node)
- {
- if (*node == NULL)
- return;
- if ((*node)->_left != NULL)
- bstDestroy(&(*node)->_left);
- if ((*node)->_right != NULL)
- bstDestroy(&(*node)->_right);
- free(*node);
- *node = NULL;
- }
Листинг программы
- typedef enum _kLetters
- {
- A = 0, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
- } kLetters;
- typedef struct _BstNode
- {
- kLetters _key;
- struct _BstNode *_left;
- struct _BstNode *_right;
- } BstNode;
- BstNode *bstInsert(BstNode **node, const kLetters key);
- int bstRemove(BstNode **node, const kLetters key);
- void bstDestroy(BstNode **node);
Листинг программы
- typedef char _kLetters
Решение задачи: «Можно ли как-то изменить условие для enum на тоже самое для char?»
textual
Листинг программы
- kLetters
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д