Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions hw10/hashTable/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
In the world of technology, innovation drives progress. Technology has become an integral part of our daily lives, shaping the way
we work, communicate, and entertain ourselves. The rapid growth of technology has brought both opportunities and challenges. For
businesses, technology offers tools to improve efficiency and reach new markets. For individuals, technology provides convenience
and access to knowledge.
However, the dependence on technology has also raised concerns. Privacy is a major issue in the digital age, as technology collects
vast amounts of personal data. Cybersecurity has become a pressing concern, with hackers targeting individuals and organizations
alike. Despite these challenges, the benefits of technology continue to outweigh its drawbacks.
Education, for example, has been transformed by technology. Online learning platforms, virtual classrooms, and educational apps
have made knowledge accessible to millions worldwide. At the same time, technology fosters creativity by enabling people to
design, innovate, and share their ideas with a global audience.
Yet, as technology advances, it is crucial to ensure that it serves humanity's best interests. Ethical considerations must guide
the development and use of technology, ensuring that it benefits society as a whole. In the end, technology is a tool, and
its impact depends on how it is used.
169 changes: 169 additions & 0 deletions hw10/hashTable/hashTable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#include "hashTable.h"
#include "list.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct HashTable {
int size;
int number;
List** buckets;
} HashTable;

HashTable* createTable(int size) {
HashTable* table = malloc(sizeof(HashTable));
if (table == NULL) {
return NULL;
}
table->size = size;
table->number = 0;
table->buckets = malloc(size * sizeof(List*));
if (table->buckets == NULL) {
free(table);
return NULL;
}
for (int i = 0; i < size; ++i) {
table->buckets[i] = createList();
}
return table;
}

int hash(char* key, int size) {
int hash = 0;
for (int i = 0; i < strlen(key); ++i) {
hash = ((hash * 31) * 101 + key[i]) % size;
}
return hash;
}

void insert(HashTable* table, char* key) {
if (table == NULL) {
return;
}
int index = hash(key, table->size);

List* bucket = table->buckets[index];
ListElement* current = getFirst(bucket);
while (current != NULL) {
if (strcmp(getKey(current), key) == 0) {
setCount(current, 1);
return;
}
current = getNext(current);
}
addLast(bucket, key);
table->number++;
}

double getFillFactor(HashTable* table) {
if (table == NULL) {
return 0;
}
if (table->number == 0) {
printf("HashTable is empty :( ");
return 0;
}
double res = (double)table->number / (double)table->size;
return res;
}

int getNumberOfNonEmptyLists(HashTable* table) {
if (table == NULL) {
return 0;
}
int numOfNon = 0;
for (int i = 0; i < table->size; ++i) {
if (getFirst(table->buckets[i]) != NULL) {
++numOfNon;
}
}
return numOfNon;
}

float averageLength(HashTable* table) {
if (table == NULL) {
return 0;
}
int lengthOfNonEmptyLists = 0;
int NonEmptyLists = getNumberOfNonEmptyLists(table);
for (int i = 0; i < table->size; ++i) {
lengthOfNonEmptyLists += getListLength(table->buckets[i]);
}
return (float)lengthOfNonEmptyLists / (float)NonEmptyLists;
}

int findingTheMaximumLength(HashTable* table) {
int max = 0;
for (int i = 0; i < table->size; ++i) {
int tmp = getListLength(table->buckets[i]);
if (max < tmp) {
max = tmp;
}
}
return max;
}

void cleanWord(char* word) {
int index = 0;
for (int i = 0; word[i] != '\0'; ++i) {
if (isalnum(word[i])) {
word[index] = word[i];
index++;
}
}
word[index] = '\0';
}

void readAndPrint(const char* filename, HashTable* table) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
return;
}
char word[256] = "";
while (fscanf(file, "%255s", word) == 1) {
cleanWord(word);
insert(table, word);
}
fclose(file);
printf("=============\nWord frequency: \n");
for (int i = 0; i < table->size; ++i) {
List* bucket = table->buckets[i];
ListElement* element = getFirst(bucket);
while (element != NULL) {
printf("%s : %d \n", getKey(element), getCount(element));
element = getNext(element);
}
}
}

void removeTable(HashTable* table) {
if (table == NULL) {
return;
}
for (int i = 0; i < table->size; ++i) {
if (table->buckets[i] != NULL) {
removeList(table->buckets[i]);
}
}
free(table->buckets);
free(table);
}

bool searchInBuckets(HashTable* table, const char* key) {
if (table == NULL) {
printf("Table is NULL\n");
return false;
}
for (int i = 0; i < table->size; ++i) {
ListElement* current = getFirst(table->buckets[i]);
while (current != NULL) {
if (strcmp(key, getKey(current)) == 0) {
return true;
}
current = getNext(current);
}
}
return false;
}
38 changes: 38 additions & 0 deletions hw10/hashTable/hashTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <stdbool.h>

typedef struct HashTable HashTable;

//function that creates a hash table
HashTable* createTable(int size);

//hash function
int hash(char* key, int size);

//function for inserting an item into a hash table
void insert(HashTable* table, char* key);

//function to get hash table fill factor
double getFillFactor(HashTable* table);

//function for obtaining the number of non - empty lists
int getNumberOfNonEmptyLists(HashTable* table);

//function for obtaining the average list length
float averageLength(HashTable* table);

//function for obtaining the value of the maximum list length
int findingTheMaximumLength(HashTable* table);

//function to read a file and output words with their frequency
void readAndPrint(const char* filename, HashTable* table);

//function for deleting a hash table
void removeTable(HashTable* table);

//function for finding the key among the buckets
bool searchInBuckets(HashTable* table, const char* key);

//function for deleting characters after a word
void cleanWord(char* word);
31 changes: 31 additions & 0 deletions hw10/hashTable/hashTable.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35222.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hashTable", "hashTable.vcxproj", "{37711D77-9B18-48A0-A4FB-22E25701AB82}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Debug|x64.ActiveCfg = Debug|x64
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Debug|x64.Build.0 = Debug|x64
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Debug|x86.ActiveCfg = Debug|Win32
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Debug|x86.Build.0 = Debug|Win32
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Release|x64.ActiveCfg = Release|x64
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Release|x64.Build.0 = Release|x64
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Release|x86.ActiveCfg = Release|Win32
{37711D77-9B18-48A0-A4FB-22E25701AB82}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4CED467D-CC5F-432E-B496-90FC22796120}
EndGlobalSection
EndGlobal
Loading