Копирование узлов в связанном списке C
Я пытаюсь дублировать узел в связанном списке. Я не уверен, правильно ли я это делаю. Я пытался сделать контрольные примеры, но они не увенчались успехом. Если кто-то может сказать мне, где я ошибся и что я сделал правильно, также, каков наилучший способ проверить мой код.
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;
}