Copy an array to linked List in each data field

冷暖自知 提交于 2021-01-28 09:16:36

问题


First, sorry if my question has already been answered. I found some threads that are (in a way) similiar but I was not able to solve my problem. Second, I am new to single-linked-list in C so I would be happy if you could answer my question as easy as possible.

I made a simple linke-list, that has characters in it:

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

The linked list does now look like this:

 /* Linked list _Node_

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

  */

Let's assume I have 3 arrays with the following:

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

And my goal is to copy this array into each data field, so the result looks like this:

 /* Linked list _Node_

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

  */

How can I achieve this? I already tried adding/changing the following:

...

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;

...

But all I get after compiling is:

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.

Maybe someone could help, I appreciate any help. Again, sorry if this is a duplicate but I am not able to solve this with other threads..


回答1:


You asked for a code example:

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

Notes:

  • you alocate the memory for the string 1 more than the length because a string in C has a null terminating character.

  • do not use underscores before an identifier name - those are reserved for the compiler.

  • do not cast the result of malloc. It returns a void pointer, which is compatible with any pointer type.

  • this example includes all required error checking.



来源:https://stackoverflow.com/questions/53813720/copy-an-array-to-linked-list-in-each-data-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!