Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:07:42+00:00 2026-06-12T10:07:42+00:00

i am trying to make a queue library that is based on a linked

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:07:44+00:00Added an answer on June 12, 2026 at 10:07 am
    void add(queue currentQueue, int data){
    

    You are passing a copy of the queue struct to add, so only the copy’s members are changed. You need to pass a queue* to the function to be able to change the members of the queue itself.

    void add(queue *currentQueue, int data){
        if (currentQueue == NULL) {
            exit(EXIT_FAILURE);
        }
        addTail(currentQueue->list, data, data+5);
        currentQueue->back = currentQueue->back->next;
    }
    

    and call it as add(&your_queue);

    In your addTail function, you should check whether head is NULL too.

    And with

    node *newNode = (struct listNode *)malloc(sizeof(node));
    newNode = initNode(value, length);
    

    in addTail, you have a serious problem. With the assignment newNode = initNode(value, length);, you are losing the reference to the just malloced memory.

    If initNode mallocs a new chunk of memory, it’s “just” a memory leak, then you should remove the malloc in addTail.

    Otherwise, I fear initNode returns the address of a local variable, à la

    node * initNode(int val, int len) {
        node new;
        new.nodeValue = val;
        new.nodeLength = len;
        new.next = NULL;
        return &new;
    }
    

    If initNode looks 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, if initNode looked like that.

    Anyway, without seeing the code for initNode, I can’t diagnose the cause.

    But if you change your addTail to

    void addTail (node *head, int value, int length) {
        if (head == NULL) { // violation of contract, die loud
            exit(EXIT_FAILURE);
        }
        node *current = head;
        node *newNode = malloc(sizeof(node));
        if (newNode == NULL) {
            exit(EXIT_FAILURE); // or handle gracefully if possible
        }
        newNode->nodeValue = value;
        newNode->nodeLength = length;
        newNode->next = NULL;
    
        while (current->next != NULL)   
            current = current->next;
    
        current->next = newNode;
    }
    

    it 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 back pointer to append a new node,

    void add(queue *currentQueue, int data){
        node *newNode = malloc(sizeof *newNode);
        if (newNode == NULL) {
            exit(EXIT_FAILURE); // or handle gracefully if possible
        }
        newNode->nodeValue = data;
        newNode->nodeLength = data+5;
        newNode->next = NULL;
        currentQueue->back->next = newNode;
        currentQueue->back = newNode;
    }
    

    since you needn’t traverse the entire list to find the end.


    A simple sample programme

    #include <stdlib.h>
    #include <stdio.h>
    
    struct listNode {
        int nodeLength;
        int nodeValue;
        struct listNode *next;
    };
    
    typedef struct listNode node;
    
    struct QueueRecord {
        node *list;
        node *front;
        node *back;
        int maxLen;
    };
    typedef struct QueueRecord queue;
    
    node *createList (){
        node *head = NULL;
        head = (struct listNode *)malloc(sizeof(node));
        head->next = NULL;
        return head;
    }
    
    void addTail (node *head, int value, int length) {
        if (head == NULL) { // violation of contract, die loud
            exit(EXIT_FAILURE);
        }
        node *current = head;
        node *newNode = malloc(sizeof(node));
        if (newNode == NULL) {
            exit(EXIT_FAILURE); // or handle gracefully if possible
        }
        newNode->nodeValue = value;
        newNode->nodeLength = length;
        newNode->next = NULL;
    
        while (current->next != NULL)   
            current = current->next;
    
        current->next = newNode;
    }
    
    queue createQueue(int maxLen){
        queue newQueue;
        newQueue.list = createList();
        newQueue.front = newQueue.list;
        newQueue.back = newQueue.list;
        newQueue.maxLen = maxLen;
        return newQueue;
    }
    
    void add(queue *currentQueue, int data){
        if (currentQueue == NULL) {
            exit(EXIT_FAILURE);
        }
        addTail(currentQueue->list, data, data+5);
        currentQueue->back = currentQueue->back->next;
    }
    
    int main(void) {
        queue myQ = createQueue(10);
        for(int i = 1; i < 6; ++i) {
            add(&myQ, i);
            printf("list:  %p\nfront: %p\nback:  %p\n",
                    (void*)myQ.list, (void*)myQ.front, (void*)myQ.back);
        }
        node *curr = myQ.front->next;
        while(curr) {
            printf("Node %d %d, Back %d %d\n", curr->nodeValue,
                     curr->nodeLength, myQ.back->nodeValue, myQ.back->nodeLength);
            curr = curr->next;
        }
        while(myQ.list) {
            myQ.front = myQ.front->next;
            free(myQ.list);
            myQ.list = myQ.front;
        }
        return 0;
    }
    

    works as expected, also with the alternative add implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a linked list based queue for fast operations, now
I am trying to make a simple windows service that maintains a queue of
I am trying to make a priority queue of a class I made like
I'm currently trying to edit a project, that already uses OpenCL.dll, to make it
I'm having two unexpected problems using Queue from the STL Library: 1) I'm trying
I'm trying to make a queue struct that have push and pop functions. I
I'm trying to make a client that places messages into a queue on a
I'm trying to make a C# console app that adds messages to a queue.
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.