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

The program below represents decimal integers as a linked list of digits. It ask

ID: 3840222 • Letter: T

Question

The program below represents decimal integers as a linked list of digits. It asks for 2 positive integers, stores them as linked lists, computes the sum, and outputs it. It then 'free()'s *all* nodes of all 3 lists.

You must use my struct DigitNode

I strongly suggest that you represent the integers in little-endian order. That will make add() way easier.

Please write:

obtainPostiveInt()

numberList()

add()

printList()

freeList()

Sample output:

(No surprises here, it is just decimal integer addition, but these are some interesting boundary cases to make sure you handle.)

Explanation / Answer

Below are the required code for few of the needed functions:

// PURPOSE: To build and return a linked list IN LITTLE ENDIAN ORDER
// of the decimal number whose text is pointed to by 'numberCPtr'.
// If 'numberCPtr' points to the string "123" then the linked list
// returned is 'digit_=3' -> 'digit_=2' -> 'digit_=1' -> NULL.
struct DigitNode*
numberList (const char* numberCPtr
)
{
// YOUR CODE HERE
DigitNode* head = NULL;
DigitNode* temp = head;
int num = atoi(numberCPtr);
int rem = 0;
int temp = num;
if (head == NULL)
{
   rem = temp%10;
    head->digit_ = rem;
   head->nextPtr = NULL;
   temp = temp/10;
}
while (temp != 0)
{
   struct DigitNode * new;
   rem = temp%10;
   head -> nextPtr = new;
   head->digit_ = rem;
   head->nextPtr = NULL;
   temp = temp/10;
}

return head; //returning the head node of the linked list
}

// PURPOSE: To build and return a linked list IN LITTLE ENDIAN ORDER
// of the decimal number that results from adding the decimal numbers
// whose digits are pointed to by 'list0' and 'list1'.
struct DigitNode*
add (const struct DigitNode* list0,
const struct DigitNode* list1
)
{
   // YOUR CODE HERE
    struct DigitNode * temp1 = list0;
   stuct DigitNode * temp2 = list1;
   struct DigitNode * sumlist = NULL;
   struct DigitNode * temp_sum = sumlist;
   int digit_sum = 0;
   int carry = 0;

   while(temp1 != NULL || temp2 != NULL)
   {
       digit_sum = temp1->digit_ + temp2->digit_ + carry;
       if (digit_sum > 9)
       {
           carry = digit_sum/10;
           digit_sum = digit_sum%10;
       }
       if(temp_sum == NULL)
       {
           temp_sum->digit_ = digit_sum;
           temp_sum->nextPtr = NULL;
       }
       else
       {
           struct DigitNode* new;
           new->digit_ = digit_sum;
           new->nextPtr = NULL;
           temp_sum->nextPtr = new;
       }
       temp1 = temp1->next;
       temp2 = temp2->next;      
   }
   if(temp1 == NULL)
   {
       if(temp2 != NULL)
       {
           while(temp2 != NULL)
           {
           struct DigitNode* new;
           new->digit_ = temp2->digit_;
           new->nextPtr = NULL;
           temp_sum->nextPtr = new;
           temp2 = temp2->nextPtr;
       }
       }
   }
   if(temp2 == NULL)
   {
       if(temp1 != NULL)
       {
           while(temp1 != NULL)
           {
           struct DigitNode* new;
           new->digit_ = temp1->digit_;
           new->nextPtr = NULL;
           temp_sum->nextPtr = new;
           temp1 = temp1->nextPtr;
       }
       }
   }
   return sumList; //returning the head node of the list of sums of digits
}