Home  >  Article  >  Backend Development  >  C program to store car information using dynamic linked list

C program to store car information using dynamic linked list

WBOY
WBOYforward
2023-09-17 18:33:051202browse

C program to store car information using dynamic linked list

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. It is a collection of nodes.

The node has two parts, as shown below-

  • Data
  • Link

Type of linked list

The types of linked lists in C language are as follows -

  • Singly linked list/singly linked list list
  • Double linked list
  • Cyclic singly linked list
  • Cyclic double linked list

Algorithm

Refer to the algorithm given below and use a dynamic linked list to store car information.

Step 1 - Declare structure variables.

Step 2 - Declare the function definition to be displayed.

Step 3 - Allocate dynamic memory for the variable.

Step 4 - Use a do while loop to enter car information.

Step 5 - Call the display function and go to step 2.

Example

The following is a C program that uses a dynamic linked list to store car information-

Live Demo

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
   char model[10],color[10];
   int year;
   struct node *next;
};
struct node *temp,*head;
void display(struct node *head){
   temp=head;
   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;
      printf("</p><p>");
   }
}
int main(){
   int n;
   char option,enter;
   head=(struct node *)malloc(sizeof(struct node));
   temp=head;
   do{
      printf("</p><p>enter car model: ");
      scanf("%s",temp->model);
      printf("enter car color: ");
      scanf("%s",temp->color);
      printf("enter car year: ");
      scanf("%d",&temp->year);
      printf("</p><p>Do you want continue Y(es) | N(o) : ");
      scanf("%c",&enter);
      scanf("%c",&option);
      if (option!=&#39;N&#39;){
         temp->next=(struct node *)malloc(sizeof(struct node));
         temp=temp->next;
      } else {
         temp->next=NULL;
      }
   }while(option!=&#39;N&#39;);
   display(head);
   return 0;
}

Output

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

enter car model: I20
enter car color: white
enter car year: 2016
Do you want continue Y(es) | N(o) : Y
enter car model: verna
enter car color: red
enter car year: 2018
Do you want continue Y(es) | N(o) : Y
enter car model: creta
enter car color: Maroon
enter car year: 2010
Do you want continue Y(es) | N(o) : N

The above is the detailed content of C program to store car information using dynamic linked list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete