FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Data-Structures/Queue/Queue.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Data-Structures
/
Queue
/
Queue.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
114 lines (92 loc) · 2.08 KB
Breadcrumbs
JavaScript
/
Data-Structures
/
Queue
/
Queue.js
Copy path
File metadata and controls
114 lines (92 loc) · 2.08 KB
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
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
/* Queue
* A Queue is a data structure that allows you to add an element to the end of
* a list and remove the item at the front. A queue follows a FIFO (First In First Out)
* system, where the first item to enter the queue is the first to be removed,
* All these operation complexities are O(1).
* This implementation following the linked list structure.
*/
class
Queue
{
#size
constructor
(
)
{
this
.
head
=
null
this
.
tail
=
null
this
.
#size
=
0
return
Object
.
seal
(
this
)
}
get
length
(
)
{
return
this
.
#size
}
/**
*
@description
- Add a value to the end of the queue
*
@param
{
*
} data
*
@returns
{
number
} - The current size of queue
*/
enqueue
(
data
)
{
const
node
=
{
data
,
next
:
null
}
if
(
!
this
.
head
&&
!
this
.
tail
)
{
this
.
head
=
node
this
.
tail
=
node
}
else
{
this
.
tail
.
next
=
node
this
.
tail
=
node
}
return
++
this
.
#size
}
/**
*
@description
- Removes the value at the front of the queue
*
@returns
{
*
} - The first data of the queue
*/
dequeue
(
)
{
if
(
this
.
isEmpty
(
)
)
{
throw
new
Error
(
'Queue is Empty'
)
}
const
firstData
=
this
.
peekFirst
(
)
this
.
head
=
this
.
head
.
next
if
(
!
this
.
head
)
{
this
.
tail
=
null
}
this
.
#size
--
return
firstData
}
/**
*
@description
- Return the item at the front of the queue
*
@returns
{
*
}
*/
peekFirst
(
)
{
if
(
this
.
isEmpty
(
)
)
{
throw
new
Error
(
'Queue is Empty'
)
}
return
this
.
head
.
data
}
/**
*
@description
- Return the item at the tail of the queue
*
@returns
{
*
}
*/
peekLast
(
)
{
if
(
this
.
isEmpty
(
)
)
{
throw
new
Error
(
'Queue is Empty'
)
}
return
this
.
tail
.
data
}
/**
*
@description
- Return the array of Queue
*
@returns
{
Array<*>
}
*/
toArray
(
)
{
const
array
=
[
]
let
node
=
this
.
head
while
(
node
)
{
array
.
push
(
node
.
data
)
node
=
node
.
next
}
return
array
}
/**
*
@description
- Return is queue empty or not
*
@returns
{
boolean
}
*/
isEmpty
(
)
{
return
this
.
length
===
0
}
}
export
default
Queue
Back
|
FazBrowse Home
|
New Git URL