C program to store the car information using dynamic linked list.

A linked list is a dynamic data structure that grows and shrinks during runtime using dynamic memory allocation. It consists of nodes, where each node contains data and a pointer to the next node. This makes it ideal for storing variable amounts of data like car information.

Node Structure

Each node in a linked list has two main components −

  • Data − Stores the actual information (car model, color, year)
  • Link − Pointer to the next node in the list

Types of Linked Lists

The types of linked lists in C programming are −

  • Single / Singly linked lists
  • Double / Doubly linked lists
  • Circular single linked list
  • Circular double linked list

Syntax

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

Algorithm

The algorithm for storing car information using dynamic linked list −

  1. Declare structure variables for car information
  2. Define function to display filtered car data
  3. Allocate dynamic memory for nodes using malloc()
  4. Use loop to input car information continuously
  5. Link nodes together and call display function

Example

Following is the C program for storing car information using dynamic linked list −

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

struct node {
    char model[20], color[15];
    int year;
    struct node *next;
};

struct node *temp, *head;

void display(struct node *head) {
    temp = head;
    printf("\nCars manufactured after 2010 with yellow color:
"); printf("Model\t\tColor\t\tYear
"); printf("----------------------------------------
"); while (temp != NULL) { if (temp->year > 2010 && strcmp("yellow", temp->color) == 0) { printf("%s\t\t%s\t\t%d
", temp->model, temp->color, temp->year); } temp = temp->next; } } int main() { char option; head = (struct node *)malloc(sizeof(struct node)); temp = head; do { printf("\nEnter car model: "); scanf("%s", temp->model); printf("Enter car color: "); scanf("%s", temp->color); printf("Enter car year: "); scanf("%d", &temp->year); printf("\nDo you want to continue Y(es) | N(o): "); scanf(" %c", &option); if (option != 'N' && option != 'n') { temp->next = (struct node *)malloc(sizeof(struct node)); temp = temp->next; } else { temp->next = NULL; } } while (option != 'N' && option != 'n'); display(head); /* Free allocated memory */ temp = head; while (temp != NULL) { struct node *next = temp->next; free(temp); temp = next; } return 0; }

Output

When the above program is executed, it produces the following output −

Enter car model: BMW
Enter car color: yellow
Enter car year: 2015

Do you want to continue Y(es) | N(o): Y

Enter car model: Audi
Enter car color: black
Enter car year: 2018

Do you want to continue Y(es) | N(o): Y

Enter car model: Honda
Enter car color: yellow
Enter car year: 2012

Do you want to continue Y(es) | N(o): N

Cars manufactured after 2010 with yellow color:
Model		Color		Year
----------------------------------------
BMW		yellow		2015
Honda		yellow		2012

Key Points

  • Dynamic memory allocation using malloc() allows flexible list size
  • Always check for memory allocation success in production code
  • Free allocated memory using free() to prevent memory leaks
  • Use proper input validation for robust programs

Conclusion

Dynamic linked lists provide an efficient way to store variable amounts of data like car information. They offer flexibility in memory usage and easy insertion/deletion operations compared to static arrays.

Updated on: 2026-03-15T14:10:05+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements