-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayqueue.h
More file actions
71 lines (61 loc) · 1.48 KB
/
Arrayqueue.h
File metadata and controls
71 lines (61 loc) · 1.48 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
#ifndef __ARRAYQUEUE__
#define __ARRAYQUEUE__
#include <iostream>
template <typename T>
class Arrayqueue
{
private:
T* _array;
int _head;
int _end;
int _count;
int _capacity;
public:
Arrayqueue(const int& capacity = 10);
~Arrayqueue();
Arrayqueue<T>& push(const T& value);
Arrayqueue<T>& pop();
int size() const { return _count; }
T get_head() const{ return _array[_head]; }
bool IsEmpty() const { return _count == 0; }
bool IsFull() const { return _count == _capacity; }
T* get_array() const { return _array; } //this method is used to test code!It should NOT appear in release version!
};
template <typename T>
inline
Arrayqueue<T>::Arrayqueue(const int& capacity)
: _count(0), _head(0), _end(-1), _capacity(capacity)
{
_array = new T[_capacity];
}
template <typename T>
inline
Arrayqueue<T>::~Arrayqueue()
{
delete[] _array;
}
template <typename T>
inline
Arrayqueue<T>& Arrayqueue<T>::push(const T& value){
if(this->IsFull()){
std::cout << "Arrayqueue is full!NO MORE PUSH!" << std::endl;
return *this;
}
_end = (_end + 1) % _capacity;
_array[_end] = value;
_count++;
return *this;
}
template <typename T>
inline
Arrayqueue<T>& Arrayqueue<T>::pop(){
if(this->IsEmpty()){
std::cout << "Arrayqueue is empty!NO MORE POP!" << std::endl;
return *this;
}
_array[_head] = 0;
_head = (_head + 1) % _capacity;
_count--;
return *this;
}
#endif