-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathqueue.h
136 lines (124 loc) · 3.03 KB
/
queue.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
* QUEUE
*
* Features:
* 1. Queue with capcity
*
* http://en.wikipedia.org/wiki/Queue_(data_structure)
*
******************************************************************************/
#ifndef ALGO_QUEUE_H__
#define ALGO_QUEUE_H__
#include <stdbool.h>
#include <stdint.h>
#include <exception>
namespace alg {
/**
* Queue Definition
*/
template<typename T>
class Queue {
private:
class QueueEmptyException: public std::exception {
public:
virtual const char * what() const throw() {
return "Queue is empty.";
}
} excp_empty;
private:
uint32_t m_capacity; // queue capacity
uint32_t m_size; // current queue size
uint32_t m_front; // index of the first element
uint32_t m_rear; // index of the last element
T * m_elements; // the elements
public:
/**
* constructor takes argument the maximum number of elements the Queue
* can hold, creates a Queue according to it and returns a pointer to the
* Queue.
*/
Queue(uint32_t max) {
this->m_elements = new T[max];
this->m_size = 0;
this->m_capacity = max;
this->m_front =0;
this->m_rear = -1;
};
~Queue() {
delete [] m_elements;
};
private:
Queue(const Queue &);
Queue& operator=(const Queue &);
public:
/**
* Dequeue
*/
inline void dequeue() {
/* If Queue size is zero then it is empty. So we cannot pop */
if(m_size==0) {
return;
}
/* Removing an element is equivalent to incrementing index of front by one */
else {
m_size--;
m_front++;
/* As we fill elements in circular fashion */
if(m_front==m_capacity) {
m_front=0;
}
}
return;
};
/**
* return the front element.
*/
inline const T& front() const {
if (m_size==0) throw excp_empty;
return m_elements[m_front];
};
/**
* test weather the queue is empty
*/
inline bool is_empty() const {
if (m_size ==0) return true;
return false;
};
/**
* enqueue an element
* returns false when queue is full
*/
bool enqueue(const T & element) {
// If the Queue is full, we cannot push an element into it
// as there is no space for it.*/
if(m_size == m_capacity) {
return false;
}
else {
m_size++;
m_rear++;
/* As we fill the queue in circular fashion */
if(m_rear == m_capacity) {
m_rear = 0;
}
/* Insert the element in its rear side */
m_elements[m_rear] = element;
return true;
}
};
/**
* return the queue count.
*/
inline uint32_t count() const { return m_size; };
/**
* return the queue capacity.
*/
inline uint32_t capcity() const { return m_capacity; };
};
}
#endif //