|
| 1 | +/* |
| 2 | + Author: Sachin19k |
| 3 | +*/ |
| 4 | + |
| 5 | +/* Problem Statement : |
| 6 | +Implement Circular Queue using Array. Perform following operations on it. |
| 7 | +a) Insertion (Enqueue) |
| 8 | +b) Deletion (Dequeue) |
| 9 | +c) Display |
| 10 | +(Note: Handle queue full condition by considering a fixed size of a queue.) */ |
| 11 | + |
| 12 | +#include<iostream> |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +// Opetations on Queue required |
| 16 | +struct Queue |
| 17 | +{ |
| 18 | +private: |
| 19 | + int rear,front,size; |
| 20 | +public: |
| 21 | + Queue(int n) |
| 22 | + { |
| 23 | + size=n; |
| 24 | + front=-1; |
| 25 | + rear=-1; |
| 26 | + } |
| 27 | + void enqueue(int a[]); |
| 28 | + void dequeue(int a[]); |
| 29 | + void show(int a[]); |
| 30 | +}; |
| 31 | + |
| 32 | +void Queue::enqueue(int a[]) //INSERTION IN QUEUE |
| 33 | +{ |
| 34 | + if((rear+1)%size == front) |
| 35 | + { |
| 36 | + cout<<"\n QUEUE FULL!!\n"; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + int x; |
| 41 | + cout<<"\n ENTER THE ELEMENT TO BE INSERTED: "; |
| 42 | + cin>>x; |
| 43 | + if(rear ==-1) |
| 44 | + { |
| 45 | + front=0; |
| 46 | + rear=0; |
| 47 | + |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + rear=(rear+1)%size; |
| 52 | + } |
| 53 | + a[rear]=x; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void Queue::dequeue(int a[]) //DELETION FROM QUEUE |
| 58 | +{ |
| 59 | + if(rear == -1 && front == -1) |
| 60 | + { |
| 61 | + cout<<"\n EMPTY QUEUE! (nothing to delete) \n "; |
| 62 | + return; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + cout<<"Deleted element from queue is "<<a[front]<<"\n"; |
| 67 | + if(rear == front) |
| 68 | + { |
| 69 | + rear= -1; |
| 70 | + front= -1; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + front=(front+1)%size; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void Queue::show(int a[]) //DISPLAY OF ELEMENTS |
| 80 | +{ |
| 81 | + if(front == -1) |
| 82 | + { |
| 83 | + cout<<"\n QUEUE IS EMPTY! (nothing to show )) \n"; |
| 84 | + return; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + int x=front; |
| 89 | + while(x != rear) |
| 90 | + { |
| 91 | + cout<<a[x]<<"\t"; |
| 92 | + x=(x+1)%size; |
| 93 | + } |
| 94 | + cout<<a[rear]<<"\t"; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +int main() //main function |
| 99 | +{ |
| 100 | + int n,ch; |
| 101 | + cout<<"Enter the size of the array: "; |
| 102 | + cin>>n; |
| 103 | + int a[n]; |
| 104 | + Queue s(n); |
| 105 | + cout<<" \n [1] INSERTION "; |
| 106 | + cout<<" \n [2] DELETION "; |
| 107 | + cout<<" \n [3] DISPLAY \n"; |
| 108 | + do |
| 109 | + { |
| 110 | + cout<<" \n *****Enter your choice***** (want to exit? enter 0) \n"; |
| 111 | + cin>>ch; |
| 112 | + switch(ch) |
| 113 | + { |
| 114 | + case 1: |
| 115 | + { |
| 116 | + s.enqueue(a); |
| 117 | + break; |
| 118 | + } |
| 119 | + case 2: |
| 120 | + { |
| 121 | + s.dequeue(a); |
| 122 | + break; |
| 123 | + } |
| 124 | + case 3: |
| 125 | + { |
| 126 | + s.show(a); |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + while(ch>0 && ch<4); |
| 132 | + cout<<"\n \t OPERATION COMPLETED!! THANK YOU :) \t \n"; |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +/* |
| 137 | +-------------------------------------------------------------------------------------------------------------------------- |
| 138 | +OUTPUT : |
| 139 | +
|
| 140 | + Enter the size of the array: 6 |
| 141 | + |
| 142 | + [1] INSERTION |
| 143 | + [2] DELETION |
| 144 | + [3] DISPLAY |
| 145 | + |
| 146 | + *****Enter your choice***** (want to exit? enter 0) |
| 147 | +1 |
| 148 | +
|
| 149 | + ENTER THE ELEMENT TO BE INSERTED: 2 |
| 150 | + |
| 151 | + *****Enter your choice***** (want to exit? enter 0) |
| 152 | +1 |
| 153 | +
|
| 154 | + ENTER THE ELEMENT TO BE INSERTED: 6 |
| 155 | + |
| 156 | + *****Enter your choice***** (want to exit? enter 0) |
| 157 | +1 |
| 158 | +
|
| 159 | + ENTER THE ELEMENT TO BE INSERTED: 8 |
| 160 | + |
| 161 | + *****Enter your choice***** (want to exit? enter 0) |
| 162 | +1 |
| 163 | +
|
| 164 | + ENTER THE ELEMENT TO BE INSERTED: 9 |
| 165 | + |
| 166 | + *****Enter your choice***** (want to exit? enter 0) |
| 167 | +1 |
| 168 | +
|
| 169 | + ENTER THE ELEMENT TO BE INSERTED: 22 |
| 170 | + |
| 171 | + *****Enter your choice***** (want to exit? enter 0) |
| 172 | +3 |
| 173 | +2 6 8 9 22 |
| 174 | + *****Enter your choice***** (want to exit? enter 0) |
| 175 | +1 |
| 176 | +
|
| 177 | + ENTER THE ELEMENT TO BE INSERTED: 7 |
| 178 | + |
| 179 | + *****Enter your choice***** (want to exit? enter 0) |
| 180 | +1 |
| 181 | +
|
| 182 | + QUEUE FULL!! |
| 183 | + |
| 184 | + *****Enter your choice***** (want to exit? enter 0) |
| 185 | +2 |
| 186 | +Deleted element from queue is 2 |
| 187 | + |
| 188 | + *****Enter your choice***** (want to exit? enter 0) |
| 189 | +2 |
| 190 | +Deleted element from queue is 6 |
| 191 | + |
| 192 | + *****Enter your choice***** (want to exit? enter 0) |
| 193 | +3 |
| 194 | +8 9 22 7 |
| 195 | + *****Enter your choice***** (want to exit? enter 0) |
| 196 | +0 |
| 197 | +
|
| 198 | + OPERATION COMPLETED!! THANK YOU :) |
| 199 | +
|
| 200 | +
|
| 201 | +...Program finished with exit code 0 |
| 202 | +Press ENTER to exit console. |
| 203 | +*/ |
0 commit comments