递归删除双向链表中第n个位置的节点? (C++)
Delete node at nth position from doubly linked list recursively? (C++)
我一直在尝试使用一些算法进行一些练习,在我的双向链表代码中,我希望能够递归地删除第 n 个位置的节点。我曾尝试自己这样做,但我似乎无法找到一种有效的递归方法。如果有人可以帮助我这样做,那就太好了。这是我目前的代码。
此外,我还知道我的删除功能的当前代码仅适用于单链表。我自己想出了这个,只是把它作为占位符放在那里/乱用我在下面写的代码。
#include <cctype>
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);
cout<<endl;
displayReverse(head);
cout<<endl;
int del;
cout<<"pos to delete: ";
cin>>del;
Node* delNode = NULL;
head = deleteNode(head, del, delNode);
display(head);
return 0;
}
Node* add(Node* head, int data){
if(head==NULL){ //if list is empty
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
else if(head!=NULL && head->next == 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){
cout<<head->data<<" ";
display(head->next);
}
return;
}
void displayReverse(Node* head){ //checks if the nodes are actually linked (this implementation works)
while(head->next!=NULL){
head = head->next;
}
while(head!=NULL){
cout<<head->data<< " ";
head = head->prev;
}
}
Node* deleteNode(Node* head, int pos, Node* delNode){
if(pos = 1){
delNode = head->next;
delete head;
return delNode;
}
else{
head->next = deleteNode(head->next, pos-1, delNode);
return head;
}
}
从双向链表中递归删除第n
个节点:
- 维护计数器
- 如果计数器小于要删除的节点的位置则
- 增加计数器并递归调用删除函数并传递列表中当前处理节点的下一个。
- 将delete函数的return值赋给当前处理节点的next指针
- 从删除函数中重置 returned 节点的前一个指针。
- Return当前处理节点。
- 如果counter等于position表示当前节点要从链表中删除
- 记录当前处理节点的下一个节点
- 将当前处理节点的下一个和上一个链接重置为
NULL
。
- 取消分配当前处理节点节点。
- 重置计数器,这样
(counter == position)
条件将在以后的迭代中失败。
- Return记录的节点。
实施:
Node* deleteNode (Node* head, int pos) {
static int cnt;
if (head == NULL) {
return NULL;
}
if (++cnt < pos) {
head->next = deleteNode (head->next, pos);
if (head->next) {
// reset the pointer
head->next->prev = head;
}
}
if (cnt == pos) {
Node *tmp = head->next;
head->prev = NULL;
head->next = NULL;
free (head);
// reset counter
cnt = 0;
if (tmp) {
tmp->prev = NULL;
}
return tmp;
}
return head;
}
输出:
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 2
1 3 4 5
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 1
2 3 4 5
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 5
1 2 3 4
无效位置:
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 8
1 2 3 4 5
附加:
1).避免在程序中添加 using namespace std;
。检查 this.
我一直在尝试使用一些算法进行一些练习,在我的双向链表代码中,我希望能够递归地删除第 n 个位置的节点。我曾尝试自己这样做,但我似乎无法找到一种有效的递归方法。如果有人可以帮助我这样做,那就太好了。这是我目前的代码。
此外,我还知道我的删除功能的当前代码仅适用于单链表。我自己想出了这个,只是把它作为占位符放在那里/乱用我在下面写的代码。
#include <cctype>
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);
cout<<endl;
displayReverse(head);
cout<<endl;
int del;
cout<<"pos to delete: ";
cin>>del;
Node* delNode = NULL;
head = deleteNode(head, del, delNode);
display(head);
return 0;
}
Node* add(Node* head, int data){
if(head==NULL){ //if list is empty
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
else if(head!=NULL && head->next == 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){
cout<<head->data<<" ";
display(head->next);
}
return;
}
void displayReverse(Node* head){ //checks if the nodes are actually linked (this implementation works)
while(head->next!=NULL){
head = head->next;
}
while(head!=NULL){
cout<<head->data<< " ";
head = head->prev;
}
}
Node* deleteNode(Node* head, int pos, Node* delNode){
if(pos = 1){
delNode = head->next;
delete head;
return delNode;
}
else{
head->next = deleteNode(head->next, pos-1, delNode);
return head;
}
}
从双向链表中递归删除第n
个节点:
- 维护计数器
- 如果计数器小于要删除的节点的位置则
- 增加计数器并递归调用删除函数并传递列表中当前处理节点的下一个。
- 将delete函数的return值赋给当前处理节点的next指针
- 从删除函数中重置 returned 节点的前一个指针。
- Return当前处理节点。
- 如果counter等于position表示当前节点要从链表中删除
- 记录当前处理节点的下一个节点
- 将当前处理节点的下一个和上一个链接重置为
NULL
。 - 取消分配当前处理节点节点。
- 重置计数器,这样
(counter == position)
条件将在以后的迭代中失败。 - Return记录的节点。
实施:
Node* deleteNode (Node* head, int pos) {
static int cnt;
if (head == NULL) {
return NULL;
}
if (++cnt < pos) {
head->next = deleteNode (head->next, pos);
if (head->next) {
// reset the pointer
head->next->prev = head;
}
}
if (cnt == pos) {
Node *tmp = head->next;
head->prev = NULL;
head->next = NULL;
free (head);
// reset counter
cnt = 0;
if (tmp) {
tmp->prev = NULL;
}
return tmp;
}
return head;
}
输出:
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 2
1 3 4 5
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 1
2 3 4 5
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 5
1 2 3 4
无效位置:
# ./a.out
1 2 3 4 5
5 4 3 2 1
pos to delete: 8
1 2 3 4 5
附加:
1).避免在程序中添加 using namespace std;
。检查 this.