Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>print(search(type="cliente", codice=random.randrange(1000))) print(find(type="cliente", codice=random.randrange(1000))) import sys sys.exit(1)</p> <p>Your code seems a bit complex for what is needed but the idea is correct. I don't see any error in the insert method but it's quite hard to follow all this sort of copy'n paste program. You can have each students in several different lists at the same time, but I'd suggest using a different approach to avoiding all this duplication that makes very easy to introduce bugs.</p> <h2>Data structure</h2> <p>You could abstract the idea of link and list:</p> <pre><code>typedef struct TList { struct TLink *first, *last; } List; typedef struct TLink { struct TStudent *student; struct TLink *prev, *next; } Link; </code></pre> <p>The <code>List</code> structure is any of the three lists you need (first name, last name, birthdate) and <code>Link</code> is any of the links. See the following picture...</p> <p><img src="https://i.stack.imgur.com/p29Kk.png" alt="enter image description here"></p> <p>With this approach the code for inserting/removing a link in a list is shared with all link types (<code>first_name</code>, <code>last_name</code>, <code>age</code>). The price to pay is an extra pointer for each link and the need to write <code>link-&gt;student-&gt;first_name</code> instead of <code>link-&gt;first_name</code>.</p> <h2>Link insertion/deletion</h2> <p>Adding a link to a list is something like</p> <pre><code>// Adds a new link before the link `before` or as last link // if `before` is NULL void addLink(List *list, Link *newlink, Link *before) { newlink-&gt;next = before; newlink-&gt;prev = before ? before-&gt;prev : list-&gt;last; if (newlink-&gt;next) newlink-&gt;next-&gt;prev = newlink; else list-&gt;last = newlink; if (newlink-&gt;prev) newlink-&gt;prev-&gt;next = newlink; else list-&gt;first = newlink; } </code></pre> <p>and removing a link from a list is something like</p> <pre><code>void removeLink(List *lists, Link *link) { if (link-&gt;next) link-&gt;next-&gt;prev = link-&gt;prev; else list-&gt;last = link-&gt;prev; if (link-&gt;prev) link-&gt;prev-&gt;next = link-&gt;next; else list-&gt;first = link-&gt;next; } </code></pre> <p>These two function can be used to manage lists/links of all three types (first_name, last_name and age) without any code duplication.</p> <h2>Insertion of a Student object</h2> <p>With this approach the student object can have all the data and many links objects</p> <pre><code>typedef struct TStudent { char first_name[NAMESIZE]; char last_name[NAMESIZE}; int age; Link first_name_link, last_name_link, age_link; } Student; </code></pre> <p>For example to insert the student in order for the <code>first_name</code> list the code becomes</p> <pre><code>Student *newstudent = ... ... Link *before = first_name_list.first; while (before != NULL &amp;&amp; strcmp(newstudent-&gt;first_name, before-&gt;student-&gt;first_name) &gt; 0) { before = before-&gt;next; } addLink(&amp;first_name_list, &amp;newstudent-&gt;first_name_link, before); </code></pre> <p>Please note that this simple loop handles correctly all cases that in your code are instead handled separately with copy'n paste of similar statements (this is the first student inserted, the last, one in the middle).</p> <p>The idea here is just to set the <code>before</code> node to the first in the list and keeping moving it to next student if the new one must be inserted after instead.</p> <p>Of course the same kind of loop is needed to find the correct insertion point in the other two lists (<code>last_name_list</code> and <code>age_list</code>). Factoring out also the insertion point search would be possible by using function pointers.</p> <h2>Deletion of a Student object</h2> <p>To delete a <code>Student</code> of course the student data must be removed from all the lists, in other words all three links must be removed. This however simply means calling three times the <code>removeLink</code> function:</p> <pre><code> removeLink(&amp;first_name_list, &amp;student-&gt;first_name_link); removeLink(&amp;last_name_list, &amp;student-&gt;last_name_link); removeLink(&amp;age_list, &amp;student-&gt;age_link); free(student); </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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