Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(15pt) Suppose we are dealing with very large integers, so a variable with int t

ID: 3735280 • Letter: #

Question

(15pt) Suppose we are dealing with very large integers, so a variable with int type is not enough to store such big numbers. So we decided to use double linked list to store the digits of our integers. Accordingly, we created the following structures and linked them to store the digits of our integers, as shown in the below examples 6. typedef struct digitcell int digit; l Actually, it would be better to make this char. but to simplify the code, keep it int struct digitcell prev; struct digitcell *next; | digitcellT typedef struct bigint ( digitcellT *start; digitcellT *end; ) bigintT; Suppose we have already created the following two linked lists for 759 and 9634, respectively NULL NULI NULL NULL You are asked to implement a function, void sum_func (bigintT * sum, bigintT *nl, bigintT *n2), which can be called as follows to create a new link list to represent the sum of the integers given in the above lists bigintT sum, nl, n2; suppose we created the above lists and stored their start and end in nl and n2 */ sum . Start sum . end = NULL; sum func (, &nl;, &n2;) The resulting new list (sum) will look like as follows sum NUL NULL

Explanation / Answer

void AddNewDigitCellToHead(bigintT *pnum, int newdigit){

     digitcell *q = (digitcell *)malloc(sizeof(digitcell));
     q->digit = newdigit;
     q->prev = NULL;
     q->next = NULL;
     if (pnum->start == NULL){
        pnum->start = q;
        pnum->end = q;
     }
     else {
          q->next = pnum->start;
          pnum->start->prev = q;
          pnum->start = q;
     }
}

void sum_func(bigintT *psum, bigintT *pn1, bigintT *pn2){

     psum = (bigintT *)malloc(sizeof(bigintT));
     psum->start = NULL;
     psum->end = NULL;

     digitcell *d1 = pn1->end;
     digitcell *d2 = pn2->end;

     int carry = 0;
     while (d1 != NULL && d2 != NULL){
          int a = d1->digit + d2->digit + carry;
          if (a > 9){
               a = a % 10;
               carry = 1;
          }
          else {
              carry = 0;
          }
          AddNewDigitCellToHead(psum, a);   
          d1 = d1->prev;
          d2 = d2->prev;   
     }
     while(d1 != NULL){
         int a = d1->digit + carry;
          if (a > 9){
               a = a % 10;
               carry = 1;
          }
          else {
              carry = 0;
          }
          AddNewDigitCellToHead(psum, a);   
          d1 = d1->prev;
     }
     while(d2 != NULL){
         int a = d2->digit + carry;
          if (a > 9){
               a = a % 10;
               carry = 1;
          }
          else {
              carry = 0;
          }
          AddNewDigitCellToHead(psum, a);   
          d2 = d2->prev;
     }   
}