/
Messus
/
Algorithms
Обзор
Документация
Войти
/
Messus
/
Algorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ai_set.c
166 строк
4 KB
Messus
utf-8
03 мар 2026, 11:45
03 мар 2026, 11:45
67a2dd3
Код
Авторство
О чём код?
//ai_set.c --- структура сет сгенерированная ИИ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <locale.h> #include <windows.h> #define TABLE_SIZE 100 // размер хеш-таблицы // Узел списка для разрешения коллизий typedef struct Node { int data; struct Node* next; } Node; // Структура сета (хеш-таблица) typedef struct { Node* table[TABLE_SIZE]; } Set; // Хеш-функция int hash(int key) { return abs(key) % TABLE_SIZE; } // Создание нового узла Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { fprintf(stderr, "Ошибка выделения памяти\n"); return NULL; } newNode->data = data; newNode->next = NULL; return newNode; } // Инициализация сета Set* createSet() { Set* set = (Set*)malloc(sizeof(Set)); if (set == NULL) { fprintf(stderr, "Ошибка выделения памяти для сета\n"); return NULL; } for (int i = 0; i < TABLE_SIZE; i++) { set->table[i] = NULL; } return set; } // Добавление элемента bool add(Set* set, int data) { int index = hash(data); Node* current = set->table[index]; while (current != NULL) { if (current->data == data) { return false; // элемент уже есть } current = current->next; } Node* newNode = createNode(data); if (newNode == NULL) return false; newNode->next = set->table[index]; set->table[index] = newNode; return true; } // Проверка наличия элемента bool contains(Set* set, int data) { int index = hash(data); Node* current = set->table[index]; while (current != NULL) { if (current->data == data) { return true; } current = current->next; } return false; } // Удаление элемента bool set_remove(Set* set, int data) { int index = hash(data); Node* current = set->table[index]; Node* prev = NULL; while (current != NULL && current->data != data) { prev = current; current = current->next; } if (current == NULL) { return false; // элемента нет } if (prev == NULL) { set->table[index] = current->next; // удаляем первый элемент списка } else { prev->next = current->next; // удаляем элемент из середины/конца } free(current); return true; } // Вывод всех элементов сета void printSet(Set* set) { printf("Элементы сета:\n"); for (int i = 0; i < TABLE_SIZE; i++) { Node* current = set->table[i]; while (current != NULL) { printf("%d ", current->data); current = current->next; } } printf("\n"); } // Освобождение памяти void freeSet(Set* set) { for (int i = 0; i < TABLE_SIZE; i++) { Node* current = set->table[i]; while (current != NULL) { Node* temp = current; current = current->next; free(temp); } } free(set); } // Тестовый блок int main() { setlocale(LC_ALL, ""); SetConsoleCP(1251); SetConsoleOutputCP(1251); // Создаём новый сет Set* mySet = createSet(); if (mySet == NULL) { return 1; } add(mySet, 10); add(mySet, 20); add(mySet, 30); add(mySet, 40); printSet(mySet); set_remove(mySet, 20); printSet(mySet); add(mySet, 30); set_remove(mySet, 25); freeSet(mySet); }