patterncMinor
Adding two BigIntegers by putting mutiple digits in a linked list
Viewed 0 times
digitsaddingtwoputtingbigintegerslistlinkedmutiple
Problem
Based on feedback on an earlier question, I decided to implement the addition of two big ints by putting multiple digits in a node of a linked list. Can somebody take a look at my code and let me know if I am on the right track.
```
#include
#include
#include
typedef struct node{
int data;
struct node* next;
} LList;
void printList(LList * list);
LList pushToResult(LList resultList,int entry){
LList * temp = malloc(sizeof(LList));
temp -> data = entry;
temp -> next = resultList;
resultList = temp;
return resultList;
}
char substr(char const input,size_t start,size_t len){
char *nstr = malloc(len+1);
memcpy(nstr,input+start,len);
nstr[len] = '\0';
return nstr;
}
LList convertToList(char line){
int i;
int strSize = strlen(line);
//Eight elements in every node
int maxelem = 8;
int currentPartition = 0;
int currentIndex = 0;
int totalPartitions = strSize/maxelem;
LList* temp = NULL;
for(i = 0;i = totalPartitions){
break;
}
char * currStr = substr(line,i,maxelem);
int number = atoi(currStr);
if(temp == NULL){
temp = malloc(sizeof(LList));
temp -> data = number;
temp -> next = NULL;
}else{
LList * current = malloc(sizeof(LList));
current -> data = number;
current -> next = temp;
temp = current;
}
currentIndex = i;
currentPartition++;
}
currentIndex = currentIndex + maxelem;
if((currentIndex > 0) && (currentIndex data = remainingNum;
current -> next = temp;
temp = current;
}
return temp;
}
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 an
```
#include
#include
#include
typedef struct node{
int data;
struct node* next;
} LList;
void printList(LList * list);
LList pushToResult(LList resultList,int entry){
LList * temp = malloc(sizeof(LList));
temp -> data = entry;
temp -> next = resultList;
resultList = temp;
return resultList;
}
char substr(char const input,size_t start,size_t len){
char *nstr = malloc(len+1);
memcpy(nstr,input+start,len);
nstr[len] = '\0';
return nstr;
}
LList convertToList(char line){
int i;
int strSize = strlen(line);
//Eight elements in every node
int maxelem = 8;
int currentPartition = 0;
int currentIndex = 0;
int totalPartitions = strSize/maxelem;
LList* temp = NULL;
for(i = 0;i = totalPartitions){
break;
}
char * currStr = substr(line,i,maxelem);
int number = atoi(currStr);
if(temp == NULL){
temp = malloc(sizeof(LList));
temp -> data = number;
temp -> next = NULL;
}else{
LList * current = malloc(sizeof(LList));
current -> data = number;
current -> next = temp;
temp = current;
}
currentIndex = i;
currentPartition++;
}
currentIndex = currentIndex + maxelem;
if((currentIndex > 0) && (currentIndex data = remainingNum;
current -> next = temp;
temp = current;
}
return temp;
}
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 an
Solution
Aside from the glaring bug, the major theme I see is that you can strive to generalize your code.
Examples:
-
Instead of reading from
-
Why limit the program to adding two numbers, when you could add n numbers? It may be counterintuitive, but writing a program to add many lines of numbers would take less code, since you don't have to copy-and-paste the code to handle
-
In
Examples:
-
Instead of reading from
input.txt, read from stdin. For less work, the user gets more flexibility.-
Why limit the program to adding two numbers, when you could add n numbers? It may be counterintuitive, but writing a program to add many lines of numbers would take less code, since you don't have to copy-and-paste the code to handle
num1 and num2. (Start with an empty list, which represents an accumulator with an initial 0 value.)-
In
ConvertToList(), you don't need a special case for if(temp == NULL){ … } — the general case works just fine. Also, it's weird that your temp variable has a longer lifespan than current. Therefore, I would consider temp to be misnamed. I suggest head instead.Context
StackExchange Code Review Q#44425, answer score: 3
Revisions (0)
No revisions yet.