i am trying to make a queue library that is based on a linked list library i already made. specifically i am having troubles updating the tail pointer in the queue structure after i add a new node to the linked list.
linked list structure:
struct listNode {
int nodeLength;
int nodeValue;
struct listNode *next;
};
typedef struct listNode node;
queue structure:
struct QueueRecord {
node *list;
node *front;
node *back;
int maxLen;
};
typedef struct QueueRecord queue;
so here is my add function in the queue library
void add(queue currentQueue, int data){
addTail(currentQueue.list, data, data+5);
currentQueue.back = currentQueue.back->next;
}
and the addTail function from the linked list library
void addTail (node *head, int value, int length) {
node *current = head;
node *newNode = (struct listNode *)malloc(sizeof(node));
newNode = initNode(value, length);
while (current->next != NULL)
current = current->next;
newNode->next = NULL;
current->next = newNode;
}
so again my problem is the tail pointer is not getting set to the last node in the list. it is remaining in the same place as the head pointer. ive been researching this for hours trying to see if im just missing something small but i cant find it. if more code or explanation is needed to understand my problem i can provide it.
how a queue is created:
queue createQueue(int maxLen){
queue newQueue;
newQueue.list = createList();
newQueue.front = newQueue.list;
newQueue.back = newQueue.list;
newQueue.maxLen = maxLen;
return newQueue;
}
node *createList (){
node *head = NULL;
head = (struct listNode *)malloc(sizeof(node));
head->next = NULL;
return head;
}
node *initNode (int value, int length){
node *newNode = NULL;
newNode = (struct listNode *)malloc(sizeof(node));
newNode->nodeValue = value;
newNode->nodeLength = length;
newNode->next = NULL;
return newNode;
}
You are passing a copy of the
queuestruct toadd, so only the copy’s members are changed. You need to pass aqueue*to the function to be able to change the members of thequeueitself.and call it as
add(&your_queue);In your
addTailfunction, you should check whetherheadisNULLtoo.And with
in
addTail, you have a serious problem. With the assignmentnewNode = initNode(value, length);, you are losing the reference to the justmalloced memory.If
initNodemallocs a new chunk of memory, it’s “just” a memory leak, then you should remove themallocinaddTail.Otherwise, I fear
initNodereturns the address of a local variable, à laIf
initNodelooks similar to that, that would cause a problem since the address becomes invalid as soon as the function returns. But your compiler should have warned you, ifinitNodelooked like that.Anyway, without seeing the code for
initNode, I can’t diagnose the cause.But if you change your
addTailtoit should work.
However, since you have pointers to the first and the last node in the list, it would be more efficient to use the
backpointer to append a new node,since you needn’t traverse the entire list to find the end.
A simple sample programme
works as expected, also with the alternative
addimplementation.