简体   繁体   中英

Copying nodes in linked list C

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.

This isn't a proper answer, since your code simply doesn't contain the necessary code/explanation to answer your question.

struct onode* newNode (struct node* data)
{
    struct order* dataValue  = (struct node*) malloc(sizeof(struct node));
}

What is struct order* ?? You mean struct node * ?

    struct onode* linkedlist = (struct onode*) malloc(sizeof(struct onode));

    linkedlist ->data = (struct node*)malloc(sizeof(data)+1);

The above line seems wrong - sizeof(data) + 1 ? It's not a string, so adding one is not meaningful, and the size is the size of the pointer, which is probably not what you want. I'm guessing you want linkedList->data = dataValue; instead.

And you need to set the next and prev pointers in linkedList .

    if(dataValue && data)
    {
        *dataValue = *data;
    }

You are probably supposed to return the node.

As Carl pointed out, you shouldn't cast the return value from malloc() - if your compiler complains about it, it's probably because you are compiling the code as C++ rather than as C.

Edit: in the updated code:

*dataValue = *data;

A

linkedlist ->data = dataValue;

linkedlist->data->id = dataValue->id;
linkedlist->data->price = dataValue->price;
linkedlist->data->quantity = dataValue->quantity;
linkedlist->data->side = dataValue->side;

B

linkedlist->next->prev = NULL; 

C

A and B does the same thing, so one of them is redundant.

C is almost certainly going to crash your code, since next has not been set to anything. You probably want to use linkedlist->next = NULL and linkedlist->prev = NULL

Because struct order is a POD type, things are quite simple.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM