• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

编写一个链表结构关于车的属性,读取任意多辆车的车型、车牌号、外形颜色、价钱输入。之后遍历链表,再将链表数据打印出来。

#include<stdio.h>
#include<stdlib.h>
typedef struct vehicle
{
	int number;
	char color[8];
	char xinghao[80];
	int price;
	struct vehicle *next;
}car;
int n;
car *create()
{
	car *head,*p1,*p2;
	n=0;
	p1=p2=(car*)malloc(sizeof(car));
	printf("请输入车型、车号、颜色、价格\n");
	scanf("%s %d %s %d",p1->xinghao,&p1->number,p1->color,&p1->price);
	while(p1->number!=0)
	{
		n++;
		if(n==1)
			head=p1;
		else p2->next=p1;
		p2=p1;
		p1 = (car*) malloc(sizeof(car));
		scanf("%s %d %s %d",p1->xinghao,&p1->number,p1->color,&p1->price);
	}
	p2->next=NULL;
	return (head);
}
int print(car *head)
{
	struct vehicle *p;
	if(head==NULL)
		return;
	for(p=head;p!=NULL;p=p->next)
		printf("车型为%s 车号为%d 车颜色为%s 车价格为%d\n",p->xinghao,p->number,p->color,p->price);

}
int main()
{
	car *car1;
	car1=create();
	print(car1);
}



输出

 

输入

宝马320Li 3811 红色 20015                     
宝马520Li 4311 蓝色 35833
0 0 0 0

posted on 2022-04-12 20:02  王陈锋  阅读(22)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3