Skip to content

Commit

Permalink
resolve issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-coding committed Jul 28, 2024
1 parent 585ff2d commit e4d4c27
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 142 deletions.
51 changes: 31 additions & 20 deletions code/logic/doublylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fossil_dlist_t* fossil_dlist_create(char* type) {
if (dlist) {
dlist->head = NULL;
dlist->tail = NULL;
dlist->type = type; // Assuming type is a static string or managed separately
dlist->type = type; // Ensure type is managed appropriately
}
return dlist;
}
Expand All @@ -32,30 +32,28 @@ void fossil_dlist_erase(fossil_dlist_t* dlist) {
fossil_dlist_node_t* current = dlist->head;
while (current) {
fossil_dlist_node_t* next = current->next;
fossil_tofu_free(current);
fossil_tofu_free(current); // Ensure deep free of node's data if necessary
current = next;
}
dlist->head = NULL;
dlist->tail = NULL;
fossil_tofu_free(dlist);
}

int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) {
if (!dlist) return -1;

fossil_dlist_node_t* new_node = (fossil_dlist_node_t*)fossil_tofu_alloc(sizeof(fossil_dlist_node_t));
if (!new_node) {
return -1; // Allocation failed
}

new_node->data = data;
new_node->data = data; // Consider deep copying data if necessary
new_node->prev = NULL;
new_node->next = NULL;

if (dlist->head == NULL) {
// Empty list case
if (dlist->tail == NULL) {
dlist->head = new_node;
dlist->tail = new_node;
} else {
// Non-empty list case
new_node->prev = dlist->tail;
dlist->tail->next = new_node;
dlist->tail = new_node;
Expand All @@ -70,17 +68,17 @@ int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) {
}

fossil_dlist_node_t* node_to_remove = dlist->tail;
if (!node_to_remove) return -1;

if (node_to_remove == dlist->head) {
// Only one node in the list
dlist->head = NULL;
dlist->tail = NULL;
} else {
dlist->tail = node_to_remove->prev;
dlist->tail->next = NULL;
}

*data = node_to_remove->data;
*data = node_to_remove->data; // Consider deep copy if necessary
fossil_tofu_free(node_to_remove);

return 0; // Success
Expand All @@ -98,37 +96,49 @@ int32_t fossil_dlist_search(const fossil_dlist_t* dlist, fossil_tofu_t data) {
}

void fossil_dlist_reverse_forward(fossil_dlist_t* dlist) {
if (!dlist) return;

fossil_dlist_node_t* current = dlist->head;
fossil_dlist_node_t* temp = NULL;

while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev; // Move to the next node
current = current->prev; // Move to the next node (which is actually previous in the original order)
}

// Swap head and tail
temp = dlist->head;
dlist->head = dlist->tail;
dlist->tail = temp;
if (temp) {
dlist->head = temp->prev;
}
dlist->tail = dlist->head;
while (dlist->tail && dlist->tail->next) {
dlist->tail = dlist->tail->next;
}
}

void fossil_dlist_reverse_backward(fossil_dlist_t* dlist) {
if (!dlist) return;

fossil_dlist_node_t* current = dlist->tail;
fossil_dlist_node_t* temp = NULL;

while (current) {
temp = current->next;
current->next = current->prev;
current->prev = temp;
current = current->next; // Move to the previous node
current = current->next; // Move to the next node (which is actually previous in the original order)
}

// Swap head and tail
temp = dlist->head;
if (temp) {
dlist->tail = temp->next;
}
dlist->head = dlist->tail;
dlist->tail = temp;
while (dlist->head && dlist->head->prev) {
dlist->head = dlist->head->prev;
}
}

size_t fossil_dlist_size(const fossil_dlist_t* dlist) {
Expand All @@ -145,7 +155,7 @@ fossil_tofu_t* fossil_dlist_getter(fossil_dlist_t* dlist, fossil_tofu_t data) {
fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return &(current->data); // Return pointer to found data
return &(current->data);
}
current = current->next;
}
Expand All @@ -156,7 +166,8 @@ int32_t fossil_dlist_setter(fossil_dlist_t* dlist, fossil_tofu_t data) {
fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
current->data = data; // Update data
// Ensure to free old data if it was dynamically allocated
current->data = data;
return 0; // Success
}
current = current->next;
Expand All @@ -178,4 +189,4 @@ bool fossil_dlist_is_empty(const fossil_dlist_t* dlist) {

bool fossil_dlist_is_cnullptr(const fossil_dlist_t* dlist) {
return (dlist == NULL);
}
}
6 changes: 4 additions & 2 deletions code/logic/dqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fossil_dqueue_t* fossil_dqueue_create(char* type) {
if (dqueue) {
dqueue->front = NULL;
dqueue->rear = NULL;
dqueue->type = type; // Assuming type is a static string or managed separately
dqueue->type = fossil_tofu_strdup(type); // Duplicate the type string to manage its memory
}
return dqueue;
}
Expand All @@ -37,6 +37,7 @@ void fossil_dqueue_erase(fossil_dqueue_t* dqueue) {
}
dqueue->front = NULL;
dqueue->rear = NULL;
fossil_tofu_free(dqueue->type); // Free the duplicated type string
fossil_tofu_free(dqueue);
}

Expand Down Expand Up @@ -110,7 +111,7 @@ fossil_tofu_t* fossil_dqueue_getter(fossil_dqueue_t* dqueue, fossil_tofu_t data)
fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return &(current->data); // Return pointer to found data
return &(current->data); // Return pointer to the found data
}
current = current->next;
}
Expand All @@ -121,6 +122,7 @@ int32_t fossil_dqueue_setter(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
// Assuming `data` should replace current->data
current->data = data; // Update data
return 0; // Success
}
Expand Down
19 changes: 10 additions & 9 deletions code/logic/fossil/tofu/tofu.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ typedef enum {

// Union for holding different types of values
typedef union {
int64_t int_val;
uint64_t uint_val;
double double_val;
float float_val;
char16_t *byte_string_val; // for byte string types
wchar_t *wide_string_val; // for wide string types
char *c_string_val; // for C string type
char char_val; // for char types
int64_t int_val; // for signed int types
uint64_t uint_val; // for unsigned int types
double double_val; // for double types
float float_val; // for float types
char16_t *uchar_string_val; // for byte string types
wchar_t *wchar_string_val; // for wide string types
char *cchar_string_val; // for C string type
char cchar_val; // for char types
wchar_t wchar_val; // for wide char types
char16_t byte_val; // for byte types
char16_t uchar_val; // for byte types
size_t size_val; // for size types
uint8_t bool_val; // for bool types
} fossil_tofu_value_t;

Expand Down
Loading

0 comments on commit e4d4c27

Please sign in to comment.