Request for member Ошибка - C (СИ)
Формулировка задачи:
Пользуюсь CodeBlocks и вылезает ошибка :request for member 'key' in something not a structure or union
Незнаю с чем связано и как пофиксить. Помогите)
#include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #include <locale.h> #define size 10 //const int size = 10; struct Node { int color; int key; struct Node *left, *right, *parent; char *info; }; struct Item { int busy; int key; struct Node *next; }; struct biba { struct Item *tab[size]; struct Node *root; }; struct Node Elist = { 1 }; char *getStr(); int getInt(int *); void ignore(); void initTab(struct biba *); int findADD(struct biba *, int); int insert(struct biba *, int,char *); int hash(int); int RB_Insert(struct Node * , struct biba *); int RB_Insert_Fixup(struct Node *, struct biba *); int left_rotate(struct Node *,struct biba *); int right_rotate(struct Node *, struct biba *); void delRB(struct biba *); void delRB1(struct Node *); int DeleteKey(struct biba *, int); void RB_Delete(struct Node *, struct biba *); struct Node *findDelete(struct biba *, int); struct Node *successor(struct Node *,struct biba *); //поиск следующиего элемента struct Node *minRB(struct Node *, struct biba *);//поиск минимального элемента void RB_Delete_Fixup(struct Node *,struct biba *); //void showRB(Node *root); struct Node *find1(struct biba *, int key, int *); struct Node *find2(struct biba *, int key); struct Node *find3(struct Node *, int *,int); void putTree(struct Node *ptr, int lvl); const char *msgs[] = { "0. Выход", "1. Добавить элемент", "2. Найти ключ", "3. Удалить", "4. Вывести таблицу" }; const int NMsgs = sizeof(msgs) / sizeof(msgs[0]); int dialog(const char *msgs[], int); int D_Add(struct biba *), D_Find(struct biba *), D_Delete(struct biba *), D_Show(struct biba *); int(*fptr[])(struct biba *) = { NULL, D_Add, D_Find, D_Delete, D_Show }; //Элементарные void ignore() { scanf("%*[^\n]"); scanf("%*c"); } char *getStr() { char buf[21]; int n, len = 0, curLen; char *ptr = (char *)malloc(1); *ptr = '\0'; do { n = scanf("%20[^\n]", buf, 21); if (n<0) { free(ptr); ptr = NULL; continue; } if (n>0) { curLen = strlen(buf); len += curLen; ptr = (char *)realloc(ptr, len + 1); //strcat(ptr, len + 1, buf); strcat(ptr, buf); } else scanf("%*c"); } while (n>0); return ptr; } int getInt(int *p) { int n; do { n = scanf("%d", p); if (n < 0) return 0; if (n == 0) { printf("error! Repeat input!\n"); scanf("%*[^\n]"); } } while (n == 0); return 1; } int main() { setlocale(LC_ALL, "Rus"); int rc; struct biba *boba=(struct biba *)calloc(sizeof(struct biba), 1); initTab(boba); //делает таблицу пустой while (rc = dialog(msgs, NMsgs)) { if (!fptr[rc](boba)) break; // при выполнении диалоговой функции обнаружен конец файла } delRB(boba);//написать удаление дерева printf("Пока\n"); return 0; } int dialog(const char *msgs[], int N) { char *errmsg = ""; int rc, i, n; do { puts(errmsg); errmsg = "You are wrong. Repeat, please!"; // вывод списка альтернатив for (i = 0; i < N; ++i) puts(msgs[i]); puts("Введите номер действия: --> "); n = getInt(&rc); // ввод номера альтернативы if (n == 0) // конец файла - конец работы rc = 0; } while (rc < 0 || rc >= N); return rc; } int hash(int k) { return k%size; } void initTab(struct biba *boba) { int i; for (i = 0; i < size; i++) { boba->tab[i].key = 0; boba->tab[i].busy = 0; boba->tab[i].next = NULL; } boba->root = NULL; } void delRB(struct biba *boba) { int i; for (i = 0; i < size; ++i) { boba->tab[i].next = NULL; } delRB1(boba->root); } void delRB1(struct Node *ptr) { if (ptr != &Elist&&ptr != NULL) { delRB1(ptr->left); free(ptr->info); //free(ptr); delRB1(ptr->right); } } //Диалог const char *errmsgs[] = { "Ok", "Таблица переполнена","Таакой элемент есть","Нет такого родителя" }; int D_Add(struct biba *boba) { int key; char *info=NULL; puts("ВВедите ключ"); getInt(&key); if (findADD(boba,key)) { puts("ВВедите инфо-->"); ignore(); info=getStr(); insert(boba, key,info); puts("Ключ вставлен"); } else { puts("Такой ключ уже есть"); } free(info); return 1; } int D_Find(struct biba *boba) { int key; struct Node *ptr; int p; puts("ВВедите искомый ключ"); getInt(&key); if (!(findADD(boba,key))) { puts("Получить и распишитесь"); ptr=find1(boba, key,&p); if (p) printf("Ключ %d инфо %s цвет %d взят из таблицы", ptr->key, ptr->info, ptr->color); else printf("Ключ %d инфо %s цвет %d взят из дерева", ptr->key, ptr->info, ptr->color); } else { puts("Такого ключа я не нашел, но есть немного похожий ->"); ptr=find2(boba,key); printf("Ключ %d инфо %s цвет %d", ptr->key, ptr->info, ptr->color); } return 1; } int D_Delete(struct biba *boba) { int key; ignore(); puts("ВВедите удаляемый ключ"); getInt(&key); if (DeleteKey(boba, key)) puts("Такой элемент вы больше не увидите"); else puts("Такого элемента нет в дереве"); return 1; } int D_Show(struct biba *boba) { puts("ВСЁ что вы запихнули в таблицу"); putTree(boba->root, 0); //showRB(boba->root); return 1; } //Функции вставки int insert(struct biba *boba, int key,char *info) { int i=hash(key); struct Node *x = (struct Node *)calloc(sizeof(struct Node),1); boba->tab[i].busy = 1; boba->tab[i].key = key; boba->tab[i].next = x; x->info = _strdup(info); x->key = key; RB_Insert(x,boba); return 1; } int RB_Insert(struct Node *x,struct biba *boba) { struct Node *prev = &Elist, *ptr = boba->root; while (ptr != &Elist&&ptr != NULL) { prev = ptr; if (x->key < ptr->key) ptr = ptr->left; else ptr = ptr->right; } x->parent = prev; if (prev == &Elist) boba->root = x; else if (x->key < prev->key) prev->left = x; else prev->right = x; x->left = &Elist; x->right = &Elist; x->color = 0; RB_Insert_Fixup(x, boba); return 1; } int RB_Insert_Fixup(struct Node *x, struct biba *boba) { struct Node *p1, *pp, *p2; while (x->parent->color == 0 ) { p1 = x->parent; pp = p1->parent; if (p1 == pp->left) { p2 = pp->right; if (p2->color == 0) { p1->color = 1; p2->color = 1; pp->color = 0; x = pp; } else { if (x == p1->right) { x = p1; left_rotate(x,boba); p1 = x->parent; } p1->color = 1; pp->color = 0; right_rotate(pp,boba); } } else { p2 = pp->left; if (p2->color == 0) { p1->color = 1; p2->color = 1; pp->color = 0; x = pp; } else { if (x == p1->left) { x = p1; right_rotate(x, boba); p1 = x->parent; } p1->color = 1; pp->color = 0; left_rotate(pp, boba); } } } boba->root->color = 1; return 1; } int left_rotate(struct Node *x, struct biba *boba) { struct Node *y, *p; y = x->right; p = x->parent; x->right = y->left; if (y->left != &Elist) y->left->parent = x; y->parent = p; if (p == &Elist) boba->root = y; else { if (p->left == x) p->left = y; else p->right = y; } y->left = x; x->parent = y; return 1; } int right_rotate(struct Node *x, struct biba *boba) { struct Node *y, *p; y = x->left; p = x->parent; x->left = y->right; if (y->right != &Elist) y->right->parent = x; y->parent = p; if (p == &Elist) boba->root = y; else { if (p->left == x) p->left = y; else p->right = y; } y->right = x; x->parent = y; return 1; } int findADD(struct biba *boba,int key) { int i=hash(key); struct Node *ptr = boba->root; if (boba->tab[i].key == key&&boba->tab[i].busy==1) return 0; while (ptr != &Elist && ptr!=NULL) { if (ptr->key == key) return 0; else if (key < ptr->key) ptr = ptr->left; else ptr = ptr->right; } return 1; } //Функции удаления int DeleteKey(struct biba *boba, int key) { struct Node *x; if (x = findDelete(boba, key)) { int i = hash(key); if (boba->tab[i].key == key) boba->tab[i].busy = -1; RB_Delete(x,boba); return 1; } else return 0; } struct Node *findDelete(struct biba *boba, int key) { int i = hash(key); struct Node *ptr=boba->root; if (boba->tab[i].key == key && boba->tab[i].busy == 1) return boba->tab[i].next; while (ptr != &Elist&&ptr != NULL) { if (ptr->key == key) return ptr; else if (key < ptr->key) ptr = ptr->left; else ptr = ptr->right; } return 0; } void RB_Delete(struct Node *x, struct biba *boba) { struct Node *y=&Elist, *p=&Elist; if (x->left == &Elist || x->right == &Elist) y = x; else y = successor(x,boba); if (y->left != &Elist) p = y->left; else p = y->right; p->parent = y->parent; if (y->parent == &Elist) boba->root = p; else if (y->parent->left == y) y->parent->left = p; else y->parent->right = p; if (y != x) { x->key = y->key; x->info = y->info; } if (y->color == 1) RB_Delete_Fixup(p, boba); } struct Node *successor(struct Node *x, struct biba *boba) { struct Node *ptr=&Elist; if (x->right != &Elist) { ptr = minRB(x->right,boba); return ptr; } else { ptr = x->parent; while (ptr != &Elist&&ptr->right == x) { x = ptr; ptr = x->parent; } return ptr; } } struct Node *minRB(struct Node *ptr, struct biba *boba) { if (ptr == &Elist) return &Elist; else { while (ptr->left != &Elist) ptr = ptr->left; return ptr; } } void RB_Delete_Fixup(struct Node *x, struct biba *boba) { struct Node *p, *w; while (x != boba->root && x->color == 1) { p = x->parent; if (x == p->left) {//начало w = p->right; if (w->color == 0) {//1 случай w->color = 1; p->color = 0; left_rotate(p, boba); w = p->right; }//1случай if (w->left->color == 1 && w->right->color == 1) {//2 случай w->color = 0; x = p; }//2 случай else { if (w->right->color == 1) {//3 случай w->color = 0; w->left->color = 1; right_rotate(w, boba); w = p->right; }//3 случай w->color = p->color; p->color = 1; w->right->color = 1; left_rotate(p, boba); x = boba->root; } }//конец else {//начало w = p->left; if (w->color == 0) {//1 случай w->color = 1; p->color = 0; right_rotate(p, boba); w = p->left; }//1случай if (w->right->color == 1 && w->left->color == 1) {//2 случай w->color = 0; x = p; }//2 случай else { if (w->left->color == 1) {//3 случай w->color = 0; w->right->color = 1; left_rotate(w, boba); w = p->left; }//3 случай w->color = p->color; p->color = 1; w->left->color = 1; right_rotate(p, boba); x = boba->root; } }//конец } x->color = 1; } /*void showRB(Node *ptr) { if (ptr != &Elist) { printf_s("Ключ %d Инфо %s цвет %d\n", ptr->key, ptr->info, ptr->color); showRB(ptr->left); showRB(ptr->right); } }*/ struct Node *find1(struct biba *boba, int key, int *p) { int i = hash(key); struct Node *ptr = boba->root; if (boba->tab[i].key == key&&boba->tab[i].busy == 1) { *p = 1; return boba->tab[i].next; } else while (ptr != &Elist&&ptr != NULL) { if (ptr->key == key) { *p = 0; boba->tab[i].key = key; boba->tab[i].next = ptr; return ptr; } else if (key < ptr->key) ptr = ptr->left; else ptr = ptr->right; } } struct Node *find2(struct biba *boba, int key) { int min; struct Node *ptr =boba->root; ptr = find3(boba->root, &min,key); return ptr; } struct Node *find3(struct Node *ptr, int *min,int key) { while (ptr != &Elist) { *min = abs(ptr->key - key); if (*min > abs(ptr->right->key - key)) { ptr = ptr->right; ptr = find3(ptr, min,key); } else { if (*min > abs(ptr->left->key - key)) { ptr = ptr->left; ptr = find3(ptr, min, key); } else return ptr; } } return ptr; } void putTree(struct Node *ptr, int lvl) { int i; if (ptr!=&Elist) { putTree(ptr->right, lvl + 1); for (i = 0; i < lvl; ++i) printf(" "); //printf_s("%d\n", ptr->key); printf("%d %s %d\n", ptr->key,ptr->info,ptr->color); putTree(ptr->left, lvl + 1); } }
Решение задачи: «Request for member Ошибка»
textual
Листинг программы
boba->tab[i]->key = 0;
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д