|
1 | 1 | #include <iostream> |
| 2 | +#include <stdlib.h> |
2 | 3 |
|
3 | 4 | using namespace std; |
4 | 5 |
|
5 | | -class Queue_Circular{ |
6 | | - |
7 | | -private: |
8 | | -int *data, f, r, size; |
9 | | -public: |
10 | | -Queue_Circular(int s = 10){ |
11 | | -data = new int[s+1]; |
12 | | -f = r = 0; |
13 | | -size = s+1; |
14 | | -} |
15 | | - |
16 | | -~Queue_Circular(){ |
17 | | -free(data); |
18 | | -} |
19 | | - |
20 | | -int is_empty(){ |
21 | | -return f == r; |
22 | | -} |
23 | | - |
24 | | -int is_full(){ |
25 | | -return (r+1)%size == f; |
26 | | -} |
27 | | - |
28 | | -int enqueue(int value){ |
29 | | -if( is_full() ) return -1; |
30 | | -data[r] = value; |
31 | | -r = (r+1)%size; |
32 | | -} |
33 | | - |
34 | | -int dequeue(){ |
35 | | -if( is_empty() ) return -1; |
36 | | -int temp = data[f]; |
37 | | -f = (f+1)%size; |
38 | | -return temp; |
39 | | -} |
40 | | - |
41 | | -int front(){ |
42 | | -if( is_empty() ) return -1; |
43 | | -return data[f]; |
44 | | -} |
45 | | - |
46 | | -int rear(){ |
47 | | -if( is_empty() ) return -1; |
48 | | -return data[(r+size-1)%size]; |
49 | | -} |
50 | | - |
51 | | -void display(){ |
52 | | -if( ! is_empty() ){ |
53 | | -for(int i=f; i!=r; i = (i+1)%size) cout<<data[i] <<", "; |
54 | | -} |
55 | | -} |
| 6 | +template<class DataType> |
| 7 | +class Queue_Circular { |
| 8 | +private: |
| 9 | + DataType *data; |
| 10 | + int frontPtr, rearPtr, size; |
| 11 | + |
| 12 | +public: |
| 13 | + |
| 14 | + Queue_Circular(int size = 10) { |
| 15 | + data = new DataType[size + 1]; |
| 16 | + frontPtr = rearPtr = 0; |
| 17 | + this->size = size + 1; |
| 18 | + } |
| 19 | + |
| 20 | + ~Queue_Circular(){ |
| 21 | + free(data); |
| 22 | + } |
| 23 | + |
| 24 | + bool is_empty(){ |
| 25 | + return frontPtr == rearPtr; |
| 26 | + } |
| 27 | + |
| 28 | + bool is_full(){ |
| 29 | + return (rearPtr + 1) % size == frontPtr; |
| 30 | + } |
| 31 | + |
| 32 | + void enqueue(DataType value){ |
| 33 | + data[rearPtr] = value; |
| 34 | + rearPtr = (rearPtr + 1) % size; |
| 35 | + } |
| 36 | + |
| 37 | + void dequeue() { |
| 38 | + frontPtr = (frontPtr + 1) % size; |
| 39 | + } |
| 40 | + |
| 41 | + DataType front(){ |
| 42 | + if( is_empty() ) |
| 43 | + return -1; |
| 44 | + return data[frontPtr]; |
| 45 | + } |
| 46 | + |
| 47 | + DataType rear() { |
| 48 | + if( is_empty() ) |
| 49 | + return -1; |
| 50 | + return data[(rearPtr + size - 1) % size]; |
| 51 | + } |
| 52 | + |
| 53 | + void display(){ |
| 54 | + if( ! is_empty() ){ |
| 55 | + for(int i = frontPtr; i != rearPtr; i = (i + 1) % size) |
| 56 | + cout <<data[i] <<", "; |
| 57 | + } |
| 58 | + } |
| 59 | + |
56 | 60 | }; |
57 | 61 |
|
58 | 62 |
|
59 | 63 | int main(){ |
60 | 64 |
|
61 | | -Queue_Circular Q1(5); |
| 65 | + srand(time(NULL)); |
62 | 66 |
|
63 | | -for(int i=1; ! Q1.is_full(); i++){ |
64 | | -cout<<"\nEnqueue " <<(i*10); |
65 | | -Q1.enqueue(i*10); |
66 | | -} |
| 67 | + Queue_Circular<int> Q1(5); |
| 68 | + int temp; |
| 69 | + cout <<"\nEnqueue random values until queue is full :"; |
| 70 | + while( ! Q1.is_full() ) { |
| 71 | + temp = random() % 100 + 1; |
| 72 | + cout<<"\nEnqueue : " <<temp; |
| 73 | + Q1.enqueue(temp); |
| 74 | + } |
67 | 75 |
|
68 | | -cout<<"\n\nQueue : "; |
69 | | -Q1.display(); |
| 76 | + cout<<"\n\nQueue : "; |
| 77 | + Q1.display(); |
70 | 78 |
|
71 | | -cout<<"\n\nDequeue = " <<Q1.dequeue(); |
| 79 | + cout<<"\n\nDequeue = " <<Q1.front(); |
| 80 | + Q1.dequeue(); |
72 | 81 |
|
73 | | -cout<<"\nEnqueue " <<(100); |
74 | | -Q1.enqueue(100); |
| 82 | + temp = random() % 100 + 1; |
| 83 | + cout<<"\n\nEnqueue = " <<temp; |
| 84 | + Q1.enqueue(temp); |
75 | 85 |
|
76 | | -cout<<"\n\nQueue : "; |
77 | | -Q1.display(); |
| 86 | + cout<<"\n\nQueue : "; |
| 87 | + Q1.display(); |
78 | 88 |
|
79 | | -cout<<"\n\nFront = " <<Q1.front() <<" Rear = " <<Q1.rear(); |
| 89 | + cout<<"\n\nFront = " <<Q1.front() <<" Rear = " <<Q1.rear(); |
80 | 90 |
|
81 | | -cout<<endl; |
82 | | -return 0; |
| 91 | + cout<<endl; |
| 92 | + return 0; |
83 | 93 | } |
0 commit comments