This commit is contained in:
e2hang
2025-07-26 16:00:30 +08:00
parent fdbc36e348
commit bbfcdd1a16
4 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
#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;
}

62
Queue/arrayQueue/main.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "arrayQueue.h"
using namespace std;
int main() {
arrayQueue<int> a(10);
cout << "If a is empty? : " << a.empty() << endl;
a.push(1); a.push(2); a.push(3); a.push(4); a.push(5);
a.showArray();
a.showQueue();
a.pop();
a.pop();
a.showArray();
a.showQueue();
a.push(6); a.push(7); a.push(8); a.push(9); a.push(10);
a.showArray();
a.showQueue();
a.push(11);
a.showArray();
a.showQueue();
a.push(12);
a.showArray();
a.showQueue();
a.push(13); a.push(14); a.push(15);
a.showArray();
a.showQueue();
a.push(16); a.push(17); a.push(18); a.push(19); a.push(20);
a.showArray();
a.showQueue();
a.push(21);
a.showArray();
a.showQueue();
a.push(22);
a.showArray();
a.showQueue();
return 0;
}
/*
This is the result, Perfectly match
-1 1 2 3 4 5 -1 -1 -1 -1
3 4 5
0 1 2 3 4 5 6 7 8 9
10 1 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
10 11 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
10 11 12 0 0 1 40 0 159 -1 10 11 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 40 0 159 -1 10 11 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 16 17 18 19 20 11 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 16 17 18 19 20 21 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
0 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
10 11 12 13 14 15 16 17 18 19 20 21 22 0 0 1 80 0 406 -1 10 11 12 13 14 15 16 17 18 19 20 21 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
*/

13
Queue/arrayQueue/queue.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
//Abstract Class
template <class T>
class Queue {
public:
virtual ~Queue() = default;
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& front() = 0;
virtual T& back() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
};

0
Queue/chainQueue/p211 Normal file
View File