Я пытался немного попрактиковаться с некоторыми алгоритмами, и в моем коде для двусвязного списка я хочу иметь возможность рекурсивно удалять узел в n-й позиции. Я пытался сделать это самостоятельно, но, похоже, не могу найти эффективный способ сделать это с помощью рекурсии. Если бы кто-то мог мне помочь в этом, было бы здорово. Вот код, который у меня есть на данный момент.
Кроме того, я также знаю, что текущий код моей функции удаления работает только для односвязного списка. Я понял это самостоятельно и просто поместил это здесь в качестве заполнителя / возился с кодом, который написал ниже.
#include
using namespace std;
struct Node{
int data;
Node* next;
Node* prev;
};
Node* add(Node* head, int data);
void display(Node* head);
void displayReverse(Node* head);
Node* deleteNode(Node* head, int pos, Node* delNode);
int main(){
Node* head = NULL;
head = add(head, 1);
head = add(head,2);
head = add(head, 3);
head = add(head, 4);
head = add(head, 5);
display(head);
coutnext == NULL){ //if the head points to a node that has a next that is not empty, but the node at the next is empty, then add to the end of list
Node* newNode = new Node;
head->next = newNode;
newNode->data = data;
newNode->next = NULL;
newNode->prev = head;
}
else{ //if the head does not equal null, and the head next does not equal null either
head->next = add(head->next, data);
}
return head;
}
void display(Node* head){
if(head!=NULL){
coutnext;
}
while(head!=NULL){
coutnext;
delete head;
return delNode;
}
else{
head->next = deleteNode(head->next, pos-1, delNode);
return head;
}
}
Подробнее здесь: https://stackoverflow.com/questions/674 ... ursively-c