FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript-algorithms/src/data-structures/queue/Queue.js at master · trekhleb/javascript-algorithms · GitHub
trekhleb
/
javascript-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
31k
Star
197k
Code
Issues
135
Pull requests
270
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript-algorithms
/
src
/
data-structures
/
queue
/
Queue.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (51 loc) · 1.41 KB
Breadcrumbs
javascript-algorithms
/
src
/
data-structures
/
queue
/
Queue.js
Copy path
File metadata and controls
58 lines (51 loc) · 1.41 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
import
LinkedList
from
'../linked-list/LinkedList'
;
export
default
class
Queue
{
constructor
(
)
{
// We're going to implement Queue based on LinkedList since the two
// structures are quite similar. Namely, they both operate mostly on
// the elements at the beginning and the end. Compare enqueue/dequeue
// operations of Queue with append/deleteHead operations of LinkedList.
this
.
linkedList
=
new
LinkedList
(
)
;
}
/**
*
@return
{
boolean
}
*/
isEmpty
(
)
{
return
!
this
.
linkedList
.
head
;
}
/**
* Read the element at the front of the queue without removing it.
*
@return
{
*
}
*/
peek
(
)
{
if
(
this
.
isEmpty
(
)
)
{
return
null
;
}
return
this
.
linkedList
.
head
.
value
;
}
/**
* Add a new element to the end of the queue (the tail of the linked list).
* This element will be processed after all elements ahead of it.
*
@param
{
*
} value
*/
enqueue
(
value
)
{
this
.
linkedList
.
append
(
value
)
;
}
/**
* Remove the element at the front of the queue (the head of the linked list).
* If the queue is empty, return null.
*
@return
{
*
}
*/
dequeue
(
)
{
const
removedHead
=
this
.
linkedList
.
deleteHead
(
)
;
return
removedHead
?
removedHead
.
value
:
null
;
}
/**
*
@param
[callback]
*
@return
{
string
}
*/
toString
(
callback
)
{
// Return string representation of the queue's linked list.
return
this
.
linkedList
.
toString
(
callback
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL