-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem29.c
More file actions
131 lines (110 loc) Β· 3.05 KB
/
problem29.c
File metadata and controls
131 lines (110 loc) Β· 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINE 500
// Definition for singly-linked list node
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
// Function to create a new node with a given value
ListNode* createNode(int val) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (!node) {
printf("!! Memory allocation failed !!\n");
exit(1);
}
node->val = val;
node->next = NULL;
return node;
}
// Append a new node at the end of the linked list
void appendNode(ListNode** headRef, int val) {
ListNode* newNode = createNode(val);
if (!*headRef) {
*headRef = newNode;
} else {
ListNode* temp = *headRef;
while (temp->next)
temp = temp->next;
temp->next = newNode;
}
}
// Function to read a line of space-separated digits and create a linked list
ListNode* readListFromLine(const char* label) {
char line[MAX_LINE];
ListNode* head = NULL;
int val;
printf("Enter %s: ", label);
if (!fgets(line, sizeof(line), stdin)) {
printf("!! Invalid Input !!\n");
exit(1);
}
char* token = strtok(line, " \n");
while (token != NULL) {
if (isdigit(token[0])) {
val = atoi(token);
if (val >= 0 && val <= 9) {
appendNode(&head, val);
} else {
printf("!! Invalid Input !!\n");
exit(1);
}
}
else {
printf("!! Invalid Input !!\n");
exit(1);
}
token = strtok(NULL, " \n");
}
return head;
}
// Function to add two numbers represented by reversed linked lists
ListNode* addTwoLists(ListNode* l1, ListNode* l2) {
ListNode *dummyHead = createNode(0), *current = dummyHead;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) { sum += l1->val; l1 = l1->next; }
if (l2) { sum += l2->val; l2 = l2->next; }
carry = sum / 10;
current->next = createNode(sum % 10);
current = current->next;
}
ListNode* result = dummyHead->next;
free(dummyHead); // Free unused dummy head
return result;
}
// Print linked list in readable format
void printList(ListNode* head) {
while (head) {
printf("%d", head->val);
if (head->next) printf(", ");
head = head->next;
}
}
// Free memory used by linked list
void freeList(ListNode* head) {
while (head) {
ListNode* temp = head;
head = head->next;
free(temp);
}
}
// Main function
int main() {
ListNode* l1 = readListFromLine("L1");
ListNode* l2 = readListFromLine("L2");
ListNode* result = addTwoLists(l1, l2);
printf("Sum of the two numbers (in reverse order): [");
printList(result);
printf("]\n");
// Clean up memory
freeList(l1);
freeList(l2);
freeList(result);
return 0;
}
// End of problem30.c
// This code implements a solution to the problem of adding two numbers represented by reversed linked lists.