161 lines
3.5 KiB
C++
161 lines
3.5 KiB
C++
#pragma once
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include "queue.h"
|
|
//As a Circle
|
|
template <class T>
|
|
class arrayQueue : public Queue<T> {
|
|
private:
|
|
T* element;
|
|
int queueFront;
|
|
int queueBack;
|
|
int arrayLength;
|
|
public:
|
|
virtual ~arrayQueue() { delete[] element; }
|
|
virtual bool empty() const override;
|
|
virtual int size() const override;
|
|
virtual T& front() override;
|
|
virtual T& back() override;
|
|
virtual void pop() override;
|
|
virtual void push(const T& theElement) override;
|
|
void showQueue() const;
|
|
void showArray() const;
|
|
|
|
arrayQueue(int a = 10);
|
|
arrayQueue(const arrayQueue& x);
|
|
arrayQueue& operator=(const arrayQueue& x);
|
|
|
|
};
|
|
|
|
template<class T>
|
|
inline bool arrayQueue<T>::empty() const
|
|
{
|
|
return arrayLength == 0;
|
|
}
|
|
|
|
template<class T>
|
|
inline int arrayQueue<T>::size() const
|
|
{
|
|
return arrayLength;
|
|
}
|
|
|
|
template<class T>
|
|
inline T& arrayQueue<T>::front()
|
|
{
|
|
return element[queueFront];
|
|
}
|
|
|
|
template<class T>
|
|
inline T& arrayQueue<T>::back()
|
|
{
|
|
return element[queueBack];
|
|
}
|
|
|
|
template<class T>
|
|
inline void arrayQueue<T>::pop()
|
|
{
|
|
if (queueFront == queueBack) {
|
|
std::cout << "Empty Queue" << std::endl;
|
|
}
|
|
queueFront = (queueFront + 1) % arrayLength;
|
|
element[queueFront].~T();
|
|
}
|
|
|
|
template<class T>
|
|
inline void arrayQueue<T>::push(const T& theElement)
|
|
{
|
|
if ((queueBack + 1) % arrayLength == queueFront) {
|
|
//Add Length: Remember it's a Circle
|
|
T* tmp = new T[arrayLength * 2];
|
|
int start = (queueFront + 1) % arrayLength;
|
|
int cnt = 1;
|
|
if (start < 2)
|
|
{
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
tmp[i] = element[i];
|
|
}
|
|
}
|
|
//from queueFront to arrayLength, all copy to last;
|
|
|
|
else {
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
tmp[i] = element[i];
|
|
}
|
|
for (int i = arrayLength * 2 - 1; i >= queueFront; i--) {
|
|
tmp[i] = element[arrayLength - cnt];
|
|
cnt++;
|
|
}
|
|
}
|
|
queueFront = start + arrayLength - 1;
|
|
queueBack = start - 2;
|
|
arrayLength *= 2;
|
|
delete[] element;
|
|
element = tmp;
|
|
}
|
|
queueBack = (queueBack + 1) % arrayLength;
|
|
element[queueBack] = theElement;
|
|
}
|
|
|
|
template<class T>
|
|
inline void arrayQueue<T>::showQueue() const
|
|
{
|
|
int tmp = queueFront;
|
|
while (tmp != queueBack) {
|
|
tmp = (tmp + 1) % arrayLength;
|
|
element[tmp] < 0 ? std::cout << -1 << " " : std::cout << element[tmp] << " ";
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
template<class T>
|
|
inline void arrayQueue<T>::showArray() const
|
|
{
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
std::cout << std::left << std::setw(4) << i;
|
|
}
|
|
std::cout << std::endl;
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
element[i] < 0 ? std::cout << std::left << std::setw(4) << -1 : std::cout << std::left << std::setw(4) << element[i];
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
template<class T>
|
|
inline arrayQueue<T>::arrayQueue(int a): arrayLength(a), queueBack(0), queueFront(0)
|
|
{
|
|
element = new T[a];
|
|
}
|
|
|
|
template<class T>
|
|
inline arrayQueue<T>::arrayQueue(const arrayQueue& x): arrayLength(x.arrayLength), queueBack(x.queueBack), queueFront(x.queueFront)
|
|
{
|
|
element = new T[arrayLength];
|
|
int tmp = queueFront;
|
|
while (tmp != queueBack) {
|
|
element[tmp] = x.element[tmp];
|
|
tmp = (tmp + 1) % arrayLength;
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
inline arrayQueue<T>& arrayQueue<T>::operator=(const arrayQueue& x)
|
|
{
|
|
if (this != &x) {
|
|
delete[] element;
|
|
arrayLength = x.arrayLength;
|
|
queueBack = x.queueBack;
|
|
queueFront = x.queueFront;
|
|
element = new T[arrayLength];
|
|
int tmp = queueFront;
|
|
while (tmp != queueBack) {
|
|
element[tmp] = x.element[tmp];
|
|
tmp = (tmp + 1) % arrayLength;
|
|
}
|
|
}
|
|
else {
|
|
std::cout << "Can't Copy Itself" << std::endl;
|
|
}
|
|
return *this;
|
|
}
|
|
|