鱼C论坛

 找回密码
 立即注册
查看: 2116|回复: 7

[已解决]单链表输入航班信息的问题

[复制链接]
发表于 2019-8-26 22:31:58 | 显示全部楼层 |阅读模式
52鱼币
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<windows.h>
  5. typedef struct airline
  6. {
  7.     char line_num[10];//航班号
  8.     char start_place[20];//起飞地
  9.     char end_place[20];//目的地
  10.     char start_time[10];//起飞时间
  11.     char end_time[10];//降落时间
  12.     int total;//座位总数
  13.     int left;//剩余座位
  14.     int count;
  15.     struct airline *next;//下一个结点
  16. } airline,*Airline;
  17. //*初始化单链表**//
  18. void InitList_1(Airline *pheadline)
  19. {
  20.         *pheadline=(Airline)malloc(sizeof(airline));
  21.         (*pheadline)->next=NULL;
  22. }
  23. airline* Put_Flight_Information(int n, Airline pheadline)//录入航班函数: n为所要录入航班的数量:
  24. {
  25.         color(4);
  26.         int i;
  27.         int b;
  28.         airline *temp,*p,*Investigation;
  29.         Investigation=pheadline->next;
  30.         p=pheadline;
  31.             pheadline->count = n;
  32.             for(i=0; i<n; i++)
  33.             {
  34.                     temp=(airline *)malloc(sizeof(airline));
  35.                     printf("\t\t请输入第%d个航班的航班号:\n\t\t",i+1);
  36.                 scanf("%s",temp->line_num);
  37.                 while(Investigation!=NULL)
  38.                 {
  39.                         if(strcmp(Investigation->next->line_num,temp->line_num)!=0)
  40.                         {
  41.                                 Investigation=Investigation->next;
  42.                         }
  43.                         else
  44.                         {
  45.                                 printf("\t\t您输入的航班号重复,请重新输入:\n\t\t");
  46.                                 scanf("%s",temp->line_num);
  47.                                 Investigation=pheadline->next;                                            
  48.                         }
  49.                 }
  50.                 printf("\t\t请输入第%d个航班起飞点\n\t\t",i+1);
  51.                 scanf("%s",temp->start_place);
  52.                 printf("\t\t请输入第%d个航班目的地\n\t\t",i+1);
  53.                 scanf("%s",temp->end_place);
  54.                 printf("\t\t请输入第%d个航班起飞的时间\n\t\t",i+1);
  55.                 scanf("%s",temp->start_time);
  56.                 printf("\t\t请输入第%d个航班降落的时间\n\t\t",i+1);
  57.                 scanf("%s",temp->end_time);
  58.                 getchar();
  59.                 printf("\t\t请输入第%d个航班的座位\n\t\t",i+1);
  60.                 scanf("%d",temp->total);
  61.                 getchar();
  62.                 temp->left=temp->total;
  63.                 //printf("\t\t你好\n");
  64.                 p->next=temp;
  65.                 p=temp;
  66.                 //printf("\t\t你好\n");
  67.                 if(i==n-1)
  68.                 {
  69.                         p->next=NULL;
  70.                 }
  71.         }
  72.         return pheadline;

  73. }
  74. int main()
  75. {
  76.         Airline bb;
  77.         InitList_1(&bb);
  78.         Put_Flight_Information(3,bb);
  79.         return 0;
  80. }
复制代码

大佬们,我想问一下就是那个Put_Flight_Information函数里面,输入到最后一个(请输入第%d个航班的座位)那里,程序就会错误,不能完整输入,我已经找了好久了,找不到问题所在,望大佬们看看
最佳答案
2019-8-26 22:31:59
其中还有一两点小细节帮你改了,你自己对照着代码看 ̄  ̄)σ

最佳答案

查看完整内容

其中还有一两点小细节帮你改了,你自己对照着代码看 ̄  ̄)σ
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 22:31:59 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
其中还有一两点小细节帮你改了,你自己对照着代码看 ̄  ̄)σ
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-26 22:35:00 | 显示全部楼层
我用的是code::Blocks写的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-27 00:46:00 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<windows.h>
  5. typedef struct airline
  6. {
  7.     char line_num[10];//航班号
  8.     char start_place[20];//起飞地
  9.     char end_place[20];//目的地
  10.     char start_time[10];//起飞时间
  11.     char end_time[10];//降落时间
  12.     int total;//座位总数
  13.     int left;//剩余座位
  14.     int count;
  15.     struct airline *next;//下一个结点
  16. } airline,*Airline;
  17. //*初始化单链表**//
  18. void InitList_1(Airline*pheadline)
  19. {
  20.         *pheadline=(Airline)malloc(sizeof(airline));
  21.         (*pheadline)->next=NULL;
  22. }
  23. airline* Put_Flight_Information(int n, Airline pheadline)//录入航班函数: n为所要录入航班的数量:
  24. {
  25.         //color(4);
  26.         int i;
  27.         int b;
  28.         airline *temp,*p,*Investigation;
  29.         p=pheadline;
  30.             pheadline->count = n;
  31.             for(i=0; i<n; i++)
  32.             {
  33.                     temp=(airline*)malloc(sizeof(airline));
  34.                     printf("\t\t请输入第%d个航班的航班号:\n\t\t",i+1);
  35.                 scanf("%s",temp->line_num);
  36.                 Investigation=pheadline->next;
  37.                 while(Investigation!=NULL)
  38.                 {
  39.                         if(strcmp(Investigation->line_num,temp->line_num)!=0)
  40.                         {
  41.                                 Investigation=Investigation->next;
  42.                         }
  43.                         else
  44.                         {
  45.                                 printf("\t\t您输入的航班号重复,请重新输入:\n\t\t");
  46.                                 scanf("%s",temp->line_num);
  47.                                 Investigation=pheadline->next;                                            
  48.                         }
  49.                 }
  50.                 printf("\t\t请输入第%d个航班起飞点\n\t\t",i+1);
  51.                 scanf("%s",temp->start_place);
  52.                 printf("\t\t请输入第%d个航班目的地\n\t\t",i+1);
  53.                 scanf("%s",temp->end_place);
  54.                 printf("\t\t请输入第%d个航班起飞的时间\n\t\t",i+1);
  55.                 scanf("%s",temp->start_time);
  56.                 printf("\t\t请输入第%d个航班降落的时间\n\t\t",i+1);
  57.                 scanf("%s",temp->end_time);
  58.                 //getchar();
  59.                 printf("\t\t请输入第%d个航班的座位\n\t\t",i+1);
  60.                 scanf("%d",&(temp->total));
  61.                 //getchar();
  62.                 temp->left=temp->total;
  63.                 //printf("\t\t你好\n");
  64.                 p->next=temp;
  65.                 p=temp;
  66.                 //printf("\t\t你好\n");
  67.                      p->next = NULL;
  68.         }
  69.         return pheadline;
  70. }
  71. int main()
  72. {
  73.         Airline bb;
  74.         InitList_1(&bb);
  75.         Put_Flight_Information(3,bb);
  76.         return 0;
  77. }
复制代码

小修改了下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-27 06:45:18 From FishC Mobile | 显示全部楼层
灰色的天空 发表于 2019-8-27 00:46
小修改了下

好像temp->total那里没有加取址符   哇  这真的是个低级错误啊  谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-27 06:53:11 From FishC Mobile | 显示全部楼层
灰色的天空 发表于 2019-8-27 00:46
小修改了下

还有一个问题  就是第二次输入相同的航班号的时候  没有提示您输入的航班号重复,请重新输入。是不是while循环那里有问题啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-27 15:34:30 From FishC Mobile | 显示全部楼层
你拷贝我给你改的。你第二次不行是因为你在添加的节点next未知,但是每次比较时又要比较到null.所以不行。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-27 16:43:58 | 显示全部楼层
灰色的天空 发表于 2019-8-27 15:35
其中还有一两点小细节帮你改了,你自己对照着代码看 ̄  ̄)σ

果然可以了 蟹蟹丫
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 23:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表