FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript-Algorithms/data_structures/queue/stack_queue.ts at master · bitsnaps/TypeScript-Algorithms · GitHub
bitsnaps
/
TypeScript-Algorithms
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-Algorithms
/
data_structures
/
queue
/
stack_queue.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (79 loc) · 2.45 KB
Breadcrumbs
TypeScript-Algorithms
/
data_structures
/
queue
/
stack_queue.ts
Copy path
File metadata and controls
91 lines (79 loc) · 2.45 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
/**
* A Stack Based Queue Implementation.
* The Queue data structure which follows the FIFO (First in First Out) rule.
* The dequeue operation in a normal stack based queue would be o(n), as the entire has to be shifted
* With the help of two stacks, the time complexity of this can be brought down to amortized-O(1).
* Here, one stack acts as an Enqueue stack where elements are added.
* The other stack acts as a dequeue stack which helps in dequeuing the elements
*/
import
{
Stack
}
from
'../stack/stack'
import
{
Queue
}
from
'./queue'
export
class
StackQueue
<
T
>
implements
Queue
<
T
>
{
private
enqueueStack
:
Stack
<
T
>
=
new
Stack
<
T
>
(
)
private
dequeueStack
:
Stack
<
T
>
=
new
Stack
<
T
>
(
)
/**
* Returns the length of the Queue
*
*
@returns
{
number
} the length of the Queue
*/
length
(
)
:
number
{
return
this
.
enqueueStack
.
length
(
)
+
this
.
dequeueStack
.
length
(
)
}
/**
* Checks if the queue is empty.
*
*
@returns
{
boolean
} Whether the queue is empty or not.
*/
isEmpty
(
)
:
boolean
{
return
this
.
enqueueStack
.
isEmpty
(
)
&&
this
.
dequeueStack
.
isEmpty
(
)
}
/**
* Adds an item to the queue.
* We always add a new item to the enqueueStack.
*
@param
item The item being added to the queue.
*/
enqueue
(
item
:
T
)
:
void
{
this
.
enqueueStack
.
push
(
item
)
}
/**
* Shifts the elements from the enqueueStack to the dequeueStack
* In the worst case, all the elements from the enqueue stack needs to shifted, which needs O(n) time.
* However, after the shift, elements can de dequeued at O(1).
* This helps in dequeuing the elements in amortized O(1) time.
*/
private
shift
(
)
:
void
{
while
(
!
this
.
enqueueStack
.
isEmpty
(
)
)
{
const
enqueueStackTop
=
this
.
enqueueStack
.
pop
(
)
this
.
dequeueStack
.
push
(
enqueueStackTop
)
}
}
/**
* 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'
)
}
if
(
this
.
dequeueStack
.
isEmpty
(
)
)
{
this
.
shift
(
)
}
return
this
.
dequeueStack
.
pop
(
)
}
/**
* 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
}
if
(
this
.
dequeueStack
.
isEmpty
(
)
)
{
this
.
shift
(
)
}
return
this
.
dequeueStack
.
top
(
)
}
}
Back
|
FazBrowse Home
|
New Git URL