1

我正在使用循环队列制作简单的患者管理程序,但q.rear在执行时始终具有“0”值exit_hos()

我认为这addq()会使变量“后方”不同,但它不起作用。

is_empty()总是返回前后是一样的。

我想我误解了一些代码和内存概念。

如何修复这些功能?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 50
#define MAX_QUEUE_SIZE 6


typedef struct {
    char** value;
    int front;
    int rear;
} Queue;

void init_queue(Queue* q) {
    q->value = (char**)malloc(sizeof(char*) * MAX_QUEUE_SIZE);
    q->front = 0;
    q->rear = 0;
}

int is_full(Queue* q) {
    if (((q->rear +1) % MAX_QUEUE_SIZE) == q->front)
        return 1;

    else
        return 0;
}

int is_empty(Queue* q) {
    if (q->front == q->rear)
        return 1;

    else
        return 0;
}
void addq(Queue* q, char* value) {
    q->rear = (q->rear+1) % MAX_QUEUE_SIZE;
    q->value[q->rear] = value;
    printf("addq: %s", value);
    return;
}

char* deleteq(Queue* q) {
    q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    return q->value[q->front];
}


void arrive(Queue q) {
    int input;
    char name[MAX_SIZE];

    printf("\n");
    printf("1. submit\n");
    printf("2. cancel\n");
    scanf("%d", &input);

    if (input == 1) {
        if (is_full(&q) == 1) {
            printf("Service is not available\n");
        }

        else {
            printf("name: ");
            scanf("%s", name);
            addq(&q, name);
        }
    }

    else if (input == 2) {
        return;
    }

    else {
        printf("input error\n");
        return;
    }
    return;
}

void exit_hos(Queue q) {

    char patient[MAX_SIZE];

    if (is_empty(&q) == 1)
    {
        printf("There is no patient waiting\n");
    }

    else {
        strcpy(patient, deleteq(&q));
        printf("patient: %s", patient);
    }
    return;
}

int main() {

    int input;
    Queue q;
    init_queue(&q);

    while (1)
    {
        printf("\nINPUT\n");
        printf("1. Arrive hostpital\n");
        printf("2. Exit hospital\n");
        printf("3. service exit\n");
        scanf("%d", &input);
        
        if (input == 1)
            arrive(q);

        else if (input == 2) {
            exit_hos(q);
        }
            

        else if (input == 3) {
            printf("exit\n");
            return 0;
        }

        else {
            printf("input error\n");
        }
    }
    free(q.value);

    return 0;
}
4

2 回答 2

0

如果您已经有一个最大队列大小和一个最大大小,那么最好将整个事物预先分配为一个数组,从而减少内存问题。作为一般规则,除非它们提供了您想要的功能,否则请避免头痛。

注意:这种跟踪和重用内存的方法称为循环缓冲区(不要与更常称为队列的链表类型混淆)。


#define MAX_SIZE 50
#define MAX_QUEUE_SIZE 6

typedef struct {
    char value [MAX_QUEUE_SIZE][MAX_SIZE + 1]; //+1 to hold extra null termination
    unsigned int front;
    unsigned int size; //size is a clearer than rear, which could have meant end item or end+1 and needed special empty queue handling
} Queue;

void init_queue(Queue* q) {
    memset(q,0,sizeof(Queue)); //just zero it all
    //more info on this and some situation-dependent alternatives https://stackoverflow.com/questions/11152160/initializing-a-struct-to-0
}

int is_full(const Queue* q) {
    return q->size >= MAX_QUEUE_SIZE;
}

int is_empty(const Queue* q) {
    return q->size == 0;
}

//sometimes called a push operation
//return 0 if failed
int addq(Queue* q, const char* value) {
    //error check, abort, error handling section:
    //full queue -> abort
    if(is_full(q)) return 0;
    //long value -> truncate handled via strncpy
    
    //actual operation
    const unsigned int destination = (q->front + q->size) % MAX_QUEUE_SIZE;
    strncpy(q->value[destination],value,MAX_SIZE);
    q->size = q->size + 1;

    printf("addq: %s", q->value[destination]);
    return q->size;
}

//sometimes called a pop operation
//return value may not persist if addq is called, but fine for your use of copying on call
const char* deleteq(Queue* q) {
    if(is_empty(q)) return 0;

    const char * retval = q->value[q->front];
    q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    q->size = q->size - 1;
    return retval;
}

还记得使用 MAX_SIZE + 1 或 strncpy 和 MAX_SIZE - 1,因为“如果源长于 num,则不会在目标末尾隐式附加空字符。” (并且将 strcpy 和 scanf 吊在数组上时是不安全的)

于 2020-09-21T14:11:24.100 回答
0

我认为这条线是错误的:

q->value = (char**)malloc(sizeof(char*) * MAX_QUEUE_SIZE);

我认为应该是:

char * _value = (char*)malloc(sizeof(char*) * MAX_QUEUE_SIZE);
q->value = &_value;

malloc 将返回一个指向 char 数组的指针。 q->value是指向 char 数组的指针的指针。因此,您希望将其设置为 malloc 为您创建的 char 数组的地址。

将您init_queue的代码更改为此,它将起作用:

void init_queue(Queue* q) {
    char * _value = (char*)malloc(sizeof(char*) * MAX_QUEUE_SIZE);

    q->value = &_value;
    q->front = 0;
    q->rear = 0;
}

输出:

Chris@DESKTOP-BCMC1RF ~
$ ./main.exe

INPUT
1. Arrive hostpital
2. Exit hospital
3. service exit
1

1. submit
2. cancel
1
name: fred
addq: fred
INPUT
1. Arrive hostpital
2. Exit hospital
3. service exit
2
于 2020-09-20T16:01:15.920 回答