Linked List Fixes #11
@@ -12,12 +12,18 @@ typedef struct Node {
|
||||
void *data;
|
||||
} Node;
|
||||
|
||||
List listCreate() {
|
||||
List new_list;
|
||||
typedef struct List {
|
||||
Node *cur_head;
|
||||
Node *cur_tail;
|
||||
u64 cur_size;
|
||||
} List;
|
||||
|
||||
new_list.cur_size = 0;
|
||||
new_list.cur_head = NULL;
|
||||
new_list.cur_tail = NULL;
|
||||
List *listCreate() {
|
||||
List *new_list = malloc(sizeof(List));
|
||||
|
||||
new_list->cur_size = 0;
|
||||
new_list->cur_head = NULL;
|
||||
new_list->cur_tail = NULL;
|
||||
|
||||
return new_list;
|
||||
}
|
||||
@@ -29,34 +35,26 @@ void listDestroy(List *list) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 list_size = list->cur_size;
|
||||
Node *del_node = list->cur_tail;
|
||||
Node *next_node = list->cur_tail->prev;
|
||||
Node *next_node;
|
||||
|
||||
while (list_size > 0) {
|
||||
// First we want to free del_node
|
||||
while (del_node != NULL) {
|
||||
// We want to set del_node to the next_node to free
|
||||
next_node = del_node->prev;
|
||||
|
||||
// Then we want to free del_node
|
||||
free(del_node);
|
||||
|
||||
// Now we want to set del_node to the next_node to free
|
||||
// Finally we want to set del_node to the next_node to free
|
||||
del_node = next_node;
|
||||
|
||||
// Now we set next_node equal to the del_node's previous node
|
||||
if (next_node->prev != NULL) {
|
||||
next_node = next_node->prev;
|
||||
} else if (next_node->prev == NULL) {
|
||||
next_node->next = NULL;
|
||||
}
|
||||
|
||||
// Now we decrement list_size
|
||||
list_size--;
|
||||
}
|
||||
|
||||
// Now we null out the cur_head and cur_tail of our list
|
||||
list->cur_tail = NULL;
|
||||
list->cur_head = NULL;
|
||||
|
||||
// Now we null out the list and make it point to nothing
|
||||
list = NULL;
|
||||
// Now we free out the list and make it point to nothing
|
||||
free(list);
|
||||
}
|
||||
|
||||
u64 listSize(const List *list) {
|
||||
@@ -244,6 +242,16 @@ void listInsertAt(List *list, u64 pos, void *data) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we passed in a valid position
|
||||
if (pos < 1 || pos > list->cur_size) {
|
||||
// If we have elements in the list
|
||||
if (list->cur_size != 0) {
|
||||
// Then the position is actually out of bounds, so return.
|
||||
QERROR("Position given is out of range: %" PRId64, pos);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
u64 counter = 1;
|
||||
Node *insert_pos;
|
||||
Node *prev_node;
|
||||
|
||||
@@ -2,15 +2,9 @@
|
||||
|
||||
#include "../defines.h"
|
||||
|
||||
typedef struct Node Node;
|
||||
typedef struct List List;
|
||||
|
||||
typedef struct List {
|
||||
Node *cur_head;
|
||||
Node *cur_tail;
|
||||
u64 cur_size;
|
||||
} List;
|
||||
|
||||
List listCreate();
|
||||
List *listCreate();
|
||||
void listDestroy(List *list);
|
||||
u64 listSize(const List *list);
|
||||
void *listPeek(List *list);
|
||||
|
||||
@@ -14,47 +14,46 @@ int main() {
|
||||
QINFO("Size of f64: %lu", sizeof(f64));
|
||||
|
||||
QINFO("Creating a List...");
|
||||
List my_list = listCreate();
|
||||
List *my_list = listCreate();
|
||||
QINFO("Pushing to my_list...")
|
||||
i32 pushVal = 42;
|
||||
listPush(&my_list, &pushVal);
|
||||
i32 peekVal = *(i32 *)listPeek(&my_list);
|
||||
listPush(my_list, &pushVal);
|
||||
i32 peekVal = *(i32 *)listPeek(my_list);
|
||||
|
||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
||||
|
||||
QINFO("Pushing another number to my_list...");
|
||||
i32 pushVal2 = 169420;
|
||||
listPush(&my_list, &pushVal2);
|
||||
peekVal = *(i32 *)listPeek(&my_list);
|
||||
listPush(my_list, &pushVal2);
|
||||
peekVal = *(i32 *)listPeek(my_list);
|
||||
|
||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
||||
|
||||
i32 pushVal3 = 69;
|
||||
listPush(&my_list, &pushVal3);
|
||||
listPush(my_list, &pushVal3);
|
||||
QDEBUG("Peeking a 3rd value off of my_list: %" PRId32, peekVal);
|
||||
|
||||
i32 popVal = *(i32 *)listPop(&my_list);
|
||||
i32 popVal = *(i32 *)listPop(my_list);
|
||||
QDEBUG("Popped a value off of my_list: %" PRId32, popVal);
|
||||
|
||||
peekVal = *(i32 *)listPeek(&my_list);
|
||||
peekVal = *(i32 *)listPeek(my_list);
|
||||
|
||||
QDEBUG("Peeking value off of my_list: %" PRId32, peekVal);
|
||||
|
||||
i32 insertValue = 80085;
|
||||
|
||||
QINFO("Inserting a value at position 2.");
|
||||
listInsertAt(&my_list, 2, &insertValue);
|
||||
listInsertAt(my_list, 2, &insertValue);
|
||||
|
||||
QDEBUG("Data at position 2 is: %" PRId32, *(i32 *)listDataAt(&my_list, 2));
|
||||
QDEBUG("Data at position 2 is: %" PRId32, *(i32 *)listDataAt(my_list, 2));
|
||||
|
||||
QINFO("Removing a value at position 2.");
|
||||
listRemoveAt(&my_list, 2);
|
||||
listRemoveAt(my_list, 2);
|
||||
|
||||
QDEBUG("Data at position 2 is now: %" PRId32,
|
||||
*(i32 *)listDataAt(&my_list, 2));
|
||||
QDEBUG("Data at position 2 is now: %" PRId32, *(i32 *)listDataAt(my_list, 2));
|
||||
|
||||
QINFO("Destorying List...");
|
||||
listDestroy(&my_list);
|
||||
listDestroy(my_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user