【问题标题】:Copy an array to linked List in each data field将数组复制到每个数据字段中的链表
【发布时间】:2019-05-17 17:33:00
【问题描述】:

首先,很抱歉,如果我的问题已经得到解答。我发现了一些(在某种程度上)相似的线程,但我无法解决我的问题。 其次,我是 C 中单链表的新手,所以如果您能尽可能简单地回答我的问题,我会很高兴。

我做了一个简单的链接列表,里面有字符:

#include <stdio.h>
#include <stdlib.h> 

// declaration of node
struct _Node_ 
{
char data_string;
struct _Node_ *next;
};
int main() {
//a simple linked list with 3 Nodes, Create Nodes
struct _Node_* head = NULL; 
struct _Node_* second = NULL; 
struct _Node_* third = NULL;

//allocate 3 Nodes in the heap
head = (struct _Node_*)malloc(sizeof(struct _Node_));  
second = (struct _Node_*)malloc(sizeof(struct _Node_)); 
third = (struct _Node_*)malloc(sizeof(struct _Node_)); 

// assign data for head
head->data_string = 'H';      //assign value according struct
head->next = second;          //points to the next node

// assign data for second
second->data_string = 'E';
second->next = third;

third->data_string = 'Y';
third->next = NULL;

return 0;
}

链接列表现在看起来像这样:

 /* Linked list _Node_

       head         second           third
         |            |                |
         |            |                |
    +---+---+     +---+---+       +----+------+ 
    | 1 | o-----> |  2| o-------> |  3 | NULL | 
    +---+---+     +---+---+       +----+------+        

  */

假设我有 3 个数组,其中包含以下内容:

char name1[] = "Joe";
char name2[] = "Eve";
char name3[] = "Brad"; 

而我的目标是将这个数组复制到每个数据字段中,所以结果如下所示:

 /* Linked list _Node_

       head            second              third
         |               |                   |
         |               |                   |
    +-----+---+     +-------+---+     +-------+------+ 
    | Joe | o-----> |  Eve  | o-----> |  Brad | NULL | 
    +-----+---+     +-------+---+     +-------+------+        

  */

我怎样才能做到这一点?我已经尝试添加/更改以下内容:

...

struct _Node_ 
{
char data_string[8];
struct _Node_ *next;
};    

...

...

char name1[] = "Joe";
char name2[] = "Eve";
char name3[] = "Brad";

// assign data for head
head->data_string = name1;      //assign value according struct
head->next = second;          //points to the next node

// assign data for second
second->data_string = name2;
second->next = third;

third->data_string = name3;
third->next = NULL;

...

但我编译后得到的只是:

stack_overflow.c:27:23: error: array type 'char [8]' is not assignable
head->data_string = name1;      //assign value according struct
~~~~~~~~~~~~~~~~~ ^
stack_overflow.c:31:25: error: array type 'char [8]' is not assignable
second->data_string = name2;
~~~~~~~~~~~~~~~~~~~ ^
stack_overflow.c:34:24: error: array type 'char [8]' is not assignable
third->data_string = name3;
~~~~~~~~~~~~~~~~~~ ^
3 errors generated.

也许有人可以提供帮助,我很感激任何帮助。 再次,对不起,如果这是重复的,但我无法用其他线程解决这个问题..

【问题讨论】:

  • 将其设为char *data_string 并使用malloc 为其分配内存并使用strcpy 将字符串复制到分配的内存中。
  • 注意:您分配的操作复制数据。所以head-&gt;data_string = name1; 是错误的。使用strcpy(head-&gt;data_string, name1);
  • 问题是 /*char data_string;*/ 它只包含一个 char-bad 名称。如果您希望它指向一个字符串,您可以使用 char* str - 并将其设置为指向外部设置字符串或在根据其长度初始化节点后为该字符串分配内存。您也可以将 char var 设为一组容量 char string[10] 或 example- 但它适合短于或等于 9 的名称,并且您会记住较短的名称。
  • 好的,我将 char data_string[8]; 更改为 char *data_string;,然后我将每个名称数组从 head-&gt;data_string = name1; 更改为 strcpy(head-&gt;data_string, name1); 我必须在哪里放置 malloc 声明?
  • @PaulOgilvie 你能给我一个代码示例吗?我想我使用 malloc 错误,我得到了 Segmentation Fault..

标签: c arrays linked-list character singly-linked-list


【解决方案1】:

您要求提供代码示例:

struct Node 
{
    char *data_string;
    struct Node *next;
};

struct Node *newNode (char *s)
{
    struct Node *node;
    if (!s) return 0; // error: no string
    if (!(node= malloc(sizeof(struct Node)))) return 0; // no more memory
    node->data_string= malloc(strlen(s)+1);
    if (!node->data_string) {
        free(node);
        return 0;
    }
    strcpy(node->data_string,s);
    node->next= 0;
    return(node);
}
void freeNode(struct Node *node)
{
    if (!node) return;
    if (node->data_string) free(node->data_string);
    free(node);
}

注意事项:

  • 您为字符串 1 分配的内存超过了长度,因为 C 中的字符串有一个空终止字符。

  • 不要在标识符名称前使用下划线 - 这些是为编译器保留的。

  • 不要转换malloc 的结果。它返回一个void 指针,它兼容任何指针类型。

  • 此示例包括所有必需的错误检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多