티스토리 뷰

알고리즘

[DataStructure] Queue?

seunggabi 승가비 2018. 9. 13. 18:04

[DataStructure] Queue?


// Queue
// enqueue, dequeue, isEmpty
// generic template class

#include <iostream>
using namespace std;

template <class T>
class Queue {
private:
T *queue;
int size;
int front, back;

public:
Queue(int size) {
this->queue = new T[size];
this->size = size;
this->front = this->back = 0;
}

void enqueue(T item) {
int pos = this->back++ % size;
this->queue[pos] = item;
}

T dequeue() {
int pos = this->front++ % size;
return this->queue[pos];
}

bool isEmpty() {
return this->front == this->back;
}

bool isFull() {
return this->front == (this->back+1) % this->size;
}
};

#define SIZE 1000000

int main() {

Queue<int> *q = new Queue<int>(SIZE);

q->enqueue(3);
q->enqueue(4);
q->enqueue(5);

cout << q->dequeue() << endl;
cout << q->dequeue() << endl;
cout << q->dequeue() << endl;

return 0;
}


'알고리즘' 카테고리의 다른 글

[BFS & DFS] tomato  (0) 2018.09.16
[DataStructure] Queue?  (0) 2018.09.13
[Sort] QuickSort  (0) 2018.09.11
[DP] changeCoin?  (0) 2018.09.08
[Greedy] jewelryThief - priority_queue(heap)  (0) 2018.09.08
[Math] changeProposition?  (0) 2018.09.08
댓글
댓글쓰기 폼