FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/CircularQueue.js at master · namitmalasi/JavaScript-Interview-Question · GitHub
namitmalasi
/
JavaScript-Interview-Question
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
CircularQueue.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (37 loc) · 862 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
CircularQueue.js
Copy path
File metadata and controls
46 lines (37 loc) · 862 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class
CircularQueue
{
constructor
(
capacity
)
{
this
.
items
=
[
]
;
this
.
head
=
0
;
this
.
tail
=
0
;
this
.
count
=
0
;
this
.
capacity
=
capacity
;
}
isFull
(
)
{
return
this
.
capacity
===
this
.
count
;
}
// adding the element to the queue
enqueue
(
element
)
{
if
(
this
.
isFull
(
)
)
{
return
new
Error
(
"Queue is Full"
)
;
}
this
.
items
[
this
.
tail
]
=
element
;
this
.
tail
=
(
this
.
tail
+
1
)
%
this
.
capacity
;
this
.
count
++
;
}
// removing the element from the queue
dequeue
(
)
{
if
(
this
.
count
===
0
)
return
null
;
let
item
=
this
.
items
[
this
.
head
]
;
this
.
head
=
(
this
.
head
+
1
)
%
this
.
capacity
;
this
.
count
--
;
return
item
;
}
}
const
queue
=
new
CircularQueue
(
5
)
;
queue
.
enqueue
(
10
)
;
queue
.
enqueue
(
20
)
;
queue
.
enqueue
(
30
)
;
queue
.
enqueue
(
40
)
;
queue
.
enqueue
(
50
)
;
queue
.
enqueue
(
60
)
;
console
.
log
(
queue
)
;
Back
|
FazBrowse Home
|
New Git URL