Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
648d68f
Homework 9 - task 1 (WIP)
ilya-krivtsov Nov 23, 2024
5a7931c
Added hashtable expansion and iterator (hw9 task 1)
ilya-krivtsov Nov 25, 2024
b5af974
Added reading from file (hw9 task 1)
ilya-krivtsov Nov 25, 2024
d8bcd60
Removed debug printing and a comment (hw9 task 1)
ilya-krivtsov Nov 25, 2024
d0d52c5
Removed trailing space (hw9 task 1)
ilya-krivtsov Nov 25, 2024
0885885
Removed utf8 support -> now word is string without space characters (…
ilya-krivtsov Nov 25, 2024
107fb5d
Moved disposeIterator() right after iteration (hw9 task 1)
ilya-krivtsov Nov 25, 2024
fa6d971
Made ouput less ambiguous (hw9 task 1)
ilya-krivtsov Nov 25, 2024
93435d4
Removed strings count limit in ouput and added quotes for strings (hw…
ilya-krivtsov Nov 25, 2024
0256fde
Removed strings sorting (hw9 task 1)
ilya-krivtsov Nov 25, 2024
edf078f
Added comments (hw9 task 1)
ilya-krivtsov Nov 25, 2024
ab7841c
Merge branch 'main' into hw-9-task-1
ilya-krivtsov Nov 29, 2024
310433d
Removed getMinBucketLength (hw9 task 1)
ilya-krivtsov Nov 29, 2024
8484bad
Added getAverageBucketLength (hw9 task 1)
ilya-krivtsov Nov 29, 2024
ec9ddfe
Removed unnecessary brace (hw9 task 1)
ilya-krivtsov Nov 29, 2024
5b39b5a
Added stdbool.h include (hw9 task 1)
ilya-krivtsov Dec 10, 2024
f726695
Replaced stdio.h include with stdlib.h (hw9 task 1)
ilya-krivtsov Dec 10, 2024
ab17a10
Removed unused stdio.h include (hw9 task 1)
ilya-krivtsov Dec 10, 2024
1e549cd
Added null check in setString() (hw9 task 1)
ilya-krivtsov Dec 10, 2024
53e4329
Added README (hw9 task 1)
ilya-krivtsov Dec 25, 2024
581059a
Merge branch 'main' into hw-9-task-1
ilya-krivtsov Dec 25, 2024
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ add_subdirectory(homework_3)
add_subdirectory(homework_4)
add_subdirectory(homework_5)
add_subdirectory(homework_8)
add_subdirectory(homework_9)
5 changes: 5 additions & 0 deletions homework_9/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(homework_9)

set(homeworkName "${PROJECT_NAME}")

add_subdirectory(task_1)
3 changes: 3 additions & 0 deletions homework_9/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Homework 9

[Task 1. Hashtable](/homework_9/task_1)
1 change: 1 addition & 0 deletions homework_9/task_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
input.txt
9 changes: 9 additions & 0 deletions homework_9/task_1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project("${homeworkName}_task_1")

add_library(frequencyLookup frequencyList.c frequencyLookup.c)

add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} frequencyLookup)

add_executable(${PROJECT_NAME}_test test.c)
target_link_libraries(${PROJECT_NAME}_test frequencyLookup)
102 changes: 102 additions & 0 deletions homework_9/task_1/frequencyList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "frequencyList.h"

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

typedef struct FrequencyElement {
char *string;
int frequency;
FrequencyElement *next;
} FrequencyElement;

typedef struct FrequencyList {
FrequencyElement *first;
FrequencyElement *last;
int length;
} FrequencyList;

bool createFrequencyList(FrequencyList **list) {
*list = malloc(sizeof(FrequencyList));

if (*list == NULL) {
return false;
}

(*list)->first = NULL;
(*list)->last = NULL;
(*list)->length = 0;

return true;
}

FrequencyElement *getFirstElement(FrequencyList *list) {
return list->first;
}

FrequencyElement *addNewElement(FrequencyList *list) {
FrequencyElement *element = malloc(sizeof(FrequencyElement));
if (element == NULL) {
return NULL;
}

element->string = NULL;
element->frequency = 0;

element->next = NULL;
if (list->first == NULL) {
list->first = element;
list->last = element;
list->length = 1;
return element;
}

list->last->next = element;
list->last = element;

++list->length;

return element;
}

int getLength(FrequencyList *list) {
return list->length;
}

void disposeList(FrequencyList *list) {
FrequencyElement *element = list->first;
while (element != NULL) {
FrequencyElement *next = element->next;
free(element->string);
free(element);
element = next;
}

free(list);
}

FrequencyElement *getNextElement(FrequencyElement *element) {
return element->next;
}

const char *getString(FrequencyElement *element) {
return element->string;
}

int getFrequency(FrequencyElement *element) {
return element->frequency;
}

bool setString(FrequencyElement *element, const char *string) {
char *newString = strdup(string);
if (newString == NULL) {
return false;
}
free(element->string);
element->string = newString;
return true;
}

void setFrequency(FrequencyElement *element, int frequency) {
element->frequency = frequency;
}
58 changes: 58 additions & 0 deletions homework_9/task_1/frequencyList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <stdbool.h>

/// @brief Element of a `FrequencyList` that contains string and frequency
typedef struct FrequencyElement FrequencyElement;

/// @brief List where every element contains string and frequency
typedef struct FrequencyList FrequencyList;

/// @brief Creates `FrequencyList`
/// @param list Pointer to store `FrequencyList` to
/// @return `true` if created successfully, `false` otherwise (allocation failed)
bool createFrequencyList(FrequencyList **list);

/// @brief Gets first element of a list
/// @param list List to get first element from
/// @return Fist element, may be `NULL` if list is empty
FrequencyElement *getFirstElement(FrequencyList *list);

/// @brief Creates new element, adds it to list, and returns it
/// @param list List to add new element to
/// @return Created element
FrequencyElement *addNewElement(FrequencyList *list);

/// @brief Gets length of a list
/// @param list List to get lenght of
/// @return Length of the given list
int getLength(FrequencyList *list);

/// @brief Disposes list and all of its element
/// @param list List to dispose
void disposeList(FrequencyList *list);

/// @brief Gets next element of given element
/// @param element Element to get next element of
/// @return Element that follows given element, may be `NULL`
FrequencyElement *getNextElement(FrequencyElement *element);

/// @brief Gets string stored in an element
/// @param element Element to get string from
/// @return String that is stored in an element
const char *getString(FrequencyElement *element);

/// @brief Gets frequency stored in an element
/// @param element Element to get frequency from
/// @return Frequency that is stored in an element
int getFrequency(FrequencyElement *element);

/// @brief Sets string into an element
/// @param element Element to set string into
/// @param string String to set
bool setString(FrequencyElement *element, const char *string);

/// @brief Sets frequency into an element
/// @param element Element to set frequency into
/// @param frequency Frequency to set
void setFrequency(FrequencyElement *element, int frequency);
Loading