链接列表使用动态内存分配,即它们相应地增长和收缩。它是节点的集合。
节点有两部分,如下所示 –
数据链接
链表的类型
C 语言中链表的类型如下 –
单链表/单链表列表双链表循环单链表循环双链表
算法
步骤 1 – 声明结构变量。
步骤 2 – 声明要显示的函数定义.
第3步 – 为变量分配动态内存。
第4步 – 使用do while循环输入汽车信息。
第5步 – 调用显示函数转到步骤2。
示例
以下是使用动态链表存储汽车信息的C程序 –
Live Demo
#include#include#includestruct 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 %s %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程序的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2581187.html