>  기사  >  백엔드 개발  >  동적 연결 리스트를 사용하여 자동차 정보를 저장하는 C 프로그램

동적 연결 리스트를 사용하여 자동차 정보를 저장하는 C 프로그램

WBOY
WBOY앞으로
2023-09-17 18:33:051171검색

동적 연결 리스트를 사용하여 자동차 정보를 저장하는 C 프로그램

연결된 목록은 동적 메모리 할당을 사용합니다. 즉, 그에 따라 메모리가 늘어나고 줄어듭니다. 노드의 모음입니다.

Node는 아래와 같이 두 부분으로 구성됩니다. -

  • data
  • link

연결 리스트 종류

C 언어에서 연결 리스트 종류는 다음과 같습니다. -

  • 단일 연결 목록/단일 연결 목록
  • 이중 연결 리스트
  • loop 단일 연결 리스트
  • Cyclic double linked list

Algorithm

아래 알고리즘을 참고하여 동적 연결 리스트를 활용하여 자동차 정보를 저장해 보세요.

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

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

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

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

5단계 - 디스플레이 기능을 호출하고 2단계로 이동합니다.

예제

다음은 동적 연결 리스트를 사용해 자동차 정보를 저장하는 C 프로그램입니다. -

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

위 프로그램을 실행하면 다음과 같은 출력이 나옵니다. −

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

위 내용은 동적 연결 리스트를 사용하여 자동차 정보를 저장하는 C 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제