我正在做一个项目,该项目需要我制作一个 newNode 并按顺序分配该节点。我不确定我是否做得正确。另外,我怎样才能为这个函数做一个测试用例来测试我写的代码?
/*
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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
struct order
{
int id;
char side;
int quantity;
double price;
};
struct onode
{
struct order* data;
struct onode* next;
struct onode* prev;
};
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;
}
另外,这样有效吗?我还能改变什么来使这个程序更好?