Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:26:31+00:00 2026-06-13T01:26:31+00:00

I’m having trouble modifying members of a linked list. We have a struct record

  • 0

I’m having trouble modifying members of a linked list. We have a struct “record” that is declared as the following:

typedef struct record
{
    char *make;
    int year;
    int stock;
    struct record *next;
} Record;

that holds information about cars for a dealership.

We first create the linked list using input from a CLI argument-indicated text file that is exactly like this:

Ford 2012 15
Ford 2011 3
Ford 2010 5
Toyota 2011 7
Toyota 2012 20
Toyota 2009 2
Honda 2011 9
Honda 2012 3
Honda 2013 12
Chevrolet 2013 10
Chevrolet 2012 25

These values are read into variables using fgets and then sent down to the “insertRecordInAsendingOrder” function, which then calls “createRecord” to actually create each individual record in the linked list. insertRecordInAscendingOrder then places the records into the proper spot on the list. The original list is then printed out in sorted order using the function “printList”.

From here, we proceed on to update the stock of one of the members of the linked list. The values “Ford” (for make), “2012” (for year), and “12” (for the newStock field) are hard-coded into the function “Main”, and are sent down to the “updateStockOfCar” function, which is the following code:

int updateStockOfCar(Record *head, char *make, int year, int newStock)
{
        if(head == NULL)
        {
            printf("The list is empty\n");
            return 0;
        }

        Record * matching = malloc(sizeof(Record));
        matching->make = make;
        matching->year = year;

        //If the first record is the one we want to modify, then do so
        if(strcmp(head->make, matching->make) == 0)
        {
            if(head->year == matching->year)
            {
                head->stock = newStock;
                return 1;
            }

            Record* previous = head;
            Record* current = head->next;

                //Loop through the list, looking for the record to update
            while(current != NULL)
            {
                if(strcmp(current->make, matching->make) == 0 && current->year  == matching->year)
                {
                    current->stock = newStock;
                    return 1;
                }
                else
                {
                    previous = current;
                    current = current -> next;
                }
            }
        }
        return 0;
}

As you can see, this function takes in the head node of the list (where we start our search for the correct node to update), make & year (for the node we’re targeting to modify the stock of), and newStock (the value we’re going to use to replace the standard “stock” field in the targeted node).

Our first case is if head is NULL, in which case, we say that the list is empty and return an unsuccessful “0” back to the calling variable in “Main”.

Our second case is if the strcmp between the make fields in head in matching are the same, and the year fields in head and matching are the same, then we update head’s stock field to newStock and return a successful “1” back to funciton “Main”. In essence, if the head node is the right node to modify, we do so.

Our third case is if the head node is NOT the correct node to modify. On the condition that current (defined as the head to start with) is not NULL, we compare the current node’s make and year fields to the targeted node we want to change. If they match, we set current’s stock field to newStock and return a successful “1”. If they do not match, we go through the “else” condition, traversing through the list until we find the one we’re looking for, in which case, we update current’s stock field.

If all of these cases fail, then a fourth case presents itself, returning an unsuccessful “0” up to main, indicating that the targeted record does not exist in the linked list.

However, when the code (which I have supplied you with below) is compiled and executed, the following is produced for the program’s output:

The original list in ascending order: 
--------------------------------------------------
                Make                Year     Stock
--------------------------------------------------
           Chevrolet                2012        25
           Chevrolet                2013        10
                Ford                2010         5
                Ford                2011         3
                Ford                2012        15
               Honda                2011         9
               Honda                2012         3
               Honda                2013        12
              Toyota                2009         2
              Toyota                2011         7
              Toyota                2012        20


====Update Stock of Car====
Failed to update stock. Please make sure the car that has the make/year exists in the  list.

====Removing a Record====
I'm trying to remove the record of Ford 2012...
Removed the record successfully. Printing out the new list...
--------------------------------------------------
                Make                Year     Stock
--------------------------------------------------
           Chevrolet                2012        25
           Chevrolet                2013        10
                Ford                2010         5
                Ford                2011         3
               Honda                2011         9
               Honda                2012         3
               Honda                2013        12
              Toyota                2009         2
              Toyota                2011         7
              Toyota                2012        20


====Insert Record in Ascending Order====
I'm trying to insert a record of make Jeep year 2011 stock 5...
Inserted the record successfully. Printing out the new list...
--------------------------------------------------
                Make                Year     Stock
--------------------------------------------------
           Chevrolet                2012        25
           Chevrolet                2013        10
                Ford                2010         5
                Ford                2011         3
               Honda                2011         9
               Honda                2012         3
               Honda                2013        12
                Jeep                2011         5
              Toyota                2009         2
              Toyota                2011         7
              Toyota                2012        20


I'm trying to insert a record of make Honda, year 2012, and stock 10...
Inserted the record successfully. Printing out the new list...
--------------------------------------------------
                Make                Year     Stock
