Queue 구현 (c++)
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include<iostream> class Queue { int* data; // 데이터를 저장하는 배열 int q_size; // 큐의 사이즈 int front; // 가장 먼저 넣은 요소 표시 int rear; // 가장 나중에 넣은 요소 표시 public: Queue(int n); void push(int x); void pop(); bool is_full(); bool empty(); int size(); void print(); ~Queue(); }; Queue::Queue(int n) : data(new int[n]), q_size(n), front(0), rear(0) {} void Queue::push(int x) { if (!is_full()) { data[rear] = x; rear = (rear + 1) % q_size; } else { std::cout << "큐가 가득 찼습니다!" << std::endl; } } void Queue::pop() { if (!empty()) { front = (front + 1) % q_size; } else { std::cout << "큐가 비어있습니다!" << std::endl; } } bool Queue::is_full() { if ((rear + 1) % q_size == front) { return true; } else { return false; } } bool Queue::empty() { if (front == rear) { return true; } else { return false; } } int Queue::size() { return rear - front; } void Queue::print() { for (int i = front; i < rear; i++) { std::cout << data[i] << std::endl; } } Queue::~Queue() { delete[] data; } int main() { Queue q(10); q.push(1); q.push(3); q.push(5); q.push(7); q.push(9); q.pop(); q.pop(); std::cout << "size : " << q.size() << std::endl; q.print(); } | cs |
'자료구조' 카테고리의 다른 글
| Stack 구현 (c++) (0) | 2020.08.25 |
|---|---|
| Vector 구현 (c++) (0) | 2020.08.25 |
TAGS.
