复制链表C中的节点
我正在尝试复制链接列表中的节点。我不确定我是否做对了。我试着制作测试用例,但没有成功。如果有人能告诉我我哪里做错了,什么做对了,那么测试代码的最佳方法是什么复制链表C中的节点,c,linked-list,C,Linked List,我正在尝试复制链接列表中的节点。我不确定我是否做对了。我试着制作测试用例,但没有成功。如果有人能告诉我我哪里做错了,什么做对了,那么测试代码的最佳方法是什么 struct node { int id; char side; int quantity; double price; }; struct onode { struct node* data; struct onode* next;
struct node
{
int id;
char side;
int quantity;
double price;
};
struct onode
{
struct node* data;
struct onode* next;
struct onode* prev;
};
struct onode* newNode (struct node* data)
{
struct node* dataValue = (struct node*) malloc(sizeof(struct node));
struct onode* linkedlist = (struct onode*) malloc(sizeof(struct onode));
linkedlist ->data = (struct node*)malloc(sizeof(data)+1);
if(dataValue && data)
{
*dataValue = *data;
}
}
我对代码进行了更改,并添加了更多关于此函数所需内容的描述。
一个变化:struct节点是struct order
struct order
{
int id;
char side;
int quantity;
double price;
};
struct onode
{
struct order* data;
struct onode* next;
struct onode* prev;
};
/**
* Returns a new linked list node filled in with the given order, The function
* allocates a new order and copy the values stored in data then allocate a
* linked list node. If you are implementing this function make sure that you
* duplicate, as the original data may be modified by the calling function.
*/
struct onode* newNode (struct order* data)
{
struct order* dataValue = (struct order*) malloc(sizeof(struct order));
struct onode* linkedlist = (struct onode*) malloc(sizeof(struct onode));
*dataValue = *data;
linkedlist ->data = dataValue;
linkedlist->data->id = dataValue->id;
linkedlist->data->price = dataValue->price;
linkedlist->data->quantity = dataValue->quantity;
linkedlist->data->side = dataValue->side;
linkedlist->next->prev = NULL;
return linkedlist;
}
这不是正确的答案,因为您的代码根本不包含回答问题所需的代码/解释
struct onode* newNode (struct node* data)
{
struct order* dataValue = (struct node*) malloc(sizeof(struct node));
}
什么是结构顺序*??你是说结构节点*
struct onode* linkedlist = (struct onode*) malloc(sizeof(struct onode));
linkedlist ->data = (struct node*)malloc(sizeof(data)+1);
上面的行似乎错误-sizeof(data)+1
?它不是字符串,因此添加一个字符串没有意义,并且大小是指针的大小,这可能不是您想要的。我猜你想要linkedList->data=dataValue代码>取而代之
您需要在linkedList
中设置next
和prev
指针
if(dataValue && data)
{
*dataValue = *data;
}
您可能需要返回节点
正如卡尔指出的,你不应该把返回值从MalCube()/<代码> >——如果你的编译器<代码>抱怨< /代码>,那大概是因为你把代码编译成C++而不是C.<
编辑:在更新的代码中:
*dataValue = *data;
A
B
C
A和B做同样的事情,所以其中一个是多余的
C几乎肯定会使代码崩溃,因为next
没有被设置为任何值。您可能想使用linkedlist->next=NULL
和linkedlist->prev=NULL
问题的关键在于您正在创建两个新的节点
对象,一个是dataValue
,另一个是linkedlist->data
。然后,当您确实希望将传入的数据存储在linkedlist->data
中时,将其复制到dataValue
中
如果你更换
linkedlist ->data = (struct node*)malloc(sizeof(data)+1);
与
这将使您朝着正确的方向前进。因为struct order是一种POD类型,所以事情非常简单
struct onode* newNode (struct order* data)
{
struct order* dataValue;
struct onode* linkedlist;
If (!data)
{
/* Feel free to use any other strategy to
* handle data == NULL case depending
* on the needs of your application. */
return NULL;
}
dataValue = malloc(sizeof(struct order));
linkedlist = malloc(sizeof(struct onode));
memcpy(dataValue, data, sizeof(*dataValue))
linkedlist->data = dataValue;
linkedlist->next = NULL;
linkedlist->prev = NULL;
return linkedlist;
}
你的测试用例是什么?结果如何?我的测试用例从未编译过。所以我删除了它来检查。我想我不知道如何做一个关于它的测试用例。我所做的是创建一个名为newnode的新节点(即将成为数据节点)并传递我创建的节点。你不需要在C程序中强制转换malloc的返回值。什么是“结构顺序”?@Eciliptus:一定要告诉你的教授,这样他就可以看到他让你这么做有多傻了。
linkedlist ->data = (struct node*)malloc(sizeof(data)+1);
linkedList->data = dataValue;
struct onode* newNode (struct order* data)
{
struct order* dataValue;
struct onode* linkedlist;
If (!data)
{
/* Feel free to use any other strategy to
* handle data == NULL case depending
* on the needs of your application. */
return NULL;
}
dataValue = malloc(sizeof(struct order));
linkedlist = malloc(sizeof(struct onode));
memcpy(dataValue, data, sizeof(*dataValue))
linkedlist->data = dataValue;
linkedlist->next = NULL;
linkedlist->prev = NULL;
return linkedlist;
}