Skip to content

Commit

Permalink
Changed naming of dataSize to keep it more
Browse files Browse the repository at this point in the history
consistent with the rest of the library,
also changed documentation structure for LinkC
and LinkCNode
  • Loading branch information
kpluas21 committed May 7, 2023
1 parent 444aa22 commit 220f2e9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
36 changes: 18 additions & 18 deletions Data_Structures/LinkC/src/LinkC.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

static const char *status_string;

LinkC *LinkC_init(size_t dataSize, void *data) {
LinkC *LinkC_init(size_t data_size, void *data) {
LinkC *list = malloc(sizeof(LinkC));
LinkCNode *head = malloc(sizeof(LinkCNode));

Expand All @@ -27,16 +27,16 @@ LinkC *LinkC_init(size_t dataSize, void *data) {
return NULL;
}

head->data = malloc(dataSize);
head->data = malloc(data_size);

if(head->data == NULL) {
LinkC_error_report(E_OUT_OF_MEMORY);
return NULL;
}
memcpy(head->data, data, dataSize);
memcpy(head->data, data, data_size);

list->dataSize = dataSize;
list->alloc_Data = dataSize;
list->data_size = data_size;
list->alloc_Data = data_size;

list->head = head;
list->tail = head;
Expand All @@ -47,7 +47,7 @@ LinkC *LinkC_init(size_t dataSize, void *data) {
}

size_t LinkC_size(LinkC *list) {
return list->alloc_Data / list->dataSize;
return list->alloc_Data / list->data_size;
}

void LinkC_insert_at_end(LinkC *list, void* data) {
Expand All @@ -59,21 +59,21 @@ void LinkC_insert_at_end(LinkC *list, void* data) {
return;
}

newNode->data = malloc(list->dataSize);
newNode->data = malloc(list->data_size);
if(newNode->data == NULL) {
free(newNode);
LinkC_error_report(E_OUT_OF_MEMORY);
return;
}

memcpy(newNode->data, data, list->dataSize);
memcpy(newNode->data, data, list->data_size);

current->next = newNode;
newNode->prev = current;
newNode->next = NULL;
list->tail = newNode;

list->alloc_Data += list->dataSize;
list->alloc_Data += list->data_size;

}

Expand All @@ -96,7 +96,7 @@ int LinkC_find(LinkC *list, void *data) {
int index = 0;

while(current != NULL) {
if(memcmp(current->data, data, list->dataSize) == 0) {
if(memcmp(current->data, data, list->data_size) == 0) {
return index;
}
current = current->next;
Expand Down Expand Up @@ -163,7 +163,7 @@ void LinkC_remove_at_middle(LinkC *list, size_t index) {
free(temp->data);
free(temp);

list->alloc_Data -= list->dataSize;
list->alloc_Data -= list->data_size;

return;
}
Expand All @@ -188,7 +188,7 @@ void LinkC_remove_head(LinkC *list) {

}

list->alloc_Data -= list->dataSize;
list->alloc_Data -= list->data_size;
return;
}

Expand All @@ -209,7 +209,7 @@ void LinkC_remove_tail(LinkC *list) {
free(temp);
}

list->alloc_Data -= list->dataSize;
list->alloc_Data -= list->data_size;
return;
}

Expand All @@ -221,22 +221,22 @@ void LinkC_insert_at_start(LinkC *list, void *data)
return;
}

newNode->data = malloc(list->dataSize);
newNode->data = malloc(list->data_size);
if(newNode->data == NULL) {
free(newNode);
LinkC_error_report(E_OUT_OF_MEMORY);
return;
}

memcpy(newNode->data, data, list->dataSize);
memcpy(newNode->data, data, list->data_size);

newNode->next = list->head;
newNode->prev = NULL;

list->head->prev = newNode;
list->head = newNode;

list->alloc_Data += list->dataSize;
list->alloc_Data += list->data_size;
return;
}

Expand All @@ -262,14 +262,14 @@ void LinkC_insert_at_index(LinkC *list, void *data, size_t index) {
return;
}

newNode->data = malloc(list->dataSize);
newNode->data = malloc(list->data_size);
if(newNode->data == NULL) {
free(newNode);
LinkC_error_report(E_OUT_OF_MEMORY);
return;
}

memcpy(newNode->data, data, list->dataSize);
memcpy(newNode->data, data, list->data_size);

size_t currIndex = 0;
while(currIndex != index) {
Expand Down
43 changes: 28 additions & 15 deletions Data_Structures/LinkC/src/LinkC.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _LINKC_H
#define _LINKC_H
#ifndef LINKC_H
#define LINKC_H
/**
* @file LinkC.h
* @author Kevin Pluas ([email protected])
Expand Down Expand Up @@ -58,26 +58,39 @@ typedef struct LinkCNode LinkCNode;

/**
* @struct LinkC
* @brief The main struct that contains essential information about our linked list
* @param type The data type of the elements
* @param size The number of elements in our list
* @param head A pointer to the first node in the list
* @param tail A pointer to the last node in the list
* @brief The main object that users will use to interact with the linked list.
*
* @var LinkC::data_size
* The size, in bytes, each element is.
*
* @var LinkC::alloc_data
* The amount of bytes used by the elements overall. Used for determining the amount of elements.
*
* @var LinkC::head
* A pointer to the head, or the first element, of the linked list.
*
* @var LinkC::tail
* A pointer to the tail, or the last element, of the linked list.
*/
typedef struct LinkC {
size_t dataSize;
size_t data_size;
size_t alloc_Data;
LinkCNode *head;
LinkCNode *tail;
}LinkC;

/**
* @struct LinkCNode
* @brief The nodes that make up our linked list.
* @param prev A pointer to the previous node in the list. NULL if the head node.
* @param next A pointer to the next node in the list. NULL if the tail node.
* @param data A pointer to the data contained in this node.
* @brief The nodes that make up our linked list. These are considered the actual "elements" of the linked list.
*
* @var LinkCNode::prev
* A pointer to the previous node in the linked list
*
* @var LinkCNode::next
* A pointer to the next node in the linked list
*
* @var LinkCNode::data
* A pointer to the actual data contained in the node.
*
*/
typedef struct LinkCNode {
Expand All @@ -90,11 +103,11 @@ typedef struct LinkCNode {
/**
* @brief Initializes our linked list.
*
* @param dataSize The size, in bytes, of each elements.
* @param data_size The size, in bytes, of each elements.
* @param data A pointer to our initial data.
* @return LinkC*
*/
LinkC *LinkC_init(size_t dataSize, void *data);
LinkC *LinkC_init(size_t data_size, void *data);

/**
* @brief Returns the number of elements in the array
Expand Down Expand Up @@ -186,4 +199,4 @@ void LinkC_insert_at_index(LinkC *list, void *data, size_t index);
*/
void LinkC_error_report(ErrorCode code);

#endif //_LINK_C
#endif //LINK_C

0 comments on commit 220f2e9

Please sign in to comment.