#Linked lists in c

22 messages · Page 1 of 1 (latest)

grand lion
#

Hi, im learning about linked lists, and trying to understand how to add elements , or in this care info about a car at the tail end of a linked list. Could anyone point me in the right direction please on how they work, and how i can sort of proceed here?

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

#define MAX_LENGTH 25

struct car
{
   int year;
   double price;
   char model[MAX_LENGTH];
   char type[MAX_LENGTH];

   int carId;              // this is auto-generated by the program
               // must be unique for each car
   struct car * nextCar;
                            
};


void addNewCar (struct car ** headLL){
    /* This option calls a function named addNewCar that prompts the user to enter car data from
    standard input and add their information to the end of the linked list passed as a parameter to
    this function. The user enters the model, type of the car, its price and year of manufacture

    This function must store new cars at the tail end of the current linked list.*/

    // we can start by creating a new node that'll hold the car info, and allocate memory too
    struct car *newNode = (struct car*)malloc(sizeof(struct car));
    // if the new car or node is empty, we need to check
    if (newNode == NULL){
        printf("memory couldnt be allocated\n");
        return;
    }
    printf("Enter car model\n");
    scanf("%s", newNode->model);
    printf("Enter car type:\n");
    scanf("%s", newNode->type);
    printf("Enter its year of manufacture:\n");
    scanf("%d", &newNode->year);
    printf("Enter price: CDN $");
    scanf("%lf", newNode->price);


}```
opaque fableBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pale crescent
#

I'm on mobile, so I may have missed something. But from what I'm seeing, you're missing two things. You need a way to get to the tail of your list (whether this be by iterating through, or keeping a pointer to the tail). You also need to attach your new node to the tail.

If you need any clarification, let me know!

grand lion
#

hows it looking

#
    This function must store new cars at the tail end of the current linked list.*/

    // we can start by creating a new node that'll hold the car info, and allocate memory too
    struct car *newNode = (struct car*)malloc(sizeof(struct car));
    // if the new car or node is empty, we need to check
    if (newNode == NULL){
        printf("memory couldnt be allocated\n");
        return;
    }
    printf("Enter car model\n");
    scanf("%s", newNode->model);
    printf("Enter car type:\n");
    scanf("%s", newNode->type);
    printf("Enter its year of manufacture:\n");
    scanf("%d", &newNode->year);
    printf("Enter price: CDN $");
    scanf("%lf", &newNode->price);

    // carId = (sum of ascii values of characters in the car model) + (length of the car’s type)
    int carId = 0;
    for (int j = 0; j < strlen(newNode->model); j++){
        carId = carId + newNode->model[j];
    }
    for (int i =0; i < strlen(newNode->type); i++){
        carId = carId + newNode->type[i];
    }

    // need to set the next ptr after the new node to null
    newNode->nextCar = NULL;

    if(*headLL == NULL){
        *headLL = newNode; // if the list is empty, we make the new node the head
    }
    else{
        struct car *lastCar = *headLL;
        while(lastCar->nextCar != NULL){
            lastCar = lastCar->nextCar; // we traverse to the last node
        }
        lastCar->nextCar = newNode; // linking the new node to the end of the list
    }
opaque fableBOT
#

@grand lion Has your question been resolved? If so, type !solved :)

grand lion
#

not yet

pale crescent
#

Just on transit right now, will be taking a look once I'm home 🫡

pale crescent
#

I don't see any obvious issues with this! Does it run as expected?

grand lion
#

But i belive ill have to do function 3 to see if its actually running correctly (my function 3 prints the linked list, or data from all the cars in this case)

#
void printAll(struct car * headLL){
    printf("function stub for function 3\n");
}```
#

ill give this a shot first prolly and see if the new car i added from option 1 in the menu, is working correctly

#

although ill have to learn how to print data from a linked list

#

is it similar to adding an element to the end of a LL?

grand lion
#

oh that wasnt too bad

#

i sort of just traversed through the list as long as it wasnt NULL

#

and printed eveything

#
void printAll(struct car * headLL){
    /*if option 3 is selected, a function called printAll is called. This function prints the data of all cars
    currently in the LL.
    Note that if the linked list is empty, then the function must print a message “No cars found"*/   

    struct car * current = headLL;

    // we need to check if the list is empty first, so lets go ahead and do that
    if (current == NULL){
        printf("No cars Found\n");
        return;
    }

     // Iterate through the linked list
    while(current != NULL){ // While the current node is not NULL
        // Print the details of the current car
        printf("Car Id: %d \n", current->carId);
        printf("Car year: %d \n", current->year); // Print the car's year
        printf("Car price: %lf \n", current->price); // Print the car's price
        printf("%s \n", current->model); // Print the car's model
        printf("%s \n", current->type); // Print the car's type
        
        // moving onto the next node below
        current = current->nextCar;
    }
}``` looks fine?
pale crescent
#

This looks good to me! You've got it figured out