-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation_of_k_ques_in_one_array.cpp
More file actions
157 lines (126 loc) · 3.25 KB
/
implementation_of_k_ques_in_one_array.cpp
File metadata and controls
157 lines (126 loc) · 3.25 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// A C++ program to demonstrate implementation
// of k queues in a single
// array in time and space efficient way
#include<iostream>
#include<climits>
using namespace std;
// A C++ class to represent k queues
// in a single array of size n
class kQueues
{
// Array of size n to store actual
// content to be stored in queue
int *arr;
// Array of size k to store indexes
// of front elements of the queue
int *front;
// Array of size k to store indexes
// of rear elements of queue
int *rear;
// Array of size n to store next
// entry in all queues
int *next;
int n, k;
int free; // To store the beginning index of the free list
public:
//constructor to create k queue
// in an array of size n
kQueues(int k, int n);
// A utility function to check if
// there is space available
bool isFull() { return (free == -1); }
// To enqueue an item in queue number
// 'qn' where qn is from 0 to k-1
void enqueue(int item, int qn);
// To dequeue an from queue number
// 'qn' where qn is from 0 to k-1
int dequeue(int qn);
// To check whether queue number
// 'qn' is empty or not
bool isEmpty(int qn) { return (front[qn] == -1); }
};
// Constructor to create k queues
// in an array of size n
kQueues::kQueues(int k1, int n1)
{
// Initialize n and k, and allocate
// memory for all arrays
k = k1, n = n1;
arr = new int[n];
front = new int[k];
rear = new int[k];
next = new int[n];
// Initialize all queues as empty
for (int i = 0; i < k; i++)
front[i] = -1;
// Initialize all spaces as free
free = 0;
for (int i=0; i<n-1; i++)
next[i] = i+1;
next[n-1] = -1; // -1 is used to indicate end of free list
}
// To enqueue an item in queue number
// 'qn' where qn is from 0 to k-1
void kQueues::enqueue(int item, int qn)
{
// Overflow check
if (isFull())
{
cout << "\nQueue Overflow\n";
return;
}
int i = free; // Store index of first free slot
// Update index of free slot to index of next slot in free list
free = next[i];
if (isEmpty(qn))
front[qn] = i;
else
next[rear[qn]] = i;
next[i] = -1;
// Update next of rear and then rear for queue number 'qn'
rear[qn] = i;
// Put the item in array
arr[i] = item;
}
// To dequeue an from queue number 'qn' where qn is from 0 to k-1
int kQueues::dequeue(int qn)
{
// Underflow checkSAS
if (isEmpty(qn))
{
cout << "\nQueue Underflow\n";
return INT_MAX;
}
// Find index of front item in queue number 'qn'
int i = front[qn];
// Change top to store next of previous top
front[qn] = next[i];
// Attach the previous front to the
// beginning of free list
next[i] = free;
free = i;
// Return the previous front item
return arr[i];
}
/* Driver program to test kStacks class */
int main()
{
// Let us create 3 queue in an array of size 10
int k = 3, n = 10;
kQueues ks(k, n);
// Let us put some items in queue number 2
ks.enqueue(15, 2);
ks.enqueue(45, 2);
// Let us put some items in queue number 1
ks.enqueue(17, 1);
ks.enqueue(49, 1);
ks.enqueue(39, 1);
// Let us put some items in queue number 0
ks.enqueue(11, 0);
ks.enqueue(9, 0);
ks.enqueue(7, 0);
cout << "Dequeued element from queue 2 is " << ks.dequeue(2) << endl;
cout << "Dequeued element from queue 1 is " << ks.dequeue(1) << endl;
cout << "Dequeued element from queue 0 is " << ks.dequeue(0) << endl;
return 0;
}