From 112a0d344da9e66d20ee9ab5d488e59c5d52e6ad Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 9 Apr 2020 18:09:42 +0300 Subject: added new examples --- Templates/Queue.hpp | 121 ---------------------------------------------- Templates/Queue/Queue.hpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++ Templates/Queue/main.cpp | 23 +++++++++ Templates/main.cpp | 23 --------- Templates/test.txt | 0 5 files changed, 144 insertions(+), 144 deletions(-) delete mode 100644 Templates/Queue.hpp create mode 100644 Templates/Queue/Queue.hpp create mode 100644 Templates/Queue/main.cpp delete mode 100644 Templates/main.cpp create mode 100644 Templates/test.txt (limited to 'Templates') diff --git a/Templates/Queue.hpp b/Templates/Queue.hpp deleted file mode 100644 index 128f1df..0000000 --- a/Templates/Queue.hpp +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef QUEUE_HDR -#define QUEUE_HDR - -#include -using namespace std; - -template -class Queue { -private: - T * arr; - int capacity; - int count; - - int whereToPut; - int whereToGet; - void Resize(); - void CopyFrom(const Queue& other); - void Free(); - -public: - Queue(); - Queue(const Queue& other); - Queue& operator=(const Queue& other); - ~Queue(); - - bool isEmpty() const; - void Push(T el); - T Pop(); - T& Peek(); -}; - -template -void Queue::Resize() { - T* temp = new T[capacity * 2]; - for (int i = 0; i < capacity; ++i) - temp[i] = Pop(); - whereToGet = 0; - whereToPut = capacity; - count = capacity; - capacity *= 2; - delete[] arr; - arr = temp; -} - -template -void Queue::CopyFrom(const Queue& other) { - arr = new T[other.capacity]; - for (int i = 0; i < other.count; ++i) - arr[i] = other.arr[i]; - count = other.count; - capacity = other.capacity; - whereToPut = other.whereToPut; - whereToGet = other.whereToGet; -} - -template -void Queue::Free() { - delete[] arr; - count = capacity = 0; -} - -template -Queue::Queue() { - arr = new T[4]; - capacity = 4; - count = 0; - whereToPut = 0; - whereToGet = 0; -} - -template -Queue::Queue(const Queue& other) { - CopyFrom(other); -} - -template -Queue& Queue::operator=(const Queue& other) { - if (this != &other) { - Free(); - CopyFrom(other); - } - return *this; -} - -template -Queue::~Queue() { - Free(); -} - -template -bool Queue::isEmpty() const { - return count == 0; -} - -template -void Queue::Push(T el) { - if (count == capacity) - Resize(); - arr[whereToPut] = el; - (whereToPut += 1) %= capacity; - count++; -} - -template -T Queue::Pop() { - if (count == 0) - throw "Queue is empty"; - T elToReturn = arr[whereToGet]; - count--; - (whereToGet += 1) %= capacity; - return elToReturn; -} - -template -T& Queue::Peek() { - if (isEmpty()) - throw std::logic_error("empty queue"); - return arr[whereToGet]; -} - -#endif // !QUEUE_HDR diff --git a/Templates/Queue/Queue.hpp b/Templates/Queue/Queue.hpp new file mode 100644 index 0000000..128f1df --- /dev/null +++ b/Templates/Queue/Queue.hpp @@ -0,0 +1,121 @@ +#ifndef QUEUE_HDR +#define QUEUE_HDR + +#include +using namespace std; + +template +class Queue { +private: + T * arr; + int capacity; + int count; + + int whereToPut; + int whereToGet; + void Resize(); + void CopyFrom(const Queue& other); + void Free(); + +public: + Queue(); + Queue(const Queue& other); + Queue& operator=(const Queue& other); + ~Queue(); + + bool isEmpty() const; + void Push(T el); + T Pop(); + T& Peek(); +}; + +template +void Queue::Resize() { + T* temp = new T[capacity * 2]; + for (int i = 0; i < capacity; ++i) + temp[i] = Pop(); + whereToGet = 0; + whereToPut = capacity; + count = capacity; + capacity *= 2; + delete[] arr; + arr = temp; +} + +template +void Queue::CopyFrom(const Queue& other) { + arr = new T[other.capacity]; + for (int i = 0; i < other.count; ++i) + arr[i] = other.arr[i]; + count = other.count; + capacity = other.capacity; + whereToPut = other.whereToPut; + whereToGet = other.whereToGet; +} + +template +void Queue::Free() { + delete[] arr; + count = capacity = 0; +} + +template +Queue::Queue() { + arr = new T[4]; + capacity = 4; + count = 0; + whereToPut = 0; + whereToGet = 0; +} + +template +Queue::Queue(const Queue& other) { + CopyFrom(other); +} + +template +Queue& Queue::operator=(const Queue& other) { + if (this != &other) { + Free(); + CopyFrom(other); + } + return *this; +} + +template +Queue::~Queue() { + Free(); +} + +template +bool Queue::isEmpty() const { + return count == 0; +} + +template +void Queue::Push(T el) { + if (count == capacity) + Resize(); + arr[whereToPut] = el; + (whereToPut += 1) %= capacity; + count++; +} + +template +T Queue::Pop() { + if (count == 0) + throw "Queue is empty"; + T elToReturn = arr[whereToGet]; + count--; + (whereToGet += 1) %= capacity; + return elToReturn; +} + +template +T& Queue::Peek() { + if (isEmpty()) + throw std::logic_error("empty queue"); + return arr[whereToGet]; +} + +#endif // !QUEUE_HDR diff --git a/Templates/Queue/main.cpp b/Templates/Queue/main.cpp new file mode 100644 index 0000000..89b8468 --- /dev/null +++ b/Templates/Queue/main.cpp @@ -0,0 +1,23 @@ +#include "Queue.hpp" + + +int main() +{ + Queue q; + q.Enqueue(3); + q.Dequeue(); + q.Enqueue(1); + q.Enqueue(2); + q.Enqueue(3); + q.Enqueue(4); + q.Enqueue(5); + q.Enqueue(6); + cout< q; - q.Enqueue(3); - q.Dequeue(); - q.Enqueue(1); - q.Enqueue(2); - q.Enqueue(3); - q.Enqueue(4); - q.Enqueue(5); - q.Enqueue(6); - cout<