我正在尝试复制链表中的节点。我不确定我是否做得正确。我尝试制作测试用例,但没有成功。如果有人能告诉我我哪里出了问题以及我做对了什么,那么测试我的代码的最佳方法是什么。
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 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;
}