Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you have an error in the add of a new element in the middle or in the end of the linked list. in your <code>addStoredItem</code> function you make a pointer from your newItem to the currentItem but you do not take account to have a link from previous element to the newItem</p> <p>initial linked list befor calling <code>addStoredItem</code></p> <pre><code>item1--&gt;item2--&gt;item4--&gt;item5 </code></pre> <p>Linked list afetr calling <code>addStoredItem</code> in order to add <strong>item3</strong></p> <pre><code>item1--&gt;item2--&gt;item4--&gt;item5 ^ | item3 </code></pre> <p>so as you can see you have added the new item at the head of the sub linked list starting from <strong>item4</strong> but you do not make link from <strong>item2</strong> to <strong>item3</strong> so that we have to keep a pointer to the previous item in order to complete the link.</p> <p>the <code>add_item()</code> function allow to add item in the head</p> <p>The same thing if you try to add item at the end of the linked list</p> <pre><code>item1--&gt;item2--&gt;item4--&gt;item5 NULL ^ | item6 </code></pre> <p>the <strong>item6</strong> has added as separate head and there is no link from <strong>item5</strong> (previous) to item6 (new)</p> <p>so your <code>addStoredItem()</code> function could be fixed like this</p> <pre><code>ListItem* addSortedItem(ListItem *p_head, char *s){ if(p_head==NULL)//if the list is empty, we add to the beginning return add_item(p_head,s); struct ListItem* p_new_item = malloc(sizeof(ListItem)); ListItem *p_current_item = p_head; //makes a pointer to the head of the list ListItem *p_prev_item = NULL; //FIXED while (p_current_item) { // Loop while the current pointer is not NULL printf("entering while loop with current=%s\n",p_current_item-&gt;s); // now we want to look at the value contained and compare it to the value input if(aThenB(s,p_current_item-&gt;s)!=TRUE){ // if A goes after B, we want to go on to look at the next element p_prev_item = p_current_item; //FIXED p_current_item=p_current_item-&gt;p_next; } else if (aThenB(s,p_current_item-&gt;s)==TRUE) {printf("entered elseif\n"); break; } else {printf("WHY DID WE EVER REACH THE ELSE!?"); return p_head;} } if (p_prev_item!=NULL) //FIXED p_prev_item-&gt;p_next=add_item(p_current_item,s); //FIXED else //FIXED p_head=add_item(p_current_item,s);//FIXED return p_head; //FIXED } </code></pre> <p>the fixed lines are indicated with <code>//FIXED</code></p>
    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.
 

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