Notice
Recent Posts
Recent Comments
Creative Code
queue3(array클래스와 has -a관계) 본문
※main.cpp파일
#include <iostream>
#include "queue.h"
int main(void)
{
Queue s1(10), s2(100);
s1.push(100);
s1.push(200);
std::cout << "s1 1st pop() : " << s1.pop() << std::endl;
s1.push(300);
std::cout << "s1 2nd pop() : " << s1.pop() << std::endl;
std::cout << "s1 3rd pop() : " << s1.pop() << std::endl;
s2.push(900);
s2.push(800);
std::cout << "s2 1st pop() : " << s2.pop() << std::endl;
s2.push(700);
std::cout << "s2 2nd pop() : " << s2.pop() << std::endl;
std::cout << "s2 3rd pop() : " << s2.pop() << std::endl;
return 0;
}
※queue.h파일
#ifndef QUEUE_H
#define QUEUE_H
#include "array.h"
class Queue {
private:
static const int QUEUESIZE;
Array arr_;
int rear_;
int front_;
Queue(const Queue& rhs);
Queue& operator=(const Queue& rhs);
public:
explicit Queue(int size = Queue::QUEUESIZE);
~Queue();
bool isFull() const;
bool isEmpty() const;
void push(int data);
int pop();
};
#endif
※queue.cpp파일
#include <cstdlib>
#include "queue.h"
#include <cassert>
const int QUEUESIZE = Array::getDefaultSize();
Queue::Queue(int size)
:arr_(size),rear_(0),front_(0)
{
}
Queue::~Queue()
{
}
bool Queue::isFull() const {
return rear_ == arr_.size();
}
bool Queue::isEmpty() const {
return front_ == rear_;
}
void Queue::push(int data)
{
assert(!isFull());
arr_[rear_] = data;
++rear_;
}
int Queue::pop()
{
assert(!isEmpty());
int index = front_;
++front_;
return arr_[index];
}
'C++ Programming' 카테고리의 다른 글
shape(도형 상속) (0) | 2023.09.07 |
---|---|
safeArray2(상속) (0) | 2023.09.07 |
queue2(생성자 초기화 리스트, static member, explicit) (0) | 2023.09.06 |
safeArray(배열 크기) (1) | 2023.09.06 |
stack3(stack,array클래스 연결) (0) | 2023.09.06 |