Tell me why, by adding a separate case to remove an item from the list, if it is one, then only the name is deleted. How to correct?

APTEKA* DeleteElem(APTEKA* pdel) { if(pdel == head && pdel == tail) { head->next=NULL; head->prev=NULL; tail->next=NULL; tail->prev=NULL; FreeElemMemo(pdel); return NULL; } if(pdel == head) { head=head->next; head->prev=NULL; FreeElemMemo(pdel); return head; } if(pdel == tail) { tail = tail->prev; tail->next=NULL; FreeElemMemo(pdel); return NULL; } else { APTEKA *pnext; pdel->next->prev=pdel->next; pnext=pdel->prev->next=pdel->next; FreeElemMemo(pdel); return pnext; } } 
  • What does "only the name delete" mean? What other "name"? Separately: what is the meaning of all these ... = NULL in the first branch? - AnT
  • It turns out, if the element, there is the name, date, price, and so on. When there was no this branch, then with one element the program fell. The first if I added that if there is only one element that will be the head and tail at the same time, then it would nullify the addresses. - Andrey Kompaniets
  • Look at the code I once did to demonstrate how a cyclic doubly linked list works: pastebin.com/mScMkkdy - 0andriy

1 answer 1

Found only one slip of a pen,:

 APTEKA *pnext; pdel->next->prev=pdel->next; // BUG pnext=pdel->prev->next=pdel->next; FreeElemMemo(pdel); return pnext; 

should be like this:

 APTEKA *pnext; pdel->next->prev=pdel->prev; pnext=pdel->prev->next=pdel->next; FreeElemMemo(pdel); return pnext; 

Incomprehensible question, what does it remove only the name?

  • If head == pdel == tail, then you should not reset pointers twice, even once it is not necessary. - AlexGlebe