Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In context to <code>printListrev()</code>:</p> <p>Unless this is a circular doubly linked list, in which case last element is preceded by the first, <code>start</code> would have previous element NULL. So, there is no point in accessing the previous field of <code>start</code>, as you do here:</p> <pre><code>tmpval=start; ... tmpval=tmpval-&gt;prev; </code></pre> <p>You can keep another pointer to end of the list for this purpose.</p> <p>Other alternatives include:</p> <p>recursive function:</p> <pre><code>void printrev(struct node *s) { if (s == NULL) return; printrev(s-&gt;next); printf("%d ", s-&gt;detail); } </code></pre> <p>iterative function:</p> <pre><code>void printrev() { struct node *end; for (end = start; end-&gt;next != NULL; end = end-&gt;next) ; for (; end != NULL; end = end-&gt;prev) printf("%d ", end-&gt;detail); } </code></pre> <p>Your <code>getNode</code> is of limited use. Suppose, if you want to delete element, your <code>getnode</code> would only return whether, element is present or not. Say it is present, your <code>deleteNode</code> function would still have to iterate to the appropriate element in the list before deleting it. </p> <p>This could be solved by <code>getNode</code> returning pointer to the node:</p> <pre><code>node *getNode(int x) { node *t; for (t = start; t != NULL; t = t-&gt;next) if (t-&gt;detail == x) return t; return t; } </code></pre> <p>Now, you can code delete as follows:</p> <pre><code>void delNode(node *n) { n-&gt;prev-&gt;next = n-&gt;next; n-&gt;next-&gt;prev = n-&gt;prev; free(n); } </code></pre> <p>And call as follows:</p> <pre><code>node *n; if ((n = getNode(x)) != NULL) delNode(n); </code></pre> <p>I've assumed that you <code>struct</code> is:</p> <pre><code>struct node { int detail; struct node *next; struct node *right; }; typedef struct node * node; </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.
 

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