-
자료구조: Circular Queue 구현하기 (feat. c++)알고리즘/자료구조 2021. 5. 31. 18:57
Characteristic:
- queue class is an array Circular Queue class
Operations:
- enqueue
- dequeue
- front
- isEmpty
#include<iostream> #include<assert.h> using namespace std; const int maxQueue = 10; int nextPos(int p); template<class QueueElementType> class queue { public: queue(); void enqueue(QueueElementType e); QueueElementType dequeue(); QueueElementType front(); bool isEmpty(); private: int f; int r; QueueElementType queueArray[maxQueue]; }; int nextPos(int p) { if (p == maxQueue - 1) return 0; else return p + 1; } template<class QueueElementType> queue<QueueElementType>::queue() { f = 0; r = 0; } template<class QueueElementType> void queue<QueueElementType>::enqueue(QueueElementType e) { assert(nextPos(r) != f); r = nextPos(r); queueArray[r] = e; } template<class QueueElementType> QueueElementType queue<QueueElementType>::dequeue() { assert(f != r); f = nextPos(f); return queueArray[f]; } template<class QueueElementType> QueueElementType queue<QueueElementType>::front() { assert(f != r); return queueArray[nextPos(f)]; } template<class QueueElementType> bool queue<QueueElementType>::isEmpty(){ return (f == r); } int main() { queue<char> q; char x; cin >> x; while (x != 'q') { q.enqueue(x); cin >> x; } while (!q.isEmpty()) { cout << q.dequeue()<< endl; } return 0; }
반응형'알고리즘 > 자료구조' 카테고리의 다른 글
자료구조: Array Table 구현하기 (feat. c++) (0) 2021.05.31 자료구조: Linked List Queue 구현하기 (feat. c++) (0) 2021.05.31 자료구조: Array Hash Table 구현하기 (feat. c++) (0) 2021.05.08 자료구조: Dummy Inorder Linked List 구현하기 (feat. c++) (0) 2021.04.16 자료구조: Inorder Linked List 구현하기 (feat. c++) (0) 2021.04.16