Some sketches, but I don’t know if

F_list.h

#ifndef F_LIST_H #define F_LIST_H struct Node{ int value; Node* next; Node* prev; }; class F_list{ public: void deleteInd(int); void deleteNod(Node); ~F_list(); private: Node* head; Node* tail; }; #endif // F_LIST_H 

F_List.cpp

 #include <iostream> #include "f_list.h" F_list::F_list() :head(nullptr), tail(nullptr) { } F_list::~F_list(){ } void F_list::deleteNod(Node n){ n->next = nullptr; n->prev = nullptr; delete n; } void F_list::deleteInd(int i){ } 
  • No, deleteNod is obviously wrong, because when deleting a node, you need to take care to associate the previous with the next. - HasmikGaryaka
  • Constructor this.head = head; this.tail = tail. if (this.head! = nullptr) this.head.next = tail; if (this.tail! = nullptr) this.tail.next = head; - HasmikGaryaka
  • one
    @HasmikGaryaka, and how to remove the node then? - Estet
  • @HasmikGaryaka, and what did you write about the constructor, these hyphens in the case when you only have a head and a tail? - Estet
  • Try to draw it on paper, then it is easier to write. Node as a rectangle, divided into segments, the connection between the nodes. - HasmikGaryaka

1 answer 1

 void F_list::deleteNod(Node n){ n->prev->next=n->next; n->next->prev=n->prev; n->next = nullptr; n->prev = nullptr; delete n; } void F_list::deleteInd(int i){ Node curr; if (head!=nullptr) { curr=head; for (int j=0;j<i;j++) { if(curr->next==nullptr) break; curr=curr->next; } if (curr!=nullptr) deleteNod(curr); } } 
  • only there in my opinion Node * cure - Estet
  • Node * curr = new Node; - Estet Sep.
  • and if curr = head, won't we have two heads? - Estet
  • hmm, or then curr retire anyway, and only one head will remain - Estet
  • and one more question, if curr-> nex == nullptr, is it not deleted at all? - Estet