--------------------------------------------------
           Chevrolet                2012        25
           Chevrolet                2013        10
                Ford                2010         5
                Ford                2011         3
               Honda                2011         9
               Honda                2012        10
               Honda                2012         3
               Honda                2013        12
                Jeep                2011         5
              Toyota                2009         2
              Toyota                2011         7
              Toyota                2012        20

For some reason, the “Main” function is being told the node isn’t being found, even though it exists in the list.

Here’s the whole program, and though there are more functions with problems, I’ve excluded most of them:

/*homework2stackoverflow2.c*/

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

#define MAX_LINE 50
#define MAX_MAKE 20

typedef struct record
{
    char *make;
    int year;
    int stock;
    struct record *next;
} Record;

int compareCars(Record *car1, Record *car2);
void printList(Record *head);
Record* createRecord(char *make, int year, int stock);
int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock);
int removeRecord(Record **head, char *make, int year);
int updateStockOfCar(Record *head, char *make, int year, int newStock);

int main(int argc, char **argv)
/*START MAIN*/
{
    FILE *inFile = NULL;
    char line[MAX_LINE + 1];
    char *make, *yearStr, *stockStr;
    int year, stock, len;
    Record* headRecord = NULL;

    /*Input and file diagnostics*/
    if (argc!=2)
    {
        printf ("Filename not provided.\n");
        return 1;
    }

    if((inFile=fopen(argv[1], "r"))==NULL)
    {
        printf("Can't open the file\n");
        return 2;
    }

    /*obtain values for linked list*/
    while (fgets(line, MAX_LINE, inFile))
    {
        make = strtok(line, " ");
        yearStr = strtok(NULL, " ");
        stockStr = strtok(NULL, " ");
        year = atoi(yearStr);
        stock = atoi(stockStr);
        insertRecordInAscendingOrder(&headRecord,make, year, stock);

    }

/*START PRINT ORIGINAL LIST*/
    printf("The original list in ascending order: \n");
    printList(headRecord);
/*END PRINT ORIGINAL LIST*/

/*START UPDATE STOCK TEST*/
    printf("\n====Update Stock of Car====\n");
    int newStock = 12;
    char* newMake = "Ford";
    int updateStatus = updateStockOfCar(headRecord, newMake, year, newStock);
    if(updateStatus == 0)
    {
        printf("Failed to update stock. Please make sure the car that has the make/year exists in the list.\n");

    }
    if(updateStatus == 1)
    {
        printf("Updated stock successfully. Printing out the new list...\n");
        printList(headRecord);
    }
/*END UPDATE STOCK TEST*/

/*START REMOVE RECORD TEST*/
    printf("\n====Removing a Record====\n");
    char *deleteMake = "Ford";
    int deleteYear = 2012;
    int removeStatus = removeRecord(&headRecord, deleteMake, deleteYear);

    if(removeStatus == 0)
    {
        printf("Failed to remove the record. Please make sure the care that has the make/year exists in the list.\n");
    }
    if(removeStatus == 1)
    {
        printf("Removed the record successfully. Printing out the new list...\n");
        printList(headRecord);
    }
/*END REMOVE RECORD TEST*/

/*START INSERT IN ASCENDING ORDER TEST*/
    printf("\n====Insert Record in Ascending Order====\n");
    char* insertMake = "Jeep";
    int insertYear = 2011;
    int insertStock = 5;
    int insertStatus = insertRecordInAscendingOrder(&headRecord, insertMake, insertYear, insertStock);
    printf("I'm trying to insert a record of make %s year %d stock %d...\n", insertMake, insertYear, insertStock);
    if(insertStatus == 0)
    {
        printf("Failed to insert the new record. Please make sure the car that has the make/year doesn't exist in the list.\n");        
    }
    if(insertStatus == 1)
    {
        printf("Inserted the record successfully. Printing out the new list...\n");
        printList(headRecord);
    }
    insertMake = "Honda";
    insertYear = 2012;
    insertStock = 10;
    insertStatus = insertRecordInAscendingOrder(&headRecord, insertMake, insertYear, insertStock);
    printf("\nI'm trying to insert a record of make %s, year %d, and stock %d...\n", insertMake, insertYear, insertStock);
    if(insertStatus == 0)
    {
        printf("Failed to insert the new record. Please make sure the car that has the make/year doesn't exist in the list.\n");        
    }
    if(insertStatus == 1)
    {
        printf("Inserted the record successfully. Printing out the new list...\n");
        printList(headRecord);
    }

/*END INSERT IN ASCENDING ORDER TEST*/

    fclose(inFile);
    return 0;
}
/*END MAIN*/

int compareCars(Record *car1, Record *car2)
{
    int makecmp = strcmp(car1->make, car2->make);

    if( makecmp > 0 )
      return 1;

    if( makecmp == 0 )
    {
        if (car1->year > car2->year)
        {
            return 1;
        }
    if( car1->year == car2->year )
        {
            return 0;
        }

    return -1;
}

/*prints list. lists print.*/
void printList(Record *head)
{
    int i;
    int j = 50;
    Record *aRecord;
    aRecord = head;
    for(i = 0; i < j; i++)
    {
        printf("-");
    }
    printf("\n");
    printf("%20s%20s%10s\n", "Make", "Year", "Stock");
    for(i = 0; i < j; i++)
    {
        printf("-");
    }
    printf("\n");
    while(aRecord != NULL)
    {       
        printf("%20s%20d%10d\n", aRecord->make, aRecord->year,
        aRecord->stock);        
        aRecord = aRecord->next;
    }
    printf("\n");
}

Record* createRecord(char *make, int year, int stock)
{
    Record *theRecord;
    int len;

    if(!make)
    {
        return NULL;
    }

    theRecord = malloc(sizeof(Record));

    if(!theRecord)
    {
        printf("Unable to allocate memory for the structure.\n");
        return NULL;
    }

    theRecord->year = year;
    theRecord->stock = stock;

    len = strlen(make);
    theRecord->make = malloc(len + 1);
    strncpy(theRecord->make, make, len);
    theRecord->make[len] = '\0';
    theRecord->next=NULL;

    return theRecord;
}

int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock)
{
    Record *previous = *head;
    Record *newRecord = createRecord(make, year, stock);
    int compResult;
    Record *current = *head;

    if(*head == NULL)
    {
        *head = newRecord;
        //printf("Head is null, list was empty\n");
        return 1;
    }

    else if(compareCars(newRecord, *head) == 0)
    {   

        return 0;
    }
    else if ( compareCars(newRecord, *head)==-1) 
    {
        *head = newRecord;
        (*head)->next = current;
        //printf("New record was less than the head, replacing\n");
        return 1;
    }
    else 
    {
        //printf("standard case, searching and inserting\n");
        previous = *head;

        while ( current != NULL &&(compareCars(newRecord, current)==1)) 
        {
            previous = current; 
            current = current->next; 
        }

        previous->next = newRecord;
        previous->next->next = current;
    }
    return 1;
}

int removeRecord(Record **head, char *make, int year)
{
    printf("I'm trying to remove the record of %s %d...\n", make, year);

    if(*head == NULL)
    {
        printf("The list is empty\n");
        return 0;
    }   

    Record * delete = malloc(sizeof(Record));
    delete->make = make;
    delete->year = year;

    //If the first record is the one we want to delete, then we need to update the head of the list and free it.
    if((strcmp((*head)->make, delete->make) == 0))
    {           
        if((*head)->year == delete->year)
        {               
            delete = *head;
            *head = (*head)->next;
            free(delete);
            return 1;
        }
    }

    Record * previous = *head;
    Record * current = (*head)->next;

    //Loop through the list, looking for the record to remove.
    while(current != NULL)
    {
        if(strcmp(current->make, delete->make) == 0 && current->year == delete->year)
        {       
            previous->next = current->next;
            free(current);
            return 1;
        }
        else
        {       
            previous = current;
            current = current->next;
        }
    }
    return 0;   
}

int updateStockOfCar(Record *head, char *make, int year, int newStock)
{
    if(head == NULL)
    {
        printf("The list is empty\n");
        return 0;
    }

    Record * matching = malloc(sizeof(Record));
    matching->make = make;
    matching->year = year;

    //If the first record is the one we want to modify, then do so
    if(strcmp(head->make, matching->make) == 0)
    {
        if(head->year == matching->year)
        {
            head->stock = newStock;
            return 1;
        }

        Record* previous = head;
        Record* current = head->next;

    //Loop through the list, looking for the record to update
        while(current != NULL)
        {
            if(strcmp(current->make, matching->make) == 0 && current->year == matching->year)
            {
                current->stock = newStock;
                return 1;
            }
            else
            {
                previous = current;
                current = current -> next;
            }
        }
    }
    return 0;
}

What’s causing this thing to indicate that it’s not present in the list?

And yes… this is a homework assignment 😛

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:26:32+00:00Added an answer on June 13, 2026 at 1:26 am

    I’m not entirely clear why you’re allocating a new record (which is being leaked, btw) just to walk your linked list and update a node once it has been found. Is there something wrong with:

    int updateStockOfCar(Record *head, const char *make, int year, int newStock)
    {
        Record* p = head;
        while (p)
        {
           if ((p->year == year) && !strcmp(p->make, make))
           {
              p->stock = newStock;
              break;
           }
           p = p->next;
        }
        return (p ? 1 : 0);
    }
    

    Retrieving the current stock of a car given a make/year:

    int getStockOfCar(Record *head, const char *make, int year)
    {
        Record* p = head;
        while (p)
        {
           if ((p->year == year) && !strcmp(p->make, make))
              return p->stock;
           p = p->next;
        }
        return 0;
    }
    

    Or did I miss a requirement in the question (wouldn’t be the first time).?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.