C Programming Code Examples C > Linked Lists Code Examples Program to Add Corresponding Positioned Elements of 2 Linked Lists Program to Add Corresponding Positioned Elements of 2 Linked Lists #include <stdio.h> #include <stdlib.h> #include <ctype.h> struct node { int j; struct node *next; }; int feednumber(struct node **); struct node *addlist(struct node *, struct node *, int, int); void release(struct node **); void display(struct node *); int main() { struct node *p = NULL; struct node *q = NULL; struct node *res = NULL; int pcount = 0, qcount = 0; printf("Enter first number\n"); pcount = feednumber(&p); printf("Enter second number\n"); qcount = feednumber(&q); printf("Displaying list1: "); display(p); printf("Displaying list2: "); display(q); res = addlist(p, q, pcount, qcount); printf("Displaying the resulting list: "); display(res); release(&p); release(&q); release(&res); return 0; } /*Function to create nodes of numbers*/ int feednumber(struct node **head) { char ch, dig; int count = 0; struct node *temp, *rear = NULL; ch = getchar(); while (ch != '\n') { dig = atoi(&ch); temp = (struct node *)malloc(sizeof(struct node)); temp->j = dig; temp->next = NULL; count++; if ((*head) == NULL) { *head = temp; rear = temp; } else { rear->next = temp; rear = rear->next; } ch = getchar(); } return count; } /*Function to display the list of numbers*/ void display (struct node *head) { while (head != NULL) { printf("%d", head->j); head = head->next; } printf("\n"); } /*Function to free the allocated list of numbers*/ void release (struct node **head) { struct node *temp = *head; while ((*head) != NULL) { (*head) = (*head)->next; free(temp); temp = *head; } } /*Function to add the list of numbers and store them in 3rd list*/ struct node *addlist(struct node *p, struct node *q, int pcount, int qcount) { struct node *ptemp, *qtemp, *result = NULL, *temp; int x, carry = 0; while (pcount != 0 && qcount != 0) { ptemp = p; qtemp = q; for (x = 0; x < pcount - 1; x++) { ptemp = ptemp->next; } for (x = 0; x < qcount - 1; x++) { qtemp = qtemp->next; } temp = (struct node *) malloc (sizeof(struct node)); temp->j = ptemp->j + qtemp->j + carry; carry = temp->j / 10; temp->j = temp->j % 10; temp->next = result; result = temp; pcount--; qcount--; } /*both or one of the 2 lists have been read completely by now*/ while (pcount != 0) { ptemp = p; for (x = 0; x < pcount - 1; x++) { ptemp = ptemp->next; } temp = (struct node *) malloc (sizeof(struct node)); temp->j = ptemp->j + carry; carry = temp->j / 10; temp->j = temp->j % 10; temp->next = result; result = temp; pcount--; } while (qcount != 0) { qtemp = q; for (x = 0; x < qcount - 1; x++) { qtemp = qtemp->next; } temp = (struct node *) malloc (sizeof(struct node)); temp->j = qtemp->j + carry; carry = temp->j / 10; temp->j = temp->j % 10; temp->next = result; result = temp; qcount--; } return result; }