From 5437c61dfb6ffdf95abe17be243b51994c854083 Mon Sep 17 00:00:00 2001 From: sh0723 Date: Wed, 22 Jul 2026 12:36:18 +0900 Subject: [PATCH] =?UTF-8?q?deque=20V2=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../array/deque/deque_V2.cpp | 365 ++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 src/data_structure_implement/array/deque/deque_V2.cpp diff --git a/src/data_structure_implement/array/deque/deque_V2.cpp b/src/data_structure_implement/array/deque/deque_V2.cpp new file mode 100644 index 0000000..6851304 --- /dev/null +++ b/src/data_structure_implement/array/deque/deque_V2.cpp @@ -0,0 +1,365 @@ +#include +#include +#include +using namespace std; +class IntDeque { +public: + IntDeque(); + IntDeque(int *data, int size, int capacity); + ~IntDeque(); + + void pushFront(int value); + void pushBack(int value); + + void popFront(); + void popBack(); + + int& front(); + int& back(); + int& at(int index); + int& operator[](int index); + + int size() const; + int capacity() const; + bool empty() const; + + void clear(); + void reserve(int newCapacity); + +private: + int* data; + + int current_size; + int current_capacity; + + int front_index; + int rear_index; +}; +// 12:14시작 +IntDeque::IntDeque(int *dt, int size, int capacity) { + data = dt; + current_size = size; + current_capacity = capacity; +} +IntDeque::IntDeque() : data(nullptr), current_size(0), current_capacity(0), front_index(0), rear_index(0) {} +IntDeque::~IntDeque() { + clear(); + delete[] data; + current_capacity = 0; +} + +void IntDeque::pushFront(int value) { + if (current_size == current_capacity) { + reserve(current_capacity == 0 ? 1 : current_capacity * 2); + } + + front_index = (front_index - 1 + current_capacity) % current_capacity; + data[front_index] = value; + current_size++; +} +void IntDeque::pushBack(int value) { + if (current_size == current_capacity) { + reserve(current_capacity == 0 ? 1 : current_capacity * 2); + } + + rear_index = (rear_index+1) % current_capacity; + data[rear_index] = value; + current_size++; +} + +void IntDeque::popFront(){ + if (current_size == 0) return; + front_index = (front_index + 1) % current_capacity; + current_size--; +} +void IntDeque::popBack() { + if (current_size == 0) return; + rear_index = (rear_index - 1 + current_capacity) % current_capacity; + current_size--; +} + +int& IntDeque::front() { + if(current_size == 0) throw out_of_range("deque is empty"); + + return data[front_index]; +} +int& IntDeque::back() { + if(current_size == 0) throw out_of_range("deque is empty"); + + return data[rear_index]; +} +int& IntDeque::at(int index) { + if (index < 0 || index >= current_size) throw out_of_range("index out of range"); + + return data[(front_index + index % current_capacity)]; +} +int& IntDeque::operator[](int index) { + if (index < 0 || index >= current_size) throw out_of_range("index out of range"); + + return at(index); +} + +int IntDeque::size() const { + return current_size; +} +int IntDeque::capacity() const { + return current_capacity; +} +bool IntDeque::empty() const { + return current_size == 0; +} + +void IntDeque::clear() { + current_size = 0; + front_index = 0; + rear_index = 0; +} +void IntDeque::reserve(int newCapacity) { + if (newCapacity < 0 || newCapacity <= current_capacity) return; + + int *newData = new int[newCapacity]; + for (int i=0; i