节点有两部分,如下所示 -
数据链接 链表的类型c 语言中链表的类型如下 -
单链表/单链表列表双链表循环单链表循环双链表算法参考下面给出的算法,使用动态链表存储汽车信息。
步骤 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("
"); }}int main(){ int n; char option,enter; head=(struct node *)malloc(sizeof(struct node)); temp=head; do{ printf("
enter car model: "); scanf("%s",temp->model); printf("enter car color: "); scanf("%s",temp->color); printf("enter car year: "); scanf("%d",&temp->year); printf("
do 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: i20enter car color: whiteenter car year: 2016do you want continue y(es) | n(o) : yenter car model: vernaenter car color: redenter car year: 2018do you want continue y(es) | n(o) : yenter car model: cretaenter car color: maroonenter car year: 2010do you want continue y(es) | n(o) : n
以上就是用动态链表存储汽车信息的c程序的详细内容。
