I followed the example of the circular cue in the book, and there are errors depending on the size of the array. Help me.

Asked 1 years ago, Updated 1 years ago, 119 views

Hello.

I am currently reading a book called "Easy Data Structure in C Language" by Chun In-guk.

I always think it's better to move the example yourself and carefully examine how it works and implement it myself.

While I met the round cue example and moved it myself and implemented it, There was an error.

If the size of the array designated as macro in the source code below is specified as 3, it will work normally, and if it is specified as an integer of 4 or more, it will run an infinite loop in the output part of the deletion stage;;๐Ÿ˜ฑ

The code is as follows:

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

// ==== Start the circular cue code ====
#define MAX_QUEUE_SIZE 5
typedef int element;
typeef structure { // queue type
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} } QueueType;

// Error function
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

// Circular Queue Initialization Function
void init_queue(QueueType *q) {
    q->front = q->rear = 0;
}

// Blank Status Detection Function
int is_empty(QueueType *q) {
    return (q->front == q->rear);       

}

// Saturation detection function
int is_full(QueueType *q) {
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);    
}

// Circular Queue Output Function
void queue_print(QueueType *q) {
    printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) { 
        int i = q->front;
        do {
            i = (i + 1) % (MAX_QUEUE_SIZE); 
            printf("%d | ", q->data[i]);
            if (i == q->rear)
                break;
        } } while (i != q->front);
    }
    printf("\n");
}

// Insertion function
void enqueue(QueueType *q, element item) {
    if (is_full(q))
        error ("Queue is saturated");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
    q->data[q->rear] = item;
}

// Delete function
element dequeque(QueueType *q) {
    if (is_empty(q))
        error ("Queue is blank");
    q->front = (q->front + 1)&MAX_QUEUE_SIZE;
    return q->data[q->front];
}

// Peak function
element peek(QueueType *q) {
    if (is_empty(q))
        error ("Queue is blank");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE];
}
// ===== Circular Queue Code End =====

int main() {
    QueueType queue;
    int element;

    init_queue(&queue);
    printf("--Add data step--\n");
    while (!is_full(&queue)) {
        printf("Enter an integer");
        scanf_s("%d", &element);
        enqueue(&queue, element);
        queue_print(&queue);
    }

    printf ("Queue is saturated".\n\n");

    printf("--Delete data step--\n");
    while (!is_empty(&queue)) {
        element = dequeque(&queue);
        printf("Ejected Integer: %d\n", element);
        queue_print(&queue);
    }
    printf ("Queue is blank".\n");
    return 0;
}

Honestly, I think it's a source code that's too much to readOnly

Did I misspell it? Review it five times

If the size of the array is small, it is executed again, so it is ridiculous.

I thought the author of the book would have sent it back before publishing it.

That's why I asked this question

Can you tell me why the size of the array goes beyond 4 and it goes around an infinite loop?

Please! I'll be waiting for 100 years! Just give me an answer!

queue c data-structure

2022-09-20 17:02

1 Answers

I think it's a typo.

q->front = (q->front + 1)&MAX_QUEUE_SIZE;

Replace the code above in the delete function with the following.

q->front = (q->front + 1) % MAX_QUEUE_SIZE;


2022-09-20 17:02

If you have any answers or tips


ยฉ 2024 OneMinuteCode. All rights reserved.