鱼C论坛

 找回密码
 立即注册
查看: 645|回复: 1

为什么这个排序永远数不出来呢?

[复制链接]
发表于 2023-2-19 15:45:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪头少年.zm 于 2023-2-20 07:43 编辑

这个就是一个非常简单的排队代码
先按照病情的严重进行排队,然后才按照先来后到的顺序进行排队。
如题所示,想问一下大佬们,为什么这个rank123函数跑不起来
  1. #define MaxSize 100
  2. #include <stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<iostream>
  6. using namespace std;
  7. typedef struct LNode
  8. {
  9.         char condition;
  10.         int line;
  11.         string  name;
  12.         struct LNode* next;
  13. }LNode,*LinkList;
  14. typedef struct
  15. {
  16.         LNode* rear, * front;
  17.         LNode* find;
  18. }LinkQueue;
  19. void InitQueue(LinkQueue& L)
  20. {
  21.         L.find = L.rear = L.front = (LNode*)malloc(sizeof(LNode));
  22.         L.front->next = NULL;
  23. }
  24. void EnQueue(LinkQueue& L, char x,int l)  //x表示病情的轻重缓急 i表示第几个来的
  25. {                                                                              //x有轻微(slight用s表示),中等(medium用m表示),严重(heavy用h表示)
  26.         LNode* q = (LNode*)malloc(sizeof(LNode));
  27.         q->condition = x;
  28.         q->line = l;
  29.         q->next = NULL;
  30.         L.rear->next = q;
  31.         L.rear = q;
  32. }
  33. bool DeQueue(LinkQueue& L, char x, int l)
  34. {
  35.         LNode* p = L.front->next;
  36.         if (L.rear == L.front)
  37.         {
  38.                 return false;
  39.         }
  40.         x = p->condition;
  41.         l = p->line;
  42.         printf("%c  ", x);
  43.         printf("%d  ", l);
  44.         L.rear->next = p->next;
  45.         if (L.rear == p)
  46.         {
  47.                 L.rear = L.front;
  48.         }
  49.         free(p);
  50.         return true;
  51. }
  52. void rank123(LinkQueue& L)
  53. {
  54.         if (L.front == L.rear)
  55.         {
  56.                 cout << "当前无人排队" << endl;
  57.                 return;
  58.         }
  59.         LNode *Q =(LNode*)malloc(sizeof(LNode));
  60.         LinkQueue P = L;
  61.         while (L.find != NULL)
  62.         {
  63.                 L.find = L.find->next;
  64.                 if (L.find->condition == 'h')
  65.                 {
  66.                         if (L.find = L.front->next)
  67.                         {

  68.                         }
  69.                         else
  70.                         {
  71.                                 Q->condition = L.find->condition;
  72.                                 Q->line = L.find->line;
  73.                                 L.find->condition = P.front->next->condition;
  74.                                 L.find->line = P.front->next->line;
  75.                                 P.front->next->condition = Q->condition;
  76.                                 P.front->next->line = Q->line;
  77.                                 L.find = L.find->next;
  78.                                 P.find = P.find->next;
  79.                         }
  80.                 }
  81.         }
  82.         L.find = L.front;
  83.         while (L.find != NULL)
  84.         {
  85.                 L.find = L.find->next;
  86.                 if (L.find->condition == 'l')
  87.                 {
  88.                         Q->condition = L.find->condition;
  89.                         Q->line = L.find->line;
  90.                         L.find->condition = P.front->next->condition;
  91.                         L.find->line = P.front->next->line;
  92.                         P.front->next->condition = Q->condition;
  93.                         P.front->next->line = Q->line;
  94.                         L.find = L.find->next;
  95.                         P.find = P.find->next;
  96.                 }
  97.         }
  98.         L.find = L.front;
  99.         while (L.find != NULL)
  100.         {
  101.                 L.find = L.find->next;
  102.                 if (L.find->condition == 's')
  103.                 {
  104.                         Q->condition = L.find->condition;
  105.                         Q->line = L.find->line;
  106.                         L.find->condition = P.front->next->condition;
  107.                         L.find->line = P.front->next->line;
  108.                         P.front->next->condition = Q->condition;
  109.                         P.front->next->line = Q->line;
  110.                         L.find = L.find->next;
  111.                         P.find = P.find->next;
  112.                 }
  113.         }
  114. }
  115. void print(LinkQueue& L)
  116. {
  117.         while(L.front != L.rear)
  118.         {
  119.                 printf("%d  ", L.front->line);
  120.         }
  121. }                                
  122. char sickness(char illness)
  123. {
  124.         cout << "请输入您的病情(请用h,s,l表示)" << endl;
  125.         cin >> illness;
  126.         if (illness != 'h' && illness != 's' && illness != 'l')
  127.         {
  128.                 cout << "请按照要求进行输入" << endl;
  129.                 sickness(illness);
  130.         }
  131.         return illness;
  132. }
  133. int menu(int &j)
  134. {
  135.         cout << "1.排队取号" << endl;
  136.         cout << "2.查询当前队伍状况" << endl;
  137.         cout << "请输入您要办理的业务" << endl;
  138.         cin >> j;
  139.         if (j != 1 && j != 2)
  140.         {
  141.                 cout << "请按要求进行输入" << endl;
  142.                 menu(j);
  143.         }
  144.         return j;
  145.        
  146. }
  147. int main()
  148. {
  149.         LinkQueue Q;
  150.         InitQueue(Q);
  151.         int i = 0;                      //所有的值都要进行初始化,以便后面进行调用
  152.         int        j = 0;                     //j是主菜单的返回值
  153.         char situation ='a';
  154.         char illness ='a';
  155.         while (1)
  156.         {
  157.                 menu(j);
  158.                 if (j == 1)
  159.                 {
  160.                         sickness(illness);     //sickness函数是对illness进行赋值并判断是否合法的一个函数
  161.                         EnQueue(Q, illness, i+1);
  162.                         i++;
  163.                 }
  164.                 else
  165.                 {
  166.                         rank123(Q);
  167.                         print(Q);
  168.                 }
  169.         }
  170. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-20 09:35:05 | 显示全部楼层
感觉问题不少,没有仔细看
先看第61到64行,L.find 不是 NULL,可以取 L.find->next,但是下一步立刻取 L.find->condition 相当于是 L.find->next->condition,能保证 L.find->next 的值不是 NULL 吗?
既然写 C++,如果有可能的话就用点 C++ 吧,只用引用和 std::cin, std::cout, std::endl 太浪费了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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