FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript/data_structures/queue/array_queue.ts at master · bigjohncodes/TypeScript · GitHub
bigjohncodes
/
TypeScript
Public
forked from
TheAlgorithms/TypeScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeScript
/
data_structures
/
queue
/
array_queue.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (57 loc) · 1.44 KB
Breadcrumbs
TypeScript
/
data_structures
/
queue
/
array_queue.ts
Copy path
File metadata and controls
64 lines (57 loc) · 1.44 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
/**
* This is an array-based implementation of a Queue.
* A Queue is a data structure that follows the FIFO (First In First Out) principle.
* It means that the first element that was added to the queue will be the first one to be removed.
* The time complexity of the operations is O(n).
*/
import
{
Queue
}
from
'./queue'
export
class
ArrayQueue
<
T
>
implements
Queue
<
T
>
{
private
queue
:
T
[
]
=
[
]
/**
* Returns the number of items in the queue.
*
*
@returns
{
number
} The number of items in the queue.
*/
length
(
)
:
number
{
return
this
.
queue
.
length
}
/**
* Checks if the queue is empty.
*
*
@returns
{
boolean
} Whether the queue is empty or not.
*/
isEmpty
(
)
:
boolean
{
return
this
.
queue
.
length
===
0
}
/**
* Adds an item to the queue.
*
*
@param
item The item being added to the queue.
*/
enqueue
(
item
:
T
)
:
void
{
this
.
queue
.
push
(
item
)
}
/**
* Removes an item from the queue and returns it.
*
*
@throws
Queue Underflow if the queue is empty.
*
@returns
The item that was removed from the queue.
*/
dequeue
(
)
:
T
{
if
(
this
.
isEmpty
(
)
)
{
throw
new
Error
(
'Queue Underflow'
)
}
return
this
.
queue
.
shift
(
)
as
T
}
/**
* Returns the item at the front of the queue.
*
*
@returns
The item at the front of the queue or null if the queue is empty.
*/
peek
(
)
:
T
|
null
{
if
(
this
.
isEmpty
(
)
)
{
return
null
}
return
this
.
queue
[
0
]
}
}
Back
|
FazBrowse Home
|
New Git URL