Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting deletes the value from my linked list
    primarykey
    data
    text
    <p>As a part of an assignment, I need to write two functions: </p> <ol> <li>a function that sums up two natural numbers represented as a linked list</li> <li>a functions that prints a number represented in the same way.</li> </ol> <p>for some reason, both function work perfectly fine separately, but when I try to use the print function on the result of the sum function, it changes the value of the sum right in the beginning of the print function , and prints the wrong value. when I use printf to print the same value in the main, there is no problem. my code is detailed below. any ideas? </p> <pre><code>void main() { int a[1] = { 1 }, b[1] = { 2 }; int * *pa, **pb; List lst1, lst2; List sum; pa = (int * *) malloc(sizeof(int * )); * pa = &amp;a[0]; pb = (int * *) malloc(sizeof(int * )); * pb = &amp;b[0]; lst1 = arrToList(pa, 1); lst2 = arrToList(pb, 1); addNumbers(lst1, lst2, &amp;sum); //printf("%d\n",*(sum.head-&gt;dataPtr)); printNumber(sum); } //a function that recieves a number represented ad a list and prints it void printNumber(List num) { ListNode * curr; int currData, i, number; if (isEmptyList(num) == TRUE) printf("the input was an empty list, nothing to print"); else { i = 0; number = 0; curr = num.head; while (curr != NULL) { currData = *(curr - &gt;dataPtr); number = number + currData * ((int) pow(10, i)); curr = curr - &gt;next; i++; } printf("%d \n", number); } } // a function that sums in list // representation two numbers, // each represented as a list void addNumbers(List n1, List n2, List * sum) { ListNode * currN1; ListNode * currN2; ListNode * currSum; int currN1N2Sum; //stores the sum of the current digits in n1 and n2 int carrier, prevCarrier; //current and previous carriers that carries +1 to the next digit of sum if the lst sum was bigger then 9 if ((isEmptyList(n1) == TRUE) || (isEmptyList(n2) == TRUE)) printf("bad input =("); else { currN1 = n1.head; currN2 = n2.head; * sum = createEmptyList(); carrier = 0; prevCarrier = 0; while ((currN1 != NULL) &amp;&amp; (currN2 != NULL)) { currN1N2Sum = *(currN1-&gt;dataPtr) + *(currN2-&gt;dataPtr) + prevCarrier; if (currN1N2Sum &gt; 9) { carrier = 1; currN1N2Sum = currN1N2Sum - 10; } currSum = creatNewListNode( &amp; currN1N2Sum, NULL); insertNodeToEnd(sum, currSum); prevCarrier = carrier; carrier = 0; currN1 = currN1 - &gt;next; currN2 = currN2 - &gt;next; } //while ((currL1!=NULL)&amp;&amp;(currL2!=NULL)) while (currN1 != NULL) { currN1N2Sum = *(currN1 - &gt;dataPtr) + prevCarrier; currN1 = currN1 - &gt;next; if (prevCarrier != 0) prevCarrier = 0; } while (currN2 != NULL) { currN1N2Sum = *(currN2 - &gt;dataPtr) + prevCarrier; currN2 = currN2 - &gt;next; if (prevCarrier != 0) prevCarrier = 0; } } // ! ((isEmptyList(n1)==TRUE)||(isEmptyList(n2)==TRUE)) } </code></pre> <p>here is the rest of the code:</p> <pre><code>typedef struct listNode{ int* dataPtr; struct listNode* next; } ListNode; typedef struct list { ListNode* head; ListNode* tail; } List; List createEmptyList()//creates and returns an empty linked list { List res; res.head = res.tail = NULL; return res; } Bool isEmptyList ( List lst )//checks if a given list is empty or not { if (lst.head == NULL &amp;&amp; lst.tail == NULL) return TRUE; else return FALSE; } void insertDataToEnd ( List * lst, int *dataPtr ) //inserts new data to the end of an existing linked list { ListNode * newTail; newTail = creatNewListNode ( dataPtr, NULL ); insertNodeToEnd(lst,newTail); } void insertNodeToEnd ( List * lst, ListNode * newTail )//insert an existing node to an existing linked list { if (isEmptyList(*lst) == TRUE ) insertNodeToStart ( lst,newTail ); else { (*lst).tail -&gt; next = newTail; newTail-&gt;next = NULL; (*lst).tail = newTail; } } ListNode * creatNewListNode ( int * dataPtr, ListNode * next )//inserts new node in an existing linked list { ListNode * res; res = (ListNode *) malloc (sizeof(ListNode)); res -&gt; dataPtr = dataPtr; res -&gt; next = next; return res; } void insertNodeToStart ( List * lst, ListNode * newHead )//inserts node to the begining of a given linked list { if ( isEmptyList( *lst ) == TRUE ) { (*lst).head = newHead; (*lst).tail = newHead; newHead -&gt; next = NULL; } else { newHead -&gt; next = (*lst).head; (*lst).head = newHead; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload