Home > OS >  linked list of arrays in c
linked list of arrays in c

Time:03-02

i have problem with assigning an array as an element of a linked list. I've tried changing char to char* but it didn't help me. I would really appreciate your name here i created a struct

struct node{
char data;
struct node *next;
};

and added this function to add new nodes

void addLast(struct node **head, char val)
{
//create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = val;
newNode->next     = NULL;

//if head is NULL, it is an empty list
if(*head == NULL)
     *head = newNode;
//Otherwise, find the last node and add the newNode
else
{
    struct node *lastNode = *head;

    //last node's next address will be NULL.
    while(lastNode->next != NULL)
    {
        lastNode = lastNode->next;
    }

    //add the newNode at the end of the linked list
    lastNode->next = newNode;
}

}

and this is how to pass the data to function

int main()
{
 struct node *head = NULL;
 char name[10];

 printf("Enter book title : ");
 scanf("%s",&name);
 addLast(&head,name);
 break;
  
 return 0;
}

and this is the error i get

error: invalid conversion from 'char*' to 'char' [-fpermissive]
  |             addLast(&head,name);
  |                           ^~~~
  |                           |
  |                           char*
note:   initializing argument 2 of 'void addLast(node**, char)'
void addLast(struct node **head, char val)

CodePudding user response:

In order to store the book title you need to reserve space for the whole title and not just one letter

Change

struct node{
char data;
struct node *next;
};

To

struct node{
char* data;
struct node *next;
};

Now when reading the name, allocate memory in your new node for the text

void addLast(struct node **head, char* val)
{
//create a new node
int len = strlen(val);
struct node *newNode = malloc(sizeof(struct node));
newNode->data = malloc(len 1); // text length   ending \0
// copy text
strcpy_s(newNode->data, len 1, val );
newNode->next     = NULL; 

Some other observations

Keep a pointer to the last element, that makes appending faster. 10(9) chars may be a bit small buffer for a book title

CodePudding user response:

Just redeclare the structure like

#include <strio.h>
#include <stdlib.h>
#include <string.h>

//...

#define  NAME_LENGTH 10

struct node{
    char name[NAME_LENGTH];
    struct node *next;
};

In this case the function will look like

int addLast( struct node **head, const char *name )
{
    //create a new node
    struct node *newNode = malloc( sizeof( struct node ) );
    int success = newNode != NULL;

    if ( success )
    {
        strncpy( newNode->name, name, NAME_LENGTH );
        newNode->name[NAME_LENGTH - 1] = '\0';
        newNode->next = NULL;

        //if head is NULL, it is an empty list
        if ( *head == NULL )
        {
            *head = newNode;
        }
        //Otherwise, find the last node and add the newNode
        else
        {
            struct node *lastNode = *head;

            //last node's next address will be NULL.
            while ( lastNode->next != NULL )
            {
                lastNode = lastNode->next;
            }

            //add the newNode at the end of the linked list
            lastNode->next = newNode;
        }
    }

    return success;
}
  • Related