-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement Kqueues using array
More file actions
100 lines (89 loc) · 1.58 KB
/
Implement Kqueues using array
File metadata and controls
100 lines (89 loc) · 1.58 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
#include<iostream>
using namespace std;
class Queue
{
public:
int n;
int k;
int *arr;
int *next;
int *front;
int *rear;
int freespot=0;
int index;
Queue(int n,int k)
{
this->k=k;
this->n=n;
arr=new int[n];
next=new int[n];
for(int i=0;i<n;i++)
{
next[i]=i+1;
}
next[n-1]=-1;
rear=new int[k];
front=new int[k];
for(int i=0;i<k;i++)
{
front[i]=rear[i]=-1;
}
}
void push(int element,int q)
{
if(freespot==-1)
{
cout<<"Overflowed";
return ;
}
index=freespot;
freespot=next[index];
if(front[q-1]==-1)
{
front[q-1]=index;
}
else
{
next[rear[q-1]]=index;
}
next[index]=-1;
arr[index]=element;
rear[q-1]=index;
}
int pop(int q)
{
if(front[q-1]==-1)
{
cout<<"Underflow";
return -1;
}
int index=front[q-1];
front[q-1]=next[index];
next[index]=freespot;
freespot=index;
return arr[index];
}
};
int main()
{
Queue q(10,3);
q.push(5,3);
q.push(4,3);
q.push(2,3);
q.push(1,3);
q.push(9,3);
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
cout<<q.pop(3);
cout<<endl;
}