|
| 1 | +// |
| 2 | +// file: cirque.h |
| 3 | + |
| 4 | +// This header file contains a circula queue ADT with maxlen option of the queue. |
| 5 | +// Things to add: Template |
| 6 | +// |
| 7 | + |
| 8 | +#ifndef CIRQUE_H |
| 9 | +#define CIRQUE_H |
| 10 | +#include <iostream> |
| 11 | +#include <sstream> |
| 12 | +#include <string> |
| 13 | +#include <cassert> |
| 14 | + |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +struct CircularQueue { |
| 18 | + string *items; // queue item storage |
| 19 | + int front, back; // set to -1 to begin with |
| 20 | + int maxlen; // maxlen: total capacity of the queue, FIFO but no overflow |
| 21 | + string dash; // dash - a place holder string |
| 22 | + int shown; // the max number of items to show |
| 23 | + |
| 24 | + CircularQueue(int capa=4, int nitems=20) { |
| 25 | + items = new string[capa]; |
| 26 | + dash = "-"; // a placeholder, empty slot available |
| 27 | + for (int i = 0; i < capa; i++) items[i] = dash; // marked |
| 28 | + front = -1; |
| 29 | + back = -1; |
| 30 | + maxlen = capa; // maxlen, total capacity of the cque |
| 31 | + shown = nitems; // max number of items to show |
| 32 | + } |
| 33 | + ~CircularQueue() { |
| 34 | + delete[] items; |
| 35 | + cout << "\tdestructor ends^^" << endl; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +using cirque = CircularQueue *; |
| 40 | + |
| 41 | +bool full(cirque q); // returns true when the q is full |
| 42 | +bool empty(cirque q); // returns false when the q is empty |
| 43 | +int size(cirque q); // returns the size of q using front/back/capa |
| 44 | +void resize(cirque q, int new_capa); // resize the queue items[] by resetting the maxlen |
| 45 | +void enqueue(cirque q, string item); // enqueue one item |
| 46 | +string dequeue(cirque q); // dequeue one item |
| 47 | +void clear(cirque q); // q->items are cleared, no change in capa or maxlen |
| 48 | +void show_qstat(cirque q); // show the queue status |
| 49 | +void show_queue(cirque q); // show queue from 'front' to 'back' |
| 50 | +void show_items(cirque q); // show queue items[] as it is stored |
| 51 | +#endif |
0 commit comments