Skip to content

Commit dd7955f

Browse files
committed
Refactor and document queue data structure
Improved code style and consistency in queue.c and queue.h, added detailed documentation for the queue data structure, and clarified comments. Updated main.c build instructions and fixed minor formatting issues. Added include guard for QUEUE_EMPTY in queue.h.
1 parent 3c3d748 commit dd7955f

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

AbstractDataType/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
* @file main.c
33
* @author Xuhua Huang
44
* @brief To compile and run the demo, execute the following:
5-
* $ cd .\AbstractDataTypes\
5+
* $ cd .\AbstractDataType\
66
* $ mingw32-make all
7+
*
8+
* or alternatively with CMake:
9+
* $ cd .\AbstractDataType\
10+
* $ cmake -S . -B build
11+
* $ cmake --build build --config Debug --verbose
712
*
813
* @version 0.1
914
* @date 2022-11-07
@@ -15,10 +20,11 @@
1520
#include <stdbool.h>
1621
#include <stdio.h>
1722
#include <stdlib.h>
23+
1824
#include "queue.h"
1925

2026
int main(void) {
21-
queue *q = q_create(5);
27+
queue* q = q_create(5);
2228

2329
q_endqueue(q, 23);
2430
q_endqueue(q, 34);

AbstractDataType/queue.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,46 @@
1111

1212
#include "queue.h"
1313

14+
/**
15+
* @brief Queue data structure
16+
*
17+
* @details `values` is a dynamically allocated array that holds the queue entries.
18+
* `head` points to the front of the queue, where entries are dequeued.
19+
* `tail` points to the end of the queue, where entries are enqueued.
20+
* `num_entries` keeps track of the number of entries currently in the queue.
21+
* `size` reflects the maximum number of entries that can be stored in the underlying array.
22+
*/
1423
typedef struct _queue {
15-
int *values;
16-
int head, tail;
17-
int num_entries, size;
24+
int* values;
25+
int head, tail;
26+
int num_entries, size;
1827
} queue;
1928

20-
queue *q_create(int max_size) {
21-
queue *q = malloc(sizeof(queue));
22-
q->size = max_size;
23-
q->values = malloc(sizeof(int) * q->size);
29+
queue* q_create(int max_size) {
30+
queue* q = malloc(sizeof(queue));
31+
q->size = max_size;
32+
q->values = malloc(sizeof(int) * q->size);
2433
q->num_entries = 0; // the queue is empty for now
25-
q->head = 0;
26-
q->tail = 0;
34+
q->head = 0; // points to the front of the queue
35+
q->tail = 0; // points to the end of the queue
2736
return q;
2837
}
2938

30-
void q_destroy(queue *q) {
39+
void q_destroy(queue* q) {
3140
free(q->values);
3241
free(q);
3342
q = NULL;
3443
}
3544

36-
bool q_empty(queue *q) {
37-
return (q->num_entries == 0);
45+
bool q_empty(queue* q) {
46+
return (bool)(q->num_entries == 0);
3847
}
3948

40-
bool q_full(queue *q) {
41-
return (q->num_entries == q->size);
49+
bool q_full(queue* q) {
50+
return (bool)(q->num_entries == q->size);
4251
}
4352

44-
bool q_endqueue(queue *q, int value) {
53+
bool q_endqueue(queue* q, int value) {
4554
// no more entry space available
4655
if (q_full(q)) {
4756
return false;
@@ -53,14 +62,14 @@ bool q_endqueue(queue *q, int value) {
5362
return true;
5463
}
5564

56-
int q_dequeue(queue *q) {
65+
int q_dequeue(queue* q) {
5766
int result;
5867

5968
if (q_empty(q)) {
6069
return QUEUE_EMPTY;
6170
}
6271

63-
result = q->values[q->head];
72+
result = q->values[q->head];
6473
q->head = (q->head + 1) % q->size;
6574
q->num_entries--;
6675

AbstractDataType/queue.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
#include <stdio.h>
1212
#include <stdlib.h>
1313

14+
#ifndef QUEUE_EMPTY
1415
#define QUEUE_EMPTY INT_MIN
16+
#endif // !QUEUE_EMPTY
1517

1618
/**
1719
* @brief The compiler will treat "queue"
18-
* as in incomplete type.
20+
* as an incomplete type.
1921
* queue is an opaque data type.
2022
*/
2123
typedef struct _queue queue;
@@ -30,4 +32,4 @@ bool q_full(queue* q);
3032
bool q_endqueue(queue* q, int value);
3133
int q_dequeue(queue* q);
3234

33-
#endif
35+
#endif //!QUEUE_H

0 commit comments

Comments
 (0)