Recent Posts
printf("ho_tari\n");
Queue4 (template 지정) 본문
<main.cpp>
#include <iostream>
#include "queue.h"
int main() {
Queue<int>s1;
s1.push(100);
s1.push(200);
s1.push(300);
std::cout << "s1 first pop() : " << s1.pop() << std::endl;
std::cout << "s1 second pop() : " << s1.pop() << std::endl;
std::cout << "s1 third pop() : " << s1.pop() << std::endl;
Queue<double>s2;
s2.push(1.1);
s2.push(2.2);
s2.push(3.3);
std::cout << "s2 first pop() : " << s2.pop() << std::endl;
std::cout << "s2 second pop() : " << s2.pop() << std::endl;
std::cout << "s2 third pop() : " << s2.pop() << std::endl;
return 0;
}
<queue.h>
#ifndef QUEUE_H
#define QUEUE_H
#include <cstdlib>
#include <cassert>
#include "array.h"
template <typename T>
class Queue {
private:
static const int QUEUESIZE;
Array<T> arr_;
int rear_;
int front_;
Queue(const Queue<T>& rhs);
Queue& operator=(const Queue<T>& rhs);
public:
explicit Queue(int size = Queue<T>::QUEUESIZE);
~Queue();
bool isFull() const;
bool isEmpty() const;
void push(const T& data);
const T pop();
};
template <typename T>
const int Queue<T>::QUEUESIZE = Array<T>::getDefaultSize();
template <typename T>
Queue<T>::Queue(int size)
:arr_(size),rear_(0),front_(0)
{
}
template <typename T>
Queue<T>::~Queue()
{
}
template <typename T>
bool Queue<T>::isFull() const {
return (rear_+1)%arr_.size() == front_;
}
template <typename T>
bool Queue<T>::isEmpty() const {
return front_ == rear_;
}
template <typename T>
void Queue<T>::push(const T& data)
{
assert(!isFull());
arr_[rear_] = data;
rear_ = (rear_+1)%arr_.size();
}
template <typename T>
const T Queue<T>::pop()
{
assert(!isEmpty());
int index = front_;
front_ = (front_+1)%arr_.size();
return arr_[index];
}
#endif
<compile 결과>
'C++' 카테고리의 다른 글
SafeArray3 (template 사용) (0) | 2023.09.08 |
---|---|
Swap2 (template 사용) (0) | 2023.09.08 |
Stack4 (template 지정) (0) | 2023.09.08 |
Array2 (template 지정) (0) | 2023.09.08 |
BoundArray (index 지정) (0) | 2023.09.08 |