猪头少年.zm 发表于 2023-2-19 15:45:05

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

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

这个就是一个非常简单的排队代码
先按照病情的严重进行排队,然后才按照先来后到的顺序进行排队。
如题所示,想问一下大佬们,为什么这个rank123函数跑不起来
#define MaxSize 100
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct LNode
{
        char condition;
        int line;
        stringname;
        struct LNode* next;
}LNode,*LinkList;
typedef struct
{
        LNode* rear, * front;
        LNode* find;
}LinkQueue;
void InitQueue(LinkQueue& L)
{
        L.find = L.rear = L.front = (LNode*)malloc(sizeof(LNode));
        L.front->next = NULL;
}
void EnQueue(LinkQueue& L, char x,int l)//x表示病情的轻重缓急 i表示第几个来的
{                                                                              //x有轻微(slight用s表示),中等(medium用m表示),严重(heavy用h表示)
        LNode* q = (LNode*)malloc(sizeof(LNode));
        q->condition = x;
        q->line = l;
        q->next = NULL;
        L.rear->next = q;
        L.rear = q;
}
bool DeQueue(LinkQueue& L, char x, int l)
{
        LNode* p = L.front->next;
        if (L.rear == L.front)
        {
                return false;
        }
        x = p->condition;
        l = p->line;
        printf("%c", x);
        printf("%d", l);
        L.rear->next = p->next;
        if (L.rear == p)
        {
                L.rear = L.front;
        }
        free(p);
        return true;
}
void rank123(LinkQueue& L)
{
        if (L.front == L.rear)
        {
                cout << "当前无人排队" << endl;
                return;
        }
        LNode *Q =(LNode*)malloc(sizeof(LNode));
        LinkQueue P = L;
        while (L.find != NULL)
        {
                L.find = L.find->next;
                if (L.find->condition == 'h')
                {
                        if (L.find = L.front->next)
                        {

                        }
                        else
                        {
                                Q->condition = L.find->condition;
                                Q->line = L.find->line;
                                L.find->condition = P.front->next->condition;
                                L.find->line = P.front->next->line;
                                P.front->next->condition = Q->condition;
                                P.front->next->line = Q->line;
                                L.find = L.find->next;
                                P.find = P.find->next;
                        }
                }
        }
        L.find = L.front;
        while (L.find != NULL)
        {
                L.find = L.find->next;
                if (L.find->condition == 'l')
                {
                        Q->condition = L.find->condition;
                        Q->line = L.find->line;
                        L.find->condition = P.front->next->condition;
                        L.find->line = P.front->next->line;
                        P.front->next->condition = Q->condition;
                        P.front->next->line = Q->line;
                        L.find = L.find->next;
                        P.find = P.find->next;
                }
        }
        L.find = L.front;
        while (L.find != NULL)
        {
                L.find = L.find->next;
                if (L.find->condition == 's')
                {
                        Q->condition = L.find->condition;
                        Q->line = L.find->line;
                        L.find->condition = P.front->next->condition;
                        L.find->line = P.front->next->line;
                        P.front->next->condition = Q->condition;
                        P.front->next->line = Q->line;
                        L.find = L.find->next;
                        P.find = P.find->next;
                }
        }
}
void print(LinkQueue& L)
{
        while(L.front != L.rear)
        {
                printf("%d", L.front->line);
        }
}                              
char sickness(char illness)
{
        cout << "请输入您的病情(请用h,s,l表示)" << endl;
        cin >> illness;
        if (illness != 'h' && illness != 's' && illness != 'l')
        {
                cout << "请按照要求进行输入" << endl;
                sickness(illness);
        }
        return illness;
}
int menu(int &j)
{
        cout << "1.排队取号" << endl;
        cout << "2.查询当前队伍状况" << endl;
        cout << "请输入您要办理的业务" << endl;
        cin >> j;
        if (j != 1 && j != 2)
        {
                cout << "请按要求进行输入" << endl;
                menu(j);
        }
        return j;
       
}
int main()
{
        LinkQueue Q;
        InitQueue(Q);
        int i = 0;                      //所有的值都要进行初始化,以便后面进行调用
        int        j = 0;                     //j是主菜单的返回值
        char situation ='a';
        char illness ='a';
        while (1)
        {
                menu(j);
                if (j == 1)
                {
                        sickness(illness);   //sickness函数是对illness进行赋值并判断是否合法的一个函数
                        EnQueue(Q, illness, i+1);
                        i++;
                }
                else
                {
                        rank123(Q);
                        print(Q);
                }
        }
}

dolly_yos2 发表于 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 太浪费了
页: [1]
查看完整版本: 为什么这个排序永远数不出来呢?