Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with the delete section?
    primarykey
    data
    text
    <p>Here is my code. The problem that I am getting is that I can't make work the deleter part. I want to delete one element of the structure. Here is a basic structure that you add, search and delete elements that are added. Also the elements are sorted in increasing order.</p> <pre><code>#include &lt;iostream&gt; using namespace std; struct el { int key; el *next; } *start; void init(); void add(int n); void list(); void search(int n); // ima za dopylvane void deleter(int n); void main() { init(); int n, num; do { cout &lt;&lt; "\n Menu\n"; cout &lt;&lt; "\n1 - Add element"; cout &lt;&lt; "\n2 - List elements"; cout &lt;&lt; "\n3 - Searching for element"; cout &lt;&lt; "\n4 - Delete element"; cout &lt;&lt; "\n5 - Exit"; cout &lt;&lt; "\n Your choice:"; cin &gt;&gt; n; switch (n) { case 1: { cout &lt;&lt; "\n Input num:"; cin &gt;&gt; num; add(num); break; } case 2: { list(); break; } case 3: { cout &lt;&lt; "\n Input num for searching:"; cin &gt;&gt; num; search(num); break; } case 4: { cout &lt;&lt; "\n Input num for deleting:"; cin &gt;&gt; num; deleter(num); break; } } } while (n != 5); } void list() { el *p = start; if (start) while (p) { cout &lt;&lt; p-&gt;key &lt;&lt; " "; p = p-&gt;next; } else cout &lt;&lt; "\n Empty list"; } void init() { start = NULL; } void add(int n) { el *p = start; if (!start) { start = new el; start-&gt;key = n; start-&gt;next = NULL; } else { if (start-&gt;key &gt; n) { start = new el; start-&gt;key = n; start-&gt;next = p; } else { el * q; while ((p-&gt;key &lt; n) &amp;&amp; (p-&gt;next)) { q = p; p = p-&gt;next; } if (p-&gt;key &gt; n) { el *s; s = new el; s-&gt;key = n; s-&gt;next = p; q-&gt;next = s; } else { el *s; s = new el; s-&gt;key = n; s-&gt;next = NULL; p-&gt;next = s; } } } } void search(int n) { el *p = start; if (!start) cout &lt;&lt; "\nEmpty list!"; else while ((p-&gt;key != n) &amp;&amp; (p-&gt; next)) p = p-&gt;next; if (p-&gt;key == n) cout &lt;&lt; "\nYes!"; else cout &lt;&lt; "\nNo!"; } void deleter(int n) { el *p = start; if (!start) cout &lt;&lt; "Enfsad"; else while ((p-&gt;key != n) &amp;&amp; (p-&gt;next)) { n = start-&gt;key; start = start-&gt;next; delete p; } } </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