Upon successful deletion, it should return true, but for some reason it always returns a fall. Here is the class:

struct Num { int number; bool ring; Num *next, *prev; }; class List { protected : Num *head, *tail; public : List():head(NULL),tail(NULL){} ~List(); int genNum(); void Add(int x); bool Remove(int i); }; 

And my attempt to redo the deletion:

 bool List::Remove(int idx) { Num *current = head; Num *temp = nullptr; int i = 0; while(current->next != NULL) { if(i != idx) { current = current->next; i++; } else { temp = current->prev; current->prev = current->next; current->next->prev = temp; return true; } } return false; } 
  • 3
    well, it never comes in elsewhere - TigerTV.ru
  • @ TigerTV.ru aaaaa, what a stupid mistake ... Thank you - Digital Resistance
  • and even correct this, a memory leak and logical error is guaranteed - AR Hovsepyan
  • @ARHovsepyan and how to fix it? - Digital Resistance
  • @DigitalResistance Delete an object from memory that you are deleting from the list? - LLENN

1 answer 1

I will only fix a part of the code that I thought was wrong.

 else { if (idx == 1) { temp = head; head = current; } else { temp = current->prev; // указывает на узель, который должен удаляться current->prev = temp->prev; current->prev->next = current; } delete temp; return true; } 
  • although for good idx should be unsigned and not null, these conditions should also be taken into account. And at the very beginning of the function you need to check if (idx <1) return false; - AR Hovsepyan