Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with double-linked list
    primarykey
    data
    text
    <p>So I got this task to make a program which will allow the user to enter a number of integer elements in a double-linked list and I have to delete the ones which can be divided (remainder 0) with the sum of their digits.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define NEW(t) (t*)malloc(sizeof(t)) typedef int info_t; typedef struct element { info_t info; struct element *next; struct element *prev; } node; typedef node* nodep; void insert(nodep l, info_t x) { nodep t = NEW(node); t-&gt;info=x; t-&gt;next=l-&gt;next; l-&gt;next=t; t-&gt;prev=l; } void printList(nodep l) { nodep t=l-&gt;next; while(t!=l) { printf("-&gt;%d", t-&gt;info); t=t-&gt;next; } printf("\n"); } void deletedividable(nodep l) { nodep t=l-&gt;next; nodep temp; while(t-&gt;next!=l) { int temporary=t-&gt;info; int sum=0; while(temporary&gt;0) { sum+=(temporary%10); temporary/=10; } if(!(t-&gt;info%sum)) { temp=t-&gt;next; t-&gt;next-&gt;prev=t-&gt;prev; t-&gt;prev-&gt;next=t-&gt;next; free(t); t=temp; } else t=t-&gt;next; } } int main() { // declaring a leader node nodep list = NEW(node); list-&gt;next = list; list-&gt;prev = list; printf("Enter elements:\n "); int a; //if the input isn't a number the loop will exit while(scanf("%d", &amp;a)) { //elements input function call insert(list, a); } // print list function call printList(list); // delete elements which are dividable with the sum of their digits deletedividable(list); printList(list); return 0; } </code></pre> <p>The problem is, after the deletedividable(list); function call, nothing is printed when the second printlist is called and I can't seem to locate the problem, some of the pointers must be getting screwed up, but I'm not sure which ones. Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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