-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from technicalreju/main
Circular_Queue.cpp
- Loading branch information
Showing
1 changed file
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Queue | ||
{ | ||
|
||
int rear, front; | ||
|
||
|
||
int size; | ||
int *arr; | ||
|
||
Queue(int s) | ||
{ | ||
front = rear = -1; | ||
size = s; | ||
arr = new int[s]; | ||
} | ||
|
||
void enQueue(int value); | ||
int deQueue(); | ||
void displayQueue(); | ||
}; | ||
|
||
|
||
void Queue::enQueue(int value) | ||
{ | ||
if ((front == 0 && rear == size-1) || | ||
(rear == (front-1)%(size-1))) | ||
{ | ||
printf("\nQueue is Full"); | ||
return; | ||
} | ||
|
||
else if (front == -1) /* Insert First Element */ | ||
{ | ||
front = rear = 0; | ||
arr[rear] = value; | ||
} | ||
|
||
else if (rear == size-1 && front != 0) | ||
{ | ||
rear = 0; | ||
arr[rear] = value; | ||
} | ||
|
||
else | ||
{ | ||
rear++; | ||
arr[rear] = value; | ||
} | ||
} | ||
|
||
|
||
int Queue::deQueue() | ||
{ | ||
if (front == -1) | ||
{ | ||
printf("\nQueue is Empty"); | ||
return INT_MIN; | ||
} | ||
|
||
int data = arr[front]; | ||
arr[front] = -1; | ||
if (front == rear) | ||
{ | ||
front = -1; | ||
rear = -1; | ||
} | ||
else if (front == size-1) | ||
front = 0; | ||
else | ||
front++; | ||
|
||
return data; | ||
} | ||
|
||
|
||
void Queue::displayQueue() | ||
{ | ||
if (front == -1) | ||
{ | ||
printf("\nQueue is Empty"); | ||
return; | ||
} | ||
printf("\nElements in Circular Queue are: "); | ||
if (rear >= front) | ||
{ | ||
for (int i = front; i <= rear; i++) | ||
printf("%d ",arr[i]); | ||
} | ||
else | ||
{ | ||
for (int i = front; i < size; i++) | ||
printf("%d ", arr[i]); | ||
|
||
for (int i = 0; i <= rear; i++) | ||
printf("%d ", arr[i]); | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
Queue q(5); | ||
|
||
|
||
q.enQueue(14); | ||
q.enQueue(22); | ||
q.enQueue(13); | ||
q.enQueue(-6); | ||
|
||
|
||
q.displayQueue(); | ||
|
||
|
||
printf("\nDeleted value = %d", q.deQueue()); | ||
printf("\nDeleted value = %d", q.deQueue()); | ||
|
||
q.displayQueue(); | ||
|
||
q.enQueue(9); | ||
q.enQueue(20); | ||
q.enQueue(5); | ||
|
||
q.displayQueue(); | ||
|
||
q.enQueue(20); | ||
return 0; | ||
} |