-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.h
More file actions
63 lines (58 loc) · 1.47 KB
/
queue.h
File metadata and controls
63 lines (58 loc) · 1.47 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
#ifndef QUEUE_H
#define QUEUE_H
#include <stdlib.h>
//Array implementation of Queue Data-Structure
struct Queue
{
int front, rear, size;
unsigned capacity;
float* array;
};
//initiates the Queue
struct Queue *createQueue(unsigned capacity)
{
struct Queue *queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (float*) malloc(queue->capacity * sizeof(float));
return queue;
}
//removes elementes from the queue
float deQueue(struct Queue *queue)
{
if (queue->size == 0)
return 0;
float item = queue->array[queue->front];
queue->front = (queue->front + 1)%queue->capacity;
queue->size = queue->size - 1;
return item;
}
//add elements to the queue
void enQueue(struct Queue *queue, float item)
{
if (queue->size == queue->capacity)
deQueue(queue);
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
}
//less safe but faster implementation of deQueue->enQueue
void denQueue(struct Queue *queue, float item)
{
queue->front = (queue->front + 1)%queue->capacity;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
}
//return the elt'th last stored value
float getElt(struct Queue *queue, int elt)
{
// If queue is empty, return NULL.
return queue->array[(queue->front + elt)%queue->capacity];
}
// free
float clear(struct Queue *queue){
free(queue->array);
free(queue);
}
#endif