Repository navigation
Expand file tree
/
Copy pathqueue.h
More file actions
100 lines (88 loc) · 2.54 KB
/
Copy pathqueue.h
File metadata and controls
100 lines (88 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef QUEUE_JL
#define QUEUE_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define QUEUE_MAX_SIZE 100 // The maximum number of elements that we can store in the queue.
// Define how our (linear and dynamic) queue is structured.
typedef struct{
int data[QUEUE_MAX_SIZE]; // The internal array to store our queue elements.
int front; // The index value of the front element.
int rear; // The index value of the last element.
int size; // The current number of elements in our queue.
} Queue;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Queue. We start setting size to 0, front to 0 and rear to -1.
void initQueue(Queue *queue){
queue->size = 0;
queue->front = 0;
queue->rear = -1;
}
// Check if the Queue is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyQueue(Queue *queue){
if(queue->size == 0){
return 1;
}
else{
return 0;
}
}
// Check if the Queue is Full. Returns 1 if it's full or 0 if it's not full.
int isFullQueue(Queue *queue){
if(queue->size >= QUEUE_MAX_SIZE){
return 1;
}
else{
return 0;
}
}
// Add an Element to the Rear of the Queue. Returns 1 if successful or 0 if the queue is full.
int enQueue(Queue *queue, int value){
if(isFullQueue(queue)){
return 0;
}
else{
queue->rear = (queue->rear+1) % QUEUE_MAX_SIZE;
queue->data[queue->rear] = value;
queue->size++;
return 1;
}
}
// Remove an Element from the front of the Queue. Returns 1 if successful or 0 if the queue is empty.
int deQueue(Queue *queue, int *value){
if(isEmptyQueue(queue)){
return 0;
}
else{
*value = queue->data[queue->front];
queue->front = (queue->front+1) % QUEUE_MAX_SIZE;
queue->size--;
return 1;
}
}
// Peek at the Front Element without Removing It. Returns 1 if successful or 0 if the queue is empty.
int peekQueue(Queue *queue, int *value){
if(isEmptyQueue(queue)){
return 0;
}
else{
*value = queue->data[queue->front];
return 1;
}
}
// Print all Elements of the Queue. Displays elements from front to rear.
void printQueue(Queue *queue){
printf("[");
for(int i = 0; i < queue->size; i++){
int index = (queue->front+i) % QUEUE_MAX_SIZE;
printf("%d", queue->data[index]);
if(i < queue->size-1){
printf(", ");
}
}
printf("]\n");
}
#endif //QUEUE_JL