I'm implementing a function where I would need to delete nodes based on given data and add the same time, I'll be carrying out this operating until my linked list reaches a NULL pointer.
我正在实现一个函数,我需要根据给定的数据删除节点并添加相同的时间,我将执行此操作,直到我的链表达到NULL指针。
At the same time, I need to also test whether my list is empty and print and another message and prevent the process above for being executed is there any way I can implement this?
同时,我还需要测试我的列表是否为空并打印和另一条消息,并阻止上面的进程执行是否有任何方法可以实现这一点?
void del(Node*&p, int k)
{
if(ptr!=NULL)
{
if(ptr->data==k)
{
cout<<"Random Data"<<endl;
Node*temp;
temp=p;
p=p->next;
delete temp;
}
else
del(p->next,k)
}
}
Is there any way I can implement the same without recursion?
有没有办法在不递归的情况下实现相同的功能?
3 个解决方案
#1
0
while to the rescue:
在救援时:
while (p!=NULL)
{
if(p->data==k)
{
cout<<"Random Data"<<endl;
Node*temp;
temp=p;
p=p->next;
delete temp;
return;
}
else
p = p->next,k
}
this function doesn't needs to be recursive (it doesn't have a recursive nature like trees do) - you can always walk the linked list over a while and delete the nodes there.
这个函数不需要递归(它不像树一样具有递归性质) - 你总是可以在一段时间内遍历链表并删除那里的节点。
#2
0
You can do it by loop, example:
你可以通过循环来做,例如:
del(Node* p, int k) {
Node head;
head->next = p;
head->data = 0;
Node* pre = &head;
for (; p != NULL; pre = p, p = p->next) {
if (p->data == k) {
pre->next = p->next;
delete p;
p = pre;
}
}
}
#3
0
Code written on a knee, so not guaranteed to work. But the concept is simple: you create a new node which points on the root, then clear the list in while loop and finally return whatever your artificial node is pointing at, that will be the new root of your linked list.
代码写在膝盖上,所以不能保证工作。但这个概念很简单:你创建一个指向根节点的新节点,然后在while循环中清除列表,最后返回你的人工节点所指向的内容,这将是链表的新根。
Node *del(Node *p, int k) {
if (p == NULL) return;
Node keeper;
keeper.next = p;
Node parent = keeper;
do {
if (p->data == k) {
parent.next = p->next;
delete p;
p = parent.next;
}else{
parent = p;
p = p->next;
}
} while (p != NULL);
return keeper.next;
}
#1
0
while to the rescue:
在救援时:
while (p!=NULL)
{
if(p->data==k)
{
cout<<"Random Data"<<endl;
Node*temp;
temp=p;
p=p->next;
delete temp;
return;
}
else
p = p->next,k
}
this function doesn't needs to be recursive (it doesn't have a recursive nature like trees do) - you can always walk the linked list over a while and delete the nodes there.
这个函数不需要递归(它不像树一样具有递归性质) - 你总是可以在一段时间内遍历链表并删除那里的节点。
#2
0
You can do it by loop, example:
你可以通过循环来做,例如:
del(Node* p, int k) {
Node head;
head->next = p;
head->data = 0;
Node* pre = &head;
for (; p != NULL; pre = p, p = p->next) {
if (p->data == k) {
pre->next = p->next;
delete p;
p = pre;
}
}
}
#3
0
Code written on a knee, so not guaranteed to work. But the concept is simple: you create a new node which points on the root, then clear the list in while loop and finally return whatever your artificial node is pointing at, that will be the new root of your linked list.
代码写在膝盖上,所以不能保证工作。但这个概念很简单:你创建一个指向根节点的新节点,然后在while循环中清除列表,最后返回你的人工节点所指向的内容,这将是链表的新根。
Node *del(Node *p, int k) {
if (p == NULL) return;
Node keeper;
keeper.next = p;
Node parent = keeper;
do {
if (p->data == k) {
parent.next = p->next;
delete p;
p = parent.next;
}else{
parent = p;
p = p->next;
}
} while (p != NULL);
return keeper.next;
}