簡體   English   中英

使用隊列的簡單患者管理程序

[英]simple patient managing program using queue

我正在使用循環隊列制作簡單的患者管理程序,但在執行exit_hos()q.rear始終具有“0”值

我認為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;
}

我認為這一行是錯誤的:

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

如果您已經有了最大隊列大小和最大大小,最好將整個事物預先分配為一個數組,從而減少內存問題。 作為一般規則,除非它們提供您想要的功能,否則避免頭痛。

注意:這種跟蹤和重用內存的方法稱為循環緩沖區(不要與通常稱為隊列的鏈表類型混淆)。


#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 將它們吊在陣列上是不安全的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM