From bbfcdd1a164cead8ffbd0ea592816e3acc4fbcc3 Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sat, 26 Jul 2025 16:00:30 +0800 Subject: [PATCH] queue --- Queue/arrayQueue/arrayQueue.h | 160 ++++++++++++++++++++++++++++++++++ Queue/arrayQueue/main.cpp | 62 +++++++++++++ Queue/arrayQueue/queue.h | 13 +++ Queue/chainQueue/p211 | 0 4 files changed, 235 insertions(+) create mode 100644 Queue/arrayQueue/arrayQueue.h create mode 100644 Queue/arrayQueue/main.cpp create mode 100644 Queue/arrayQueue/queue.h create mode 100644 Queue/chainQueue/p211 diff --git a/Queue/arrayQueue/arrayQueue.h b/Queue/arrayQueue/arrayQueue.h new file mode 100644 index 0000000..8d8ee9d --- /dev/null +++ b/Queue/arrayQueue/arrayQueue.h @@ -0,0 +1,160 @@ +#pragma once +#include +#include +#include "queue.h" +//As a Circle +template +class arrayQueue : public Queue { +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 +inline bool arrayQueue::empty() const +{ + return arrayLength == 0; +} + +template +inline int arrayQueue::size() const +{ + return arrayLength; +} + +template +inline T& arrayQueue::front() +{ + return element[queueFront]; +} + +template +inline T& arrayQueue::back() +{ + return element[queueBack]; +} + +template +inline void arrayQueue::pop() +{ + if (queueFront == queueBack) { + std::cout << "Empty Queue" << std::endl; + } + queueFront = (queueFront + 1) % arrayLength; + element[queueFront].~T(); +} + +template +inline void arrayQueue::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 +inline void arrayQueue::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 +inline void arrayQueue::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 +inline arrayQueue::arrayQueue(int a): arrayLength(a), queueBack(0), queueFront(0) +{ + element = new T[a]; +} + +template +inline arrayQueue::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 +inline arrayQueue& arrayQueue::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; +} + diff --git a/Queue/arrayQueue/main.cpp b/Queue/arrayQueue/main.cpp new file mode 100644 index 0000000..dcd07d6 --- /dev/null +++ b/Queue/arrayQueue/main.cpp @@ -0,0 +1,62 @@ +#include "arrayQueue.h" +using namespace std; +int main() { + arrayQueue 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 +*/ \ No newline at end of file diff --git a/Queue/arrayQueue/queue.h b/Queue/arrayQueue/queue.h new file mode 100644 index 0000000..7758184 --- /dev/null +++ b/Queue/arrayQueue/queue.h @@ -0,0 +1,13 @@ +#pragma once +//Abstract Class +template +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; +}; \ No newline at end of file diff --git a/Queue/chainQueue/p211 b/Queue/chainQueue/p211 new file mode 100644 index 0000000..e69de29