I'm not sure if this is a duplicate question, so if it is, please feel free to direct me in the proper place, but I couldn't find an answer to my question.

I am currently trying to switch from using an array to a linked list. (This is for an assignment, I am just very unclear on what I am doing)

In my original function, I asked for input from a user to get person information (name, year of birth, gender, province). I would then use pointers and to store this information in the main in an array.

Now I need to make a new structure for the province and remove that from the person struct. The Idea is now that I will have a linked list of provinces that contain linked lists of people.

I believe I know how to make a person, but I don't know how to connect the person information to my province information.

In my main:

    CitizenNode     *cit = NULL;
    DistrictNode    *dist = NULL;

    Citizen         *newCitizen;
    CitizenNode     *newCitNode;
    District        *newDist;
    DistrictNode    *newDistNode;

and in the function that uses the nodes:

//loop through the provinces
for (j = 0; j < MAX_PROVINCES-1; j++){
    for(i = 0; i < numCitizens; i++){
            Citizen *newCit = &c_ptr[*citsize_ptr];
            //all of these pointers need to be changed, but I'm unsure of how to yet
            scanf("%s %d %d %c", newCit->name, &newCit->district, &newCit->yearOfBirth, &newCit->gender);   

            //allocate memory for each citizen
            newCitNode = (CitizenNode *) malloc(sizeof(CitizenNode));
            newCitNode->data = newCitizen;
            newCitNode->next = NULL;
            rc = addCitizen(newCitNode, &cit);

                if (rc < 0)
    newDistNode = (DistNode *) malloc(sizeof(DistNode);
    newDistNode->dataList = *citizenList;
    newDistNode->next = NULL;
    addDistNode(newDistNode, &dist);

How do I connect these lists?

Thank you for any help!

The way I have done this in the past is to put a linked list of Citizens inside the District structure, something like below. Note that unless you were instructed to do otherwise you don't need both a CitizenNode and a Citizen structure, just one or the other.

struct Citizen
   // blabla

   struct Citizen* next;

 struct District
    struct District* next;
    struct Citizen* head; // <<<< linked list of Citizen structures

struct District* head = NULL; // <<< Linked list of District structures
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.