Skip to content

Commit d763fb1

Browse files
authored
A circular queue util to parse json data
1 parent a3e831f commit d763fb1

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// A custom implemenatation of circular queue to parse json data
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
class Queue
7+
{
8+
// Initialize front and rear
9+
int rear, front;
10+
11+
// Circular Queue
12+
int size;
13+
int *arr;
14+
public:
15+
Queue(int s)
16+
{
17+
front = rear = -1;
18+
size = s;
19+
arr = new int[s];
20+
}
21+
22+
void enQueue(int value);
23+
int deQueue();
24+
void displayQueue();
25+
};
26+
27+
28+
/* Function to create Circular queue */
29+
void Queue::enQueue(int value)
30+
{
31+
if ((front == 0 && rear == size-1) ||
32+
(rear == (front-1)%(size-1)))
33+
{
34+
printf("\nQueue is Full");
35+
return;
36+
}
37+
38+
else if (front == -1) /* Insert First Element */
39+
{
40+
front = rear = 0;
41+
arr[rear] = value;
42+
}
43+
44+
else if (rear == size-1 && front != 0)
45+
{
46+
rear = 0;
47+
arr[rear] = value;
48+
}
49+
50+
else
51+
{
52+
rear++;
53+
arr[rear] = value;
54+
}
55+
}
56+
57+
// Function to delete element from Circular Queue
58+
int Queue::deQueue()
59+
{
60+
if (front == -1)
61+
{
62+
printf("\nQueue is Empty");
63+
return INT_MIN;
64+
}
65+
66+
int data = arr[front];
67+
arr[front] = -1;
68+
if (front == rear)
69+
{
70+
front = -1;
71+
rear = -1;
72+
}
73+
else if (front == size-1)
74+
front = 0;
75+
else
76+
front++;
77+
78+
return data;
79+
}
80+
81+
// Function displaying the elements
82+
// of Circular Queue
83+
void Queue::displayQueue()
84+
{
85+
if (front == -1)
86+
{
87+
printf("\nQueue is Empty");
88+
return;
89+
}
90+
printf("\nElements in Circular Queue are: ");
91+
if (rear >= front)
92+
{
93+
for (int i = front; i <= rear; i++)
94+
printf("%d ",arr[i]);
95+
}
96+
else
97+
{
98+
for (int i = front; i < size; i++)
99+
printf("%d ", arr[i]);
100+
101+
for (int i = 0; i <= rear; i++)
102+
printf("%d ", arr[i]);
103+
}
104+
}
105+
106+
/* Driver of the program */
107+
int main()
108+
{
109+
return 0;
110+
}

0 commit comments

Comments
 (0)