Program
/* Experiment No.14 Addition of Integers using Linked List
Write a program to add two positive integer numbers. The number is
represented as a linked list, with each node of the list holds one
digit of the [Link] the numbers in the form of string.
[Link] Pradeep.23/09/2024 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int data;
struct node *next;
};
struct node *push(struct node *head, int val){
struct node *temp = malloc(sizeof(struct node));
temp->data = val;
temp->next = head->next;
head->next = temp;
return head;
}
struct node *add(struct node *head1, struct node *head2){
if (head1->next->data == 0)
return head2;
if (head2->next->data == 0)
return head1;
struct node *ptr1 = head1->next;
struct node *ptr2 = head2->next;
// struct node *head3 = NULL;
struct node *head3 = (struct node *)malloc(sizeof(struct node));
head3->data = 0;
head3->next = NULL;
int carry = 0, sum;
while (ptr1 || ptr2){
sum = 0;
if (ptr1)
sum += ptr1->data;
if (ptr2)
sum += ptr2->data;
sum += carry;
carry = sum / 10;
sum = sum % 10;
head3 = push(head3, sum);
if (ptr1)
ptr1 = ptr1->next;
if (ptr2)
ptr2 = ptr2->next;
}
if (carry)
head3 = push(head3, carry);
return head3;
}
struct node *reverseLL(struct node *head)
{
if (head->next == NULL)
return head;
struct node *current = NULL;
struct node *next = head->next->next;
head->next->next = NULL;
while (next != NULL){
current = next;
next = next->next;
current->next = head->next;
head->next = current;
}
return head;
}
struct node *add_node(struct node *head, int val){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = val;
// newNode->next = NULL;
newNode->next = head->next;
head->next = newNode;
return head;
}
struct node *createLL(struct node *head, char n[]){
/*while (n != 0){
head = add_node(head, n % 10);
n = n / 10;
}*/
int i, x;
for (i = strlen(n) - 1; i >= 0; i--) {
x = n[i] - '0';
head = add_node(head, x);
}
return head;
}
void print(struct node *head){
struct node *temp = head->next;
if (head->next == NULL)
printf("No number.");
else{
while (temp->next != NULL){
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("%d", temp->data);
}
}
void back2num(struct node *head){
struct node *temp = head->next;
printf("\nResult = ");
while (temp){
printf("%d", temp->data);
temp = temp->next;
}
return;
}
int main(){
char a[20], b[20];
printf("Enter the two numbers: ");
scanf("%s %s", a, b);
printf("nexted List representation of first number: \n");
struct node *head1 = (struct node *)malloc(sizeof(struct node));
head1->data = 0;
head1->next = NULL;
head1 = createLL(head1, a);
print(head1);
// struct node *head2 = NULL;
struct node *head2 = (struct node *)malloc(sizeof(struct node));
head2->data = 0;
head2->next = NULL;
printf("\nnexted List representation of second number: \n");
head2 = createLL(head2, b);
print(head2);
head1 = reverseLL(head1);
head2 = reverseLL(head2);
printf("\nReversed nexted Lists: \n");
print(head1);
printf("\n");
print(head2);
// struct node *head3 = NULL;
struct node *head3 = (struct node *)malloc(sizeof(struct node));
head3->data = 0;
head3->next = NULL;
head3 = add(head1, head2);
printf("\nResultant nexted list after addition: \n");
print(head3);
back2num(head3);
}
Sample Output
Result
The program was executed and the output verified.