c - 如何在 C 中制作队列的副本?
问题描述
我正在尝试将一个队列复制到另一个队列中,以便如果我销毁一个队列,它不会破坏另一个队列。
我尝试使用memcpy
这种方式:
memcpy(queue1, queue2, sizeof(queue2));
但我收到以下错误:
预期的“无效*限制”,但参数是“队列”类型
是否可以将一个队列复制到另一个队列中?
编辑
这是队列的结构:
typedef char *Following;
typedef struct _NodeQ {
Following element;
struct _NodeQ * next;
} NodeQ;
typedef struct {
int error;
NodeQ * first;
NodeQ * last;
} Queue;
解决方案
memcpy
绝对不是这样,因为那只会让您执行“浅拷贝”。如果您认为您的对象可以存储指向外部数据的指针,则该对象的浅拷贝将指向完全相同的外部数据,而不是复制所有内容(“深拷贝”)。
执行深拷贝的方法完全取决于实现。似乎您的队列正在使用简单的链表实现(查看每个NodeQ
结构如何包含指向另一个NodeQ
结构的指针,该结构具有指向另一个结构的指针,依此类推)。这在 C 中很常见。困难的部分是弄清楚如何复制队列中每个对象的“数据”部分(在本例中为Following
类型变量)。我不知道您使用的是哪种库或框架来定义Queue
类型,您应该参考一些文档来帮助您了解具体细节。谁知道呢,也许你会找到一种受图书馆支持的方式来复制你想要的方式。不过,我会尝试做你想做的,有一些假设。
从此开始猜测
这只是一个猜测,但它似乎Following
可以表示一个字符串,因为它是一个 char 指针。我在下面编写的代码使用了这个假设。请注意,尽管它未经测试且有点仓促,但我只是想说明在您的场景中执行手动深度复制的情况。
Queue queue2;
NodeQ *e2, *e2prev = NULL, *e2first = NULL;
// Iterate through all elements of the first queue
for (NodeQ *e1 = queue1.first; e1; e1 = e1->next) {
// Allocate memory for the new element of queue 2
if (!(e2 = (NodeQ*)malloc(sizeof(NodeQ)))) {
printf("not enough memory\n");
}
// Remember the first element of queue for future use
if (!e2first)
e2first = e2;
// Note that strlen here could cause a segfault if my guess that "element" is a string is wrong
size_t buflen = strlen(e1->element) + 1; // +1 for null terminator
if (!(e2->element = (Following)malloc(sizeof(char) * buflen))) {
printf("not enough memory\n");
}
strcpy(e2->element, e1->element);
e2->next = NULL;
// Link new element with previous element
if (e2prev)
e2prev->next = e2;
e2prev = e2;
}
queue2.error = queue1.error;
queue2.first = e2first;
queue2.last = e2;
推荐阅读
- apache-spark - Array_max spark.sql.function 未找到
- wpf - 自定义样式的组合框变得不可见 - WPF?
- angular - 角度接口属性未定义
- nuget - 命令行中的 NuGet.exe 给出“现有连接被远程主机强行关闭”错误
- java - 如何确定文件在 Java 中的 Azure 文件存储上是否完整?
- r - 如何将mlogit结果导出到excel?
- java - 如何在 java android 中发送另一个通知回复?
- angularjs - 如何在angular9中实现redux?
- java - 使用 IntelliJ 和 gradle 向我的第一个 Java11 模块添加依赖项时出现问题
- javascript - 如何从 localStorage 数组中删除项目?