Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct OneDirection{
- struct OneDirection *next;
- int key , value;
- };
- int At(int index ,struct OneDirection *HashTable){
- struct OneDirection *list = HashTable; //передаем таблицу с нужным ключом
- while(list != NULL){
- if(list->key == index)
- return list->value;
- else
- list = list->next;
- }
- return 0;
- }
- struct OneDirection *Assign(int index ,int value , struct OneDirection *HashTable){
- struct OneDirection *list = (struct OneDirection*)malloc(sizeof(struct OneDirection));
- list->next = HashTable;
- list->key = index;
- list->value = value;
- HashTable = list;
- return HashTable;
- }
- void freeList(struct OneDirection *list){
- struct OneDirection *t;
- while(list != NULL){ //ФУНКЦИЯ ОЧИСТКИ СПИСКОВ
- t = list;
- list = list->next;
- free(t);
- }
- }
- int main(){
- int count , size;
- scanf("%d\n%d" , &count , &size);
- struct OneDirection **HashTable = (struct OneDirection**)malloc(size * sizeof(struct OneDirection*));
- for (int i = 0; i < size; i++) HashTable[i] = NULL;
- for(int i = 0 ; i < count ; i++){ //Можно задавать массив разной длины, в зависимости от размера
- char word[10]; //сделал сразу большой
- scanf("%s" , word);
- if(word[1] == 'T'){
- int index;
- scanf("%d" , &index);
- int key = index % size;
- printf("%d\n" , At(index ,HashTable[key])); //передаем таблицу с нужным ключом
- }
- else{
- int value , index;
- scanf("%d %d" , &index , &value);
- int key = index % size;
- HashTable[key] = Assign(index , value , HashTable[key]);
- }
- }
- for(int i = 0 ; i < size ; i++)
- freeList(HashTable[i]);
- free(HashTable); //ОЧИСТКА ХЭШ-ТАБЛИЦЫ
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement