patterncMinor
Adding two BigInts using linked lists in C
Viewed 0 times
bigintsaddingtwousinglistslinked
Problem
While trying to learn more about linked lists, I thought I should try the exercise of reading two BigInts from an input file and then adding them up. My strategy was to store the two numbers in two different linked lists and then add the entries of the linked lists while traversing through them. I have my implementation down below and it will be great if somebody could review what I have done here. The current implementation is geared towards adding two numbers. It will be great if somebody could give me a few pointers on extending it for an arbitrary number of BigInts.
My "input.txt" is as follows:
And my implementation is as follows:
```
#include
#include
#include
typedef struct node{
int data;
struct node* next;
} LList;
LList pushToResult(LList resultList,int entry){
LList * temp = malloc(sizeof(LList));
temp -> data = entry;
temp -> next = resultList;
resultList = temp;
return resultList;
}
LList convertToList(LList list,char* line){
//Get the size of the string
//Create a new list
int i;
int strSize = strlen(line);
LList * temp = NULL;
for(i=0;i data = num - '0';
temp -> next = NULL;
}else{
LList * current = malloc(sizeof(LList));
current -> data = num - '0';
current -> next = temp;
temp = current;
}
}
list = temp;
return list;
}
void printList(LList * list){
LList * counter = list;
while( counter != NULL){
printf(" %d ",counter -> data);
counter = counter -> next;
}
printf("\n\n");
}
int main(){
//Read number 1 from the input file into a linked list
//The unit's place is the head and the mot significant number is the tail
//Read number 2 from the input file into a linked list
//The unit's place is the head and the most significant number is the tail
My "input.txt" is as follows:
65368023423432568512973371196238845129776544789456
553245642579324556736835497210698463523456234And my implementation is as follows:
```
#include
#include
#include
typedef struct node{
int data;
struct node* next;
} LList;
LList pushToResult(LList resultList,int entry){
LList * temp = malloc(sizeof(LList));
temp -> data = entry;
temp -> next = resultList;
resultList = temp;
return resultList;
}
LList convertToList(LList list,char* line){
//Get the size of the string
//Create a new list
int i;
int strSize = strlen(line);
LList * temp = NULL;
for(i=0;i data = num - '0';
temp -> next = NULL;
}else{
LList * current = malloc(sizeof(LList));
current -> data = num - '0';
current -> next = temp;
temp = current;
}
}
list = temp;
return list;
}
void printList(LList * list){
LList * counter = list;
while( counter != NULL){
printf(" %d ",counter -> data);
counter = counter -> next;
}
printf("\n\n");
}
int main(){
//Read number 1 from the input file into a linked list
//The unit's place is the head and the mot significant number is the tail
//Read number 2 from the input file into a linked list
//The unit's place is the head and the most significant number is the tail
Solution
Just two things:
-
In
-
You really should get into the habit of releasing memory once you no longer need it.
For each
-
(extra) Don't comment the obvious
This is OK though :)
-
In
convertToList() you don't really use the first argument at all. You can code that function with receiving only the (read-only) input string.LList *convertToList(const char *line) {
/* ... */
return temp;
}-
You really should get into the habit of releasing memory once you no longer need it.
For each
malloc() there should be a free(). This includes the result from getline().-
(extra) Don't comment the obvious
//Closing the input file
fclose(input);This is OK though :)
//Get the carry and the left over
int carryOver = carry / 10;
int leftOver = carry % 10;Code Snippets
LList *convertToList(const char *line) {
/* ... */
return temp;
}//Closing the input file
fclose(input);//Get the carry and the left over
int carryOver = carry / 10;
int leftOver = carry % 10;Context
StackExchange Code Review Q#44128, answer score: 4
Revisions (0)
No revisions yet.