在链表Ç复制节点 [英] Copying nodes in linked list C
问题描述
我试图复制在链表中的一个节点。我不知道如果我做正确。我试图使测试案例,但他们才没有成功。如果某些人能告诉我,如果我哪里错了,我做了什么吧,还什么来测试我的code的最佳方式。
结构节点
{
INT ID;
字符的一面;
INT数量;
双价;
};结构onode
{
结构节点*数据;
结构onode *接下来的;
结构onode * preV;
};结构onode * newNode(结构节点*数据){
结构节点* dataValue =(结构节点*)malloc的(的sizeof(结构节点));
结构onode *链表=(结构onode *)malloc的(的sizeof(结构onode)); 链表 - >数据=(结构节点*)malloc的(的sizeof(数据)+1); 如果(dataValue&安培;&安培;数据)
{
* dataValue = *的数据;
}
}
我在我的code所做的更改,并添加更多的描述此功能需要什么。
一个改变:结构节点结构秩序
结构秩序
{
INT ID;
字符的一面;
INT数量;
双价;
};结构onode
{
为了结构*数据;
结构onode *接下来的;
结构onode * preV;
};
/ **
*返回给定的顺序,功能填补了一个新的链接列表节点
*分配一个新的订单,并复制存储在数据中的值,然后分配
*链表节点。如果要实现此功能确保您
*重复,作为原始数据可以由调用功能进行修改。
* /结构onode * newNode(结构顺序*数据)
{
为了结构* dataValue =(结构顺序*)malloc的(的sizeof(结构顺序));
结构onode *链表=(结构onode *)malloc的(的sizeof(结构onode)); * dataValue = *的数据; 链表 - >数据= dataValue; linkedlist->数据 - > ID = dataValue-> ID;
linkedlist->数据 - >价格= dataValue->价格;
linkedlist->数据 - >数量= dataValue->数量;
linkedlist->数据 - >侧= dataValue->的一面;
linkedlist->下一步 - > preV = NULL; 返回LinkedList的;}
你的问题的关键是,你要创建两个新的节点
对象 - 一个是 dataValue
键,一个是 linkedlist->数据
。然后,传入的数据复制到 dataValue
当你真的希望它被保存在 linkedlist->数据
如果您替换
链表 - >数据=(结构节点*)malloc的(的sizeof(数据)+1);
与
linkedList->数据= dataValue;
这应该让你在正确的方向前进。
I am trying to duplicate a node in linked list. I am not sure if I am doing it correctly. I tried making test cases but did they were not successful. If some one could tell me if where I went wrong and what I did right, also what is the best way to test my code.
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;
}
}
I have made changes in my code and added more description on what this function wants. one change : struct node is 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;
}
The crux of your problem is that you are creating two new node
objects--one that is dataValue
and one that is linkedlist->data
. You then copy the passed-in data into dataValue
when you really want it to be stored in linkedlist->data
.
If you replace
linkedlist ->data = (struct node*)malloc(sizeof(data)+1);
with
linkedList->data = dataValue;
that should get you moving in the right direction.
这篇关于在链表Ç复制节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!