【问题标题】:Copying nodes in linked list C复制链表 C 中的节点
【发布时间】:2013-03-10 22:44:09
【问题描述】:

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

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;

}

【问题讨论】:

  • 你的测试用例是什么?结果如何?
  • 我的测试用例从未编译过。所以我删除它来检查。我想我不确定如何制作一个关于它的测试用例。我所做的是创建一个名为 newnode 的新节点(将成为数据节点)并传递我创建的节点。
  • 您不需要在 C 程序中强制转换 malloc 的返回值。
  • 什么是“结构顺序”?
  • @Eciliptus:一定要给你的教授this link,这样他就可以看到他要求你这样做是多么愚蠢。

标签: c linked-list


【解决方案1】:

问题的症结在于您正在创建两个新的node 对象——一个是dataValue,一个是linkedlist->data。然后,当您确实希望将传入的数据存储在 linkedlist->data 中时,将传入的数据复制到 dataValue

如果你替换

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

linkedList->data = dataValue;

这应该会让你朝着正确的方向前进。

【讨论】:

    【解决方案2】:

    这不是一个正确的答案,因为您的代码根本不包含回答您的问题所需的代码/解释。

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

    struct order* 是什么?你是说struct node *

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

    上面的行似乎是错误的 - sizeof(data) + 1?不是字符串,所以加一个没有意义,大小就是指针的大小,可能不是你想要的。我猜你想要linkedList->data = dataValue;

    您需要在linkedList 中设置nextprev 指针。

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

    您可能应该返回节点。

    正如 Carl 指出的那样,您不应该从 malloc() 转换返回值 - 如果您的编译器 complains 关于它,可能是因为您将代码编译为 C++ 而不是 C。

    编辑:在更新的代码中:

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

    B

    linkedlist->next->prev = NULL; 
    

    C

    A 和 B 做同样的事情,所以其中一个是多余的。

    C 几乎肯定会让你的代码崩溃,因为next 没有被设置为任何东西。您可能想使用linkedlist->next = NULLlinkedlist->prev = NULL

    【讨论】:

      【解决方案3】:

      因为 struct order 是 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多