Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

동적 연결 목록을 사용하여 자동차 정보를 저장하는 C 프로그램.

<시간/>

연결 목록은 동적 메모리 할당을 사용합니다. 즉, 그에 따라 확장 및 축소됩니다. 노드 모음입니다.

노드에는 다음과 같은 두 부분이 있습니다. -

  • 데이터
  • 링크

연결 목록의 유형

C 프로그래밍 언어의 연결 목록 유형은 다음과 같습니다. -

  • 단일/단일 연결 목록
  • 이중/이중 연결 목록
  • 순환 단일 연결 목록
  • 원형 이중 연결 목록

알고리즘

동적 연결 리스트를 이용하여 자동차 정보를 저장하는 방법은 아래의 알고리즘을 참고하세요.

1단계 - 구조체 변수를 선언합니다.

2단계 - 표시할 함수 정의를 선언합니다.

3단계 - 변수에 동적 메모리 할당을 할당합니다.

4단계 - do while 루프를 사용하여 자동차 정보를 입력합니다.

5단계 - 2단계로 이동하여 표시 기능을 호출합니다.

예시

다음은 동적 연결 리스트를 이용하여 자동차 정보를 저장하는 C 프로그램이다 -

#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("\n");
   }
}
int main(){
   int n;
   char option,enter;
   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 continue Y(es) | N(o) : ");
      scanf("%c",&enter);
      scanf("%c",&option);
      if (option!='N'){
         temp->next=(struct node *)malloc(sizeof(struct node));
         temp=temp->next;
      } else {
         temp->next=NULL;
      }
   }while(option!='N');
   display(head);
   return 0;
}

출력

위의 프로그램이 실행되면 다음과 같은 출력을 생성합니다 -

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