I'm not really getting the bigger picture with regards to linked lists. What needs to be done to link it? Also, there is an error with strncpy and for some reason on line 35 its not recognising struct trainset anymore and calls it a undefined variable.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct trainset {
    char name[50];
    int price;
    struct trainset *next;
};

void show_list(struct trainset *list);

int main (void){

    struct trainset *root;
    root = (struct trainset *)malloc(sizeof(struct trainset));
    strncpy(root ->name, " ", 50);
    root ->price = 0;
    root ->next = 0;

    struct trainset *first_train;
    first_train = (struct trainset *) malloc(sizeof(struct trainset));
    strncpy(first_train->name, "Fantasy Train Set", 50);
    first_train->price = 129;
    first_train->next = NULL;

    struct trainset *second_train;
    second_train = (struct trainset *)malloc(sizeof(struct trainset));
    first_train->next = second_train;
    strncpy(first_train->name, "Uncle Bobs train set", 50);
    second_train ->price = 69;
    second_train ->next = NULL;

    struct trainset *third_train;
    third_train = (struct trainset *)malloc(sizeof(trainset));
    second_train->next = third_train;
    strncpy(third_train ->name, "Budha Bread Train", 50);
    third_train ->price = 169;
    third_train ->next = NULL;

    show_list(root);
    return 0;
}

void show_list(struct trainset *list)
{
    while(list->next!=NULL)
    {
        printf("train set name: %s\nprice: %d", list->name, list->price);
        list = list->next;
    }
    printf("trainset name name: %s\n", list->name);
}

Recommended Answers

All 3 Replies

#include <string.h> 

(so the compiler knows about the function)

sizeof (struct trainset)

As mentioned above, include string.h and replace "trainset" with "struct trainset" on line 34. Then, as far as your actual question goes:

What needs to be done to link it?

You're not linked the items you created to the root node. Add

root->next = first_train;

Also, you're assigning names to the wrong items.

strncpy(first_train->name, "Uncle Bobs train set", 50);

should be the second train.

Finally, in your loop condition you should check if the current item is not null, as that is the one you're dereferencing.

These changes result in the following:

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

struct trainset
{
    char name[50];
    int price;
    struct trainset *next;
};

void show_list(struct trainset *list);

int main (void){

    struct trainset *root;

    root = (struct trainset *)malloc(sizeof(struct trainset));
    strncpy(root->name, " ", 50);
    root ->price = 0;
    root ->next = 0;

    struct trainset *first_train;
    first_train = (struct trainset *) malloc(sizeof(struct trainset));
    root->next = first_train;
    strncpy(first_train->name, "Fantasy Train Set", 50);
    first_train->price = 129;
    first_train->next = NULL;

    struct trainset *second_train;
    second_train = (struct trainset *)malloc(sizeof(struct trainset));
    first_train->next = second_train;
    strncpy(second_train->name, "Uncle Bobs train set", 50);
    second_train ->price = 69;
    second_train ->next = NULL;

    struct trainset *third_train;
    third_train = (struct trainset *)malloc(sizeof(struct trainset));
    second_train->next = third_train;
    strncpy(third_train ->name, "Budha Bread Train", 50);
    third_train ->price = 169;
    third_train ->next = NULL;

    show_list(root);
    return 0;
}

void show_list(struct trainset *list)
{
    while(list != NULL)
    {
        printf("train set name: \"%s\", price: %d.\n", list->name, list->price);
        list = list->next;
    }
}
root->next = first_train;

and
at 29 line

strncpy(second_train->name, "Uncle Bobs train set", 50);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.