Home > Software design >  How do I print all the nodes in a linked list?
How do I print all the nodes in a linked list?

Time:11-15

I am trying to teach myself linked lists, so I have managed to put together a small piece of code that should create three linked nodes and then print them out. Except it only prints out the first element, and I don't understand why not the other two.

Also, I am pretty sure I am supposed to free memory when I use malloc? but I don't know where?

Anyway, what am I doing wrong?? here is the code...

#include<stdio.h>
#include <stdlib.h>

struct Node 
{
    int data;
    struct Node *next;
};

void printList(struct Node *ptr);

int main(void)
{
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 10;
    head->next = second;

    second->data = 20;
    head->next = third;

    third->data = 30;
    head->next = NULL;
    
    printList(head);
}

void printList(struct Node *ptr)
{
    struct Node *listPtr;
    listPtr = ptr;
    int count = 1;
    if (listPtr == NULL)
    {
        printf("No elements in list.\n");
        return;
    }  
    while (listPtr!=NULL)
    {
        printf("element %d = %d\n",count,listPtr->data);
        listPtr = listPtr->next;
        count  ;
    }
}

I have looked into similar code examples, and they (at least a couple of them), look similar to mine, so I don't really know what I am doing wrong...

CodePudding user response:

write this instead of what you did:

second->data = 20; //same
second->next = third;

third->data = 30; //same
third->next = NULL;

CodePudding user response:

printList() is fine. The problem is how you initialize the elements in main(). @ShlomiAgiv gave you one answer and here is another:

int main(void) {
    struct Node nodes[] = {
        { 10, nodes   1 },
        { 20, nodes   2 },
        { 30, NULL }
    };
    printList(nodes);
}

which returns:

element 1 = 10
element 2 = 20
element 3 = 30

I also wrote you a createList() functions that does the same via a dynamically allocated array:

struct Node *createList(size_t n, int data[n]) {
    struct Node *root, = malloc(n * sizeof(*nodes));
    if(!nodes) return NULL;
    for(size_t i = 0; i < n; i  ) {
        nodes[i].data = data[i];
        nodes[i].next = i   1 < n ? nodes   i   1 : NULL;
    }
    return nodes;
}

int main(void) {
    struct Node *nodes = createList(3, (int []) {10, 20, 30});
    printList(nodes);
    free(nodes);
}

If you want to grow your list incrementally, then you want to allocate each node individually, and keep track of both the head (for printList()) and tail for constant time appendList():

struct Node *appendList(struct Node *tail, int data) {
    struct Node *n = malloc(sizeof *n);
    if(!n)
        return NULL;
    n->data = data;
    n->next = NULL;
    if(tail)
        tail->next = n;
    return n;
}

int main(void) {
    struct Node *root;
    struct Node *tail = NULL;
    root = \
    tail = appendList(tail, 10);
    tail = appendList(tail, 20);
    tail = appendList(tail, 30);
    printList(root);
    // freeList(root);
}
  • Related