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

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

void append(struct node** head_ref, int new_data) {
    // Allocate memory for the new node and initialize its values
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = NULL;

    // If the list is empty, make the new node the head and return
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }

    // Traverse the list until the last node is found 
    struct node* last = *head_ref;// here the address of esd is assined to last
    while (last->next != NULL) { 
        last = last->next;
    }
    
    // Append the new node to the end of the list
    last->next = new_node;// here the original linked list element gets added
}

// Function to print the elements of a linked list
void printList(struct node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

int main() {
    struct node *a = NULL, *b = NULL, *c = NULL;
    int sum;

    // Populate linked list a
    append(&a, 5);
    append(&a, 6);
    append(&a, 7);

    // Populate linked list b
    append(&b, 7);
    append(&b, 3);
    append(&b, 4);

    // Sum the corresponding elements of a and b and store in c
    struct node* tmp_a = a;
    struct node* tmp_b = b;
    while (tmp_a != NULL && tmp_b != NULL) {
        append(&c, tmp_a->data + tmp_b->data);
        tmp_a = tmp_a->next;
        tmp_b = tmp_b->next;
    }

    // Print the resulting linked list c
    printf("Sum of a and b: ");
    printList(c);

    // Free allocated memory
    while (a != NULL) {
        struct node* temp = a;
        a = a->next;
        free(temp);
    }

    while (b != NULL) {
        struct node* temp = b;
        b = b->next;
        free(temp);
    }

    while (c != NULL) {
        struct node* temp = c;
        c = c->next;
        free(temp);
    }

    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: