- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图通过递归删除甚至在双向链表中的节点。问题是它删除了节点,但在删除的节点空间中仍然显示垃圾。
这是输出:
这是原始列表:2 -> 2 -> 23 -> 2 -> 78 -> 5 -> 2
此列表包含 7 个项目
偶数的个数是:5
移除的偶数节点数为:5
结果列表是... 23 -> 5
***************删除偶数后,我得到了这个,你遇到同样的问题吗?*************** *************************
现在向后:5 -> 27303024 -> 27302960 -> 23 -> 27302928 -> 0
此列表包含 2 个项目
所有数据的总和为:28
int removeEven(node*& head) {
// double pointer to use the address of pointer head
node ** deleteNode = &head;
//if is the end of the list stop
if(head==NULL)
return 0;
//if is even
if((*deleteNode) -> data %2==0)
{
node * helper = *deleteNode;
*deleteNode = helper -> next;
delete helper;
return 1+ removeEven(helper -> next);
}
//but if is an odd number
else if ((*deleteNode) -> data %2)
{
//traverse to the next node
deleteNode = &(*deleteNode)->next;
//calls itself so that we can start againg to check in the new node.
return removeEven(head -> next);
}
}
有人告诉我像这样更改函数会有帮助,但我遇到了很多错误请帮忙
//Remove even numbers
node* recfunremoveEven(node *head,node *prevnode, int* count) //helper function for remove even nodes
{
if(head==NULL)
return NULL;
if(head->data %2 == 0) //data is even
{
*count+=1;
free(head);
node* next = recfunremoveEven(head->next,head,&count); //recursive call
if(prevnode)
{
prevnode->next = next;
next->previous = prevnode;
}
return next;
}
return recfunremoveEven(head->next,&count);
}
int removeEven(node*& head)
{
int count=0;
recfunremoveEven(head,&count);
return count;
}
编译时出现以下错误:
g++ -g -std=c++11 -o proftest dlist.h dlist.cpp main.cpp supplied.o
dlist.cpp: In function ‘node* recfunremoveEven(node*, node*, int*)’:
dlist.cpp:30:53: error: cannot convert ‘int**’ to ‘int*’ for argument ‘3’ to ‘node* recfunremoveEven(node*, node*, int*)’ node* next = recfunremoveEven(head->next,head,&count); //recursive call ^
dlist.cpp:38:42: error: cannot convert ‘int**’ to ‘node*’ for argument ‘2’ to ‘node* recfunremoveEven(node*, node*, int*)’ return recfunremoveEven(head->next,&count); ^
dlist.cpp: In function ‘int removeEven(node*&)’: dlist.cpp:45:29: error: cannot convert ‘int*’ to ‘node*’ for argument ‘2’ to ‘node* recfunremoveEven(node*, node*, int*)’ recfunremoveEven(head,&count);
这是 .h 文件,以防有人需要查看它
//doubly linked list
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
struct node
{
int data;
node * previous;
node * next;
};
/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied
//Recursively compute and return the number of nodes that contain even number
//in the doubly linked list
int countEven(node *head);
//Recursively remove all the nodes that contain even number in the doubly linked list
//and return the number of nodes removed
int removeEven(node*& head);
最佳答案
在 free(head)
之后,您不能取消引用 head
,但您可以。
您还在不应该(递归时)的地方添加了一些 &
,并且在几个函数调用中遗漏了一个参数。
但我认为您绕过“上一个”节点会使事情过于复杂。
需要处理三种情况:
第一种情况很简单。
第二个和第三个非常相似:
previous
指针。head
是偶数,删除它并增加计数。head
(如果它是奇数)或递归的结果(如果 head
是偶数)。我相信这应该可以做到(当心 - 未经测试):
node* removeEvenWorker(node* head, int* count)
{
// Case 1
if (head == nullptr)
{
*count = 0;
return head;
}
// Recurse
node* rest = removeEvenWorker(head->next, count);
// Cases 2 & 3: Set up the first node.
if (head->data % 2 != 0)
{
// Keep 'head'; link the recursive result to it.
if (rest != nullptr)
{
rest->previous = head;
}
head->next = rest;
return head;
}
else
{
// Discard 'head'
if (rest != nullptr)
{
rest->previous = nullptr;
}
free(head);
*count += 1;
return rest;
}
}
并调用它:
int removeEven(node*& head)
{
int count = 0;
head = removeEvenWorker(head, &count);
return count;
}
关于c++ - 从双向链表c++中删除偶数节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721465/
我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。 public class User { @Id @GeneratedValue(strategy=G
使用ssh转发时,我无法针对远程服务器使用cvs和ftp进行提交。是否可以让服务器对我的机器发起请求-我希望服务器上的Web应用程序调用我的机器上的REST方法。 谢谢。 尼古拉·G。 最佳答案 是的
我正在 Python 2.7.12 中实现双向 A* 算法,并在 Russell 和 Norvig 第 3 章的罗马尼亚 map 上进行测试。边具有权重,目的是找到两个节点之间的最短路径。 这是测试图
您能否建议一种映射或类似的数据结构,让我们可以轻松地相互获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自Google Gua
我想同步两个数据库运行时 服务器 A:安装了公共(public) IP 和 mysql 的 Amazon ec2。服务器B:这是局域网中带有mysql的私有(private)机器。 (IP是私有(pr
保存双向@OneToOne 映射时,hibernate 是否应该在两个表上都记录? 我有一个包含 applicant_id 列的表 interview,它引用了包含字段 interview_id 的
我喜欢新的 SwipeRefreshLayout!它看起来很棒,而且非常容易使用。但我想在两个方向上使用它。我有一个消息屏幕,我想通过从上到下滑动来加载旧消息,我想通过从下到上滑动来加载新消息。 这个
使用 ICS 4.0.1(愿意升级到 4.0.3)(不会 root 和重写 android 操作系统) 在接收到 android beam 后,是否可以将 NDEF 消息发送回 android 手机
我想知道处理这种 git 场景的最佳方法: Git 仓库:CoreProduct Git repo b: SpecificCustomerProduct 是从 a fork 出来的 到目前为止,我们一
这个问题在这里已经有了答案: How to implement an efficient bidirectional hash table? (8 个回答) 关闭2年前。 我在 python 中做这个
您能否推荐一种 map 或类似的数据结构,我们可以在其中轻松地从彼此获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自 Goo
Java中是否有类似双面列表的东西?也许第三方实现? 这里有一个小例子来证明我的想法。 原始状态: 答:0-1-2-3 | | | | 乙:0-1-2-3 删除 B 中的元素 1 后: 空值 | 答:
我有两个实体通过这样的双向 OneToOne 关联连接: @Entity class Parent { @NotNull String businessKey; @OneToO
我已将 Vagrant 配置为使用 Rsync 共享文件夹而不是(非常慢)vboxsf VirtualBox 默认提供的文件系统: Vagrant.configure("2") do |config|
@keyframes mgm { from { max-height: 250px; } to { max-height: 0px; } } .mgm {
我想了解有关使用双向 LSTM 进行序列分类时合并模式的更多详细信息,尤其是对于我还不清楚的“Concat”合并模式。 根据我对这个方案的理解: 在将前向和后向层的合并结果传递到 sigmoid 函数
我有兴趣将本地 git 存储库设置为远程存储库的镜像。我已经阅读了一些可能相关的帖子,但主要区别在于我需要对两个存储库进行读写访问。 大多数时候,用户会针对 Repo A 工作,但是有时他们会针对 R
我已经仔细阅读了文档 https://firebase.google.com/docs/database/web/read-and-write以及网上很多例子。但这里有一个脱节:在将对象添加到数据库时
这个问题已经有答案了: Hibernate bidirectional @ManyToOne, updating the not owning side not working (3 个回答) 已关闭
我知道有很多关于它的问题,但我找不到针对我的问题的好的答案。 我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供程序,因此我有简单的 @OneToM
我是一名优秀的程序员,十分优秀!