I have a doubly linked list with *head
and **ptail
. I wrote code to add to the list and remove from the list, but my problem is freeing the information.
This is the declaration of my node and my linked list:
struct tcb_t { //Node
int thread_id;
int thread_priority;
ucontext_t *thread_context;
struct tcb_t *next;
}; typedef struct tcb_t tcb_t;
struct queue_t { //Linked List
tcb_t *head, **ptail;
}; typedef struct queue_t queue_t;
This is my code for initializing a doubly linked list:
struct queue_t* queue_create() { //Good
struct queue_t *q = (queue_t *) calloc(1,sizeof(queue_t));
q->head = NULL;
q->ptail = &q->head; // problem
return q;
}
My problem stems from the following function. This function is intended to free all the nodes in the list, but the while loop is infinite. I think this is due to the tail pointing to the head in the linked list creation, but I am not sure if there is a way for me to fix it without rewriting the queue_create()
.
void t_shutdown() { //Fix
if(ready != NULL){
tcb_t *helper = ready->head;
while(helper->next != NULL){
tcb_t *temp = helper;
helper = helper->next;
if(temp->thread_id > 0){
free(temp->thread_context->uc_stack.ss_sp);
}
free(temp->thread_context);
free(temp);
}
free(ready);
}
ready = NULL;
}
I would like to loop through the list and free all the data but the helper->next
is ever NULL
.
Any help would be greatly appreciated.
Edit 1
These functions show how data is added and removed from the lists:
void queue_add(struct queue_t *q, tcb_t *ptr) { //Good
*q->ptail = ptr;
q->ptail = &ptr->next;
}
tcb_t *queue_remove(struct queue_t *q) { //Good
struct tcb_t *ptr = q->head;
if (ptr) {
q->head = ptr->next;
if (q->ptail == &ptr->next) {
q->head == NULL;
q->ptail = &q->head; // problem
}
}
return ptr;
}