简体   繁体   English

使用队列的简单患者管理程序

[英]simple patient managing program using queue

I'm making simple patient managing program using circular queue but q.rear always have "0" value while executing exit_hos()我正在使用循环队列制作简单的患者管理程序,但在执行exit_hos()q.rear始终具有“0”值

I thought that addq() makes variable "rear" different, but It doesn't work.我认为addq()使变量“后部”不同,但它不起作用。

is_empty() always return front and rear is same. is_empty()总是返回前后相同。

I think I'm misunderstanding some codes and memory concepts.我想我误解了一些代码和内存概念。

how can I fix these functions?我该如何修复这些功能?

#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;
}

I think that this line is wrong:我认为这一行是错误的:

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

I think that it should be:我认为应该是:

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

malloc is going to return a pointer to a char array. malloc 将返回一个指向 char 数组的指针。 q->value is a pointer to a pointer to a char array. q->value是指向 char 数组指针的指针。 So you want to set it to the address of the char array that malloc is created for you.所以你想把它设置为 malloc 为你创建的 char 数组的地址。

Change you init_queue code to this and it will work:将您的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;
}

Output:输出:

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

If you already have a max queue size and a max size, you are better off pre-allocating the whole thing as an array, reducing memory headaches.如果您已经有了最大队列大小和最大大小,最好将整个事物预先分配为一个数组,从而减少内存问题。 As a general rule, avoid headaches unless they provide a feature you want.作为一般规则,除非它们提供您想要的功能,否则避免头痛。

Note: This method of keeping track of and re-using memory is called a circular buffer (not to be confused with the linked list types that are more commonly called queues).注意:这种跟踪和重用内存的方法称为循环缓冲区(不要与通常称为队列的链表类型混淆)。


#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;
}

also remember to use either MAX_SIZE + 1 or strncpy with MAX_SIZE - 1 since "No null-character is implicitly appended at the end of destination if source is longer than num."还记得将 MAX_SIZE + 1 或 strncpy 与 MAX_SIZE - 1 一起使用,因为“如果源长于 num,则不会在目标末尾隐式附加空字符。” (and strcpy and scanf as you sling them onto arrays is unsafe) (并且 strcpy 和 scanf 将它们吊在阵列上是不安全的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM