Thread: Inserting a new node at the end of a linked-list

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    5

    Question Inserting a new node at the end of a linked-list

    Hi everyone!

    I'm trying to write a program that lets the user append nodes at the end of a linked list. It kinda works but the main problem is that it doesn't actually create any new nodes later on, the function I've written creates one new node and then just keeps on changing its values over and over again.

    Code:
    static void append(Node *_tail, unsigned _id) {
        Node temp = { .id = _id, .next = NULL };
    
    
        if (_tail == NULL) {
            _tail = &temp;
        } else {
            _tail->next = &temp;
            _tail = &temp;
        }
    }
    I've also tried dynamically allocating memory for the nodes with malloc() and calloc() but that gave me the same results. What am I doing wrong?

    Here's the main() function if you were wondering.

    Code:
    int main(void) {
        unsigned input;
        Node n3 = { .id = 3, .next = NULL };
        Node n2 = { .id = 2, .next = &n3 };
        Node n1 = { .id = 1, .next = &n2 };
        Node *head = &n1, *tail = &n3;
    
    
        for (;;) {
            printf("Enter an ID: "), scanf("%u", &input);
    
    
            append(tail, input);
    
    
            for (Node *i = head; i != NULL; i = i->next) {
                printf("ID: %u\n", i->id);
            }
        }
    
    
        return 0;
    }
    Last edited by bld; 4 Weeks Ago at 12:48 PM. Reason: Formatting

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,291
    This is wrong because you have a local variable for the new local: that variable ceases to exist after the function returns. You could make it static, but then all you'll ever have is one variable, so you'll never be able to grow the linked list.

    A common solution is to use dynamic memory allocation, i.e., your attempt with malloc and free, but you probably just used it wrongly. I suggest posting your attempt for that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2020
    Posts
    5
    Quote Originally Posted by laserlight View Post
    I suggest posting your attempt for that.
    I'm sorry for the delayed reply. Anyways, here it is!

    Code:
    static Node *getLast(Node *_head) {
        Node *i = NULL;
    
    
        for (i = _head; i != NULL; i = i->next);
    
    
        return i;
    }
    
    
    static void append(Node *_head, unsigned _id) {
        Node *temp = (Node*)calloc(1, sizeof(Node));
        temp->id = _id;
        temp->next = NULL;
    
    
        if (_head == NULL) {
            _head = temp;
        } else {
            _head = getLast(_head);
            _head->next = temp;
        }
    }
    And here's main()

    Code:
    int main(void) {
        unsigned input;
        Node *head = NULL;
    
    
        for (;;) {
            printf("Enter an ID: "), scanf("%u", &input);
    
    
            append(head, input);
    
    
            for (Node *i = head; i != NULL; i = i->next) {
                printf("ID: %u\n", i->id);
            }
        }
    
    
        return 0;
    }
    This program doesn't even print the list. I might be iterating over the list wrongly, but I dunno.
    Last edited by bld; 3 Weeks Ago at 02:45 PM. Reason: I had to add some stuff

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,085
    Your append function cannot modify main's local copy of head.
    Also, your getLast always returns NULL.

    For append, you can return head from it and call it like:
    Code:
        head = append(head, id);  // Node *append(Node *head, int id);
    Or you can pass in a pointer to head so it can be directly manipulated.
    Code:
        append(&head, id);        // void append(Node **phead, int id);
    For getLast,
    Code:
    Node *getLast(Node *nd) {
        if (nd) for ( ; nd->next; nd = nd->next) ;
        return nd;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a node at the tail of singly linked list
    By ghoul in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2020, 07:08 AM
  2. How to Inserting a new node in a linked list
    By abhi143 in forum C Programming
    Replies: 3
    Last Post: 11-07-2019, 01:15 PM
  3. Problem in output of code(Inserting node in linked list)
    By Kunal1997 in forum C Programming
    Replies: 3
    Last Post: 09-03-2016, 01:37 AM
  4. Two Question About Inserting Node In the Linked List
    By hefese in forum C Programming
    Replies: 10
    Last Post: 08-07-2012, 12:24 PM
  5. Replies: 7
    Last Post: 11-04-2010, 01:18 PM

Tags for this Thread