circular_queue.h
Go to the documentation of this file.
1 
23 #ifndef CIRCULAR_QUEUE_H_
24 #define CIRCULAR_QUEUE_H_
25 
26 #include <iostream>
27 
28 namespace micros_swarm{
29  template <class Type>
30  class cqueue
31  {
32  public:
34  {
35  tail_ = 0;
36  head_ = 0;
37  capacity_ = 0;
38  data_ = NULL;
39  }
40 
41  cqueue(int capacity)
42  {
43  tail_ = 0;
44  head_ = 0;
45  capacity_ = capacity;
46  data_ = new Type[capacity_];
47  }
48 
49  cqueue(const cqueue& c)
50  {
51  tail_ = c.tail_;
52  head_ = c.head_;
53  capacity_ = c.capacity_;
54  data_ = new Type[capacity_];
55  memcpy(data_, c.data_, sizeof(Type)*c.length());
56  }
57 
58  cqueue& operator=(const cqueue& c)
59  {
60  if(this == &c) {
61  return *this;
62  }
63  delete [] data_;
64  tail_ = c.tail_;
65  head_ = c.head_;
66  capacity_ = c.capacity_;
67  data_ = new Type[capacity_];
68  memcpy(data_, c.data_, sizeof(Type)*c.length());
69  return *this;
70  }
71 
73  {
74  delete []data_;
75  }
76 
77  bool empty()
78  {
79  if (head_ == tail_) {
80  return true;
81  }
82  else {
83  return false;
84  }
85  }
86 
87  bool full()
88  {
89  if ((tail_+1)%capacity_ == head_) {
90  return true;
91  }
92  else {
93  return false;
94  }
95  }
96 
97  void push(Type x)
98  {
99  if (!full()) {
100  data_[tail_] = x;
101  tail_ = (tail_+1)%capacity_;
102  }
103  else {
104  std::cout<<"cqueue is full."<<std::endl;
105  }
106  }
107 
108  void pop()
109  {
110  if (!empty()) {
111  head_ = (head_ + 1) % capacity_;
112  }
113  else {
114  std::cout<<"cqueue is empty."<<std::endl;
115  }
116  }
117 
118  const Type& front()
119  {
120  return data_[head_];
121  }
122 
123  int size()
124  {
125  return (tail_-head_+capacity_)%capacity_;
126  }
127 
128  void print()
129  {
130  int cnt = head_;
131  if (head_ > tail_) {
132  tail_ += capacity_;
133  }
134  while(cnt <= tail_-1) {
135  std::cout << data_[cnt] << " ";
136  cnt++;
137  }
138  std::cout<<std::endl;
139  }
140  private:
141  Type *data_;
143  int tail_;
144  int head_;
145  };
146 };
147 
148 #endif
const Type & front()
cqueue & operator=(const cqueue &c)
cqueue(const cqueue &c)
cqueue(int capacity)


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06