简体   繁体   English

复制链表C中的节点

[英]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节点是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 . 问题的症结在于,您正在创建两个新的node对象-一个是dataValue ,另一个是linkedlist->data You then copy the passed-in data into dataValue when you really want it to be stored in linkedlist->data . 然后,当您确实希望将传入的数据存储在linkedlist->data时,可以将其复制到dataValue

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* ?? 什么是struct order* You mean struct node * ? 您的意思是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 ? 上一行似乎是错误的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; 我猜你想要linkedList->data = dataValue; instead. 代替。

And you need to set the next and prev pointers in linkedList . 并且您需要在linkedList设置nextprev指针。

    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. 正如Carl指出的那样,您不应该从malloc()返回值-如果您的编译器complains它,那可能是因为您将代码编译为C ++而不是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 C

A and B does the same thing, so one of them is redundant. A和B做相同的事情,因此其中之一是多余的。

C is almost certainly going to crash your code, since next has not been set to anything. C几乎肯定会崩溃您的代码,因为next尚未设置为任何东西。 You probably want to use linkedlist->next = NULL and linkedlist->prev = NULL 您可能要使用linkedlist->next = NULLlinkedlist->prev = NULL

Because struct order is a POD type, things are quite simple. 因为结构顺序是POD类型,所以事情非常简单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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