1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<cstdio> #include<stdlib.h> #define INF -987654321 using namespace std;
class Queue { private: int max_size = 11; int *arr; int front = 0; int rear = 0;
void allocate() { arr = (int *)malloc(sizeof(int) * max_size); }
public: Queue() { allocate(); } Queue(int size) { max_size = size + 1; allocate(); }
bool isEmpty() { return front == rear ? true : false; }
bool isFull() { return front == (rear + 1) % max_size ? true : false; }
void push(int a) { if (!isFull()) { arr[rear] = a; rear = (rear + 1) % max_size; } else { printf("Queue is Fulled\n"); } } int pop() { if (!isEmpty()) { int item = arr[front]; front = (front + 1) % max_size; return item; } else { return -INF; } } };
void main() { Queue q1;
int push[20] = { 12, 13, 15, 16, 201, 356, 23, 76, 45, 34, 23, 23, 35, 23, 35, 46, 78, 89, 90, 21 };
int index = 0;
while (index != 20) { while (!q1.isFull()) { q1.push(push[index]); index++; } while (!q1.isEmpty()) { printf("%d ", q1.pop()); } } }
|