diff --git a/include/circularQueue/circularQueue.hpp b/include/circularQueue/circularQueue.hpp new file mode 100644 index 0000000..02e1f9a --- /dev/null +++ b/include/circularQueue/circularQueue.hpp @@ -0,0 +1,16 @@ +#ifndef circularQueue_HPP +#define circularQueue_HPP + +int arr; + +class CirQueue { +public: + void enqueue(); + void dequeue(); + bool isEmpty(); + bool isFull(); + int size(); + +}; + +#endif // circularQueue_HPP \ No newline at end of file diff --git a/include/queue/queue.hpp b/include/queue/queue.hpp index 05f2dfe..81c4d4e 100644 --- a/include/queue/queue.hpp +++ b/include/queue/queue.hpp @@ -8,6 +8,7 @@ class Queue { private: int sp; int ep; + int count; Data* buffer; public: Queue(); diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index b7f341c..f70e431 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -1,7 +1,7 @@ #include "queue/queue.hpp" template -Queue::Queue() : sp(0), ep(0) { +Queue::Queue() : sp(0), ep(0), count(0) { buffer = new Data[QUEUE_SIZE]; } @@ -14,23 +14,49 @@ template void Queue::clear() { sp = 0; ep = 0; + count = 0; } + + template void Queue::push(Data data) { + buffer[ep] = data; + //가득차면 돌아와야됨 + ep = (ep+1)%QUEUE_SIZE; + + + if(count == QUEUE_SIZE){ + // 가득 찼으면 가장 오래된 원소를 덮어씀 + sp = (sp + 1) % QUEUE_SIZE; + } + else{ + count++; + } + return; } template Data Queue::pull() { - return buffer[sp]; + if (count == 0) { + throw std::invalid_argument("empty queue"); + } + + Data ret = buffer[sp]; + sp = (sp + 1) % QUEUE_SIZE; + count--; + return ret; } template Data Queue::top() { + if (count == 0) { + throw std::invalid_argument("empty queue"); + } return buffer[sp]; } template int Queue::size() { - return 0; + return count; } \ No newline at end of file diff --git a/include/stick/stick.hpp b/include/stick/stick.hpp index 7716a2d..be55e5d 100644 --- a/include/stick/stick.hpp +++ b/include/stick/stick.hpp @@ -11,10 +11,6 @@ class Stick { void divide2(vector& stick, int idx, int N); int get_cnt(vector stick); int ans(vector stick, int N); - - - - }; diff --git a/src/circularQueue/circularQueue.cpp b/src/circularQueue/circularQueue.cpp new file mode 100644 index 0000000..1f4999e --- /dev/null +++ b/src/circularQueue/circularQueue.cpp @@ -0,0 +1,21 @@ +#include "circularQueue/circularQueue.hpp" + +void CirQueue::enqueue(){ + +} + +void CirQueue::dequeue(){ + +} + +bool CirQueue::isEmpty(){ + +} + +bool CirQueue::isFull(){ + +} + +int CirQueue::size(){ + +} diff --git a/src/stick/stick.cpp b/src/stick/stick.cpp index b0448fb..94a532b 100644 --- a/src/stick/stick.cpp +++ b/src/stick/stick.cpp @@ -41,6 +41,10 @@ int Stick::get_cnt(vector stick){ return temp; } + + + + int Stick::ans(vector stick, int N){ while(sum(stick)!=N){ int idx = find_minidx(stick); diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..863a0c8 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -42,4 +42,38 @@ TEST_F(QueueTest, push_and_pull) { // then EXPECT_EQ(second, v2); -} \ No newline at end of file +} + +TEST_F(QueueTest, size){ + int v1 = 1; + int v2 = 2; + int_queue.push(v1); + int_queue.push(v2); + + EXPECT_EQ(int_queue.size(), 2); + +} + +TEST_F(QueueTest, push_in_fullqueue) { + for (int i = 0; i < 20; i++) { + int_queue.push(i); + } + + int_queue.push(20); + EXPECT_EQ(int_queue.top(), 1); +} + +TEST_F(QueueTest, pull_emptyQueue){ + EXPECT_THROW(int_queue.pull(), std::invalid_argument); +} + +TEST_F(QueueTest, push_getSize){ + for(int i=0; i