-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem28.c
More file actions
104 lines (89 loc) · 2.13 KB
/
problem28.c
File metadata and controls
104 lines (89 loc) · 2.13 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Node structure for the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Create a new node with the given value
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Reverse a linked list
Node* reverseList(Node* head) {
Node* prev = NULL;
while (head != NULL) {
Node* temp = head->next;
head->next = prev;
prev = head;
head = temp;
}
return prev;
}
// Double the number represented by the linked list
Node* doubleNumber(Node* head) {
head = reverseList(head);
Node* current = head;
int carry = 0;
Node* last = NULL;
while (current != NULL) {
int total = current->data * 2 + carry;
current->data = total % 10;
carry = total / 10;
last = current;
current = current->next;
}
if (carry > 0) {
last->next = createNode(carry);
}
return reverseList(head);
}
// Print the list in comma-separated format
void printList(Node* head) {
while (head != NULL) {
printf("%d", head->data);
if (head->next) {
printf(",");
}
head = head->next;
}
printf("\n");
}
// Free the memory used by the linked list
void freeList(Node* head) {
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
}
int main() {
char input[10005];
printf("Enter the number: ");
scanf("%s", input);
Node* head = NULL;
Node* tail = NULL;
// Build the linked list from input digits
for (int i = 0; input[i] != '\0'; ++i) {
Node* newNode = createNode(input[i] - '0');
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
Node* result = doubleNumber(head);
printList(result);
// Free memory to avoid leaks
freeList(head);
return 0;
}