File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ export default class Queue {
2
+ constructor ( ) {
3
+ this . queue = [ ] ;
4
+ this . front = null ;
5
+ this . rear = null ;
6
+ }
7
+
8
+ enqueue ( element ) {
9
+ this . queue . push ( element ) ;
10
+ this . rear = element ;
11
+ this . front = this . queue [ 0 ] ;
12
+ }
13
+
14
+ dequeue ( ) {
15
+ if ( this . queue . length > 0 ) {
16
+ if ( this . queue . length === 1 ) {
17
+ this . front = null ;
18
+ } else {
19
+ this . front = this . queue [ this . queue . length - 2 ] ;
20
+ }
21
+ return this . queue . shift ( ) ;
22
+ } else {
23
+ throw new Error ( "Queue Underflow Exception: The queue is empty." ) ;
24
+ }
25
+ }
26
+
27
+ getFront ( ) {
28
+ if ( this . queue . length === 0 ) {
29
+ return null ;
30
+ }
31
+
32
+ return this . front ;
33
+ }
34
+
35
+ getRear ( ) {
36
+ if ( this . queue . length === 0 ) {
37
+ return null ;
38
+ }
39
+
40
+ return this . Rear ;
41
+ }
42
+
43
+ size ( ) {
44
+ return this . queue . length ;
45
+ }
46
+
47
+ isEmpty ( ) {
48
+ return this . queue . length === 0 ;
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments