Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- #include <math.h>
- #include <limits.h>
- #define SIZE 3
- typedef
- struct element
- {
- char* key;
- int value;
- struct element* next;
- }element;
- element** mas;
- void storage()
- {
- mas = (element**)calloc(SIZE, sizeof(element*));
- }
- int del(char* key)
- {
- _Bool ans = 0;
- unsigned int index = hashFunc(key) % SIZE;
- if (strcmp(mas[index]->key, key) == 0)
- {
- scanf("%i", &ans);
- if (ans == 1)
- if (mas[index]->next == NULL)
- return NULL;
- else
- {
- element* temp = mas[index];
- do
- {
- if (strcmp(temp->next->key, key) == 0)
- {
- scanf("%i", &ans);
- if (ans == 1)
- {
- if (temp->next->next == NULL)
- {
- temp->next = NULL;
- return NULL;
- }
- else
- {
- element* temp2;
- do
- {
- if (temp->next == NULL)
- {
- temp = NULL;
- return;
- }
- else
- {
- temp2 = temp->next;
- temp->next = temp->next->next;
- temp->next->next = temp2;
- }
- temp2 = temp2->next;
- } while (1);
- }
- }
- }
- temp = temp->next;
- }while (1);
- }
- }
- else
- {
- element* temp = mas[index];
- do
- {
- if (strcmp(temp->next->key, key) == 0)
- {
- scanf("%i", &ans);
- if (ans == 1)
- {
- if (temp->next->next == NULL)
- {
- temp->next = NULL;
- return NULL;
- }
- else
- {
- element* temp2;
- do
- {
- temp->next = temp->next->next;
- temp2 = temp->next->next;
- if (temp2->next == NULL)
- return NULL;
- temp2 = temp2->next;
- } while (1);
- }
- }
- }
- temp = temp->next;
- } while (1);
- }
- }
- element* search(char* key)
- {
- unsigned int index = hashFunc(key) % SIZE;
- if (strcmp(mas[index]->key, key) == 0)
- {
- return mas[index];
- }else
- {
- element* temp = mas[index];
- while (1)
- {
- if (strcmp(temp->next->key, key) == 0)
- {
- return temp->next;
- }
- if (temp->next == NULL)
- return NULL;
- temp = temp->next;
- }
- }
- return mas[index];
- }
- void push(const char *key, const int value)
- {
- unsigned int index = hashFunc(key) % SIZE;
- if (mas[index] == 0)
- {
- mas[index] = (element*)calloc(1,sizeof(element));
- mas[index]->key = (char*)calloc(strlen(key) + 1, sizeof(char));
- strcpy(mas[index]->key, key);
- mas[index]->value = value;
- }
- else
- {
- element* temp = mas[index];
- while (1)
- {
- if (strcmp(temp->key, key) == 0)
- {
- temp->value = value;
- return;
- }
- if (temp->next == NULL)
- break;
- temp = temp->next;
- }
- temp->next = (element*)calloc(1, sizeof(element));
- temp->next->key = (char*)calloc(strlen(key) + 1, sizeof(char));
- strcpy(temp->next->key, key);
- temp->next->value = value;
- }
- }
- unsigned int hashFunc(const char *key)
- {
- int index = 0, i = 0, k = 63;
- while (key[i] != '\0')
- {
- index = index * k + key[i];
- i++;
- }
- return index;
- }
- int main()
- {
- storage();
- push("apple", 100);
- push("hello", 150);
- push("student", 30);
- element *a = search("student");
- printf("%s - %d", a->key, a->value);
- del("student");
- /*if (a)
- printf("%s - %d", a->key, a->value);*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement