File tree 1 file changed +38
-39
lines changed
1 file changed +38
-39
lines changed Original file line number Diff line number Diff line change 1
- import LinkedList from ' ./LinkedList.js' ;
1
+ import LinkedList from " ./LinkedList.js" ;
2
2
3
3
export default class Queue {
4
- constructor ( maxSize = Infinity ) {
5
- this . queue = new LinkedList ( ) ;
6
- this . maxSize = maxSize ;
7
- this . size = 0 ;
4
+ constructor ( maxSize = Infinity ) {
5
+ this . queue = new LinkedList ( ) ;
6
+ this . maxSize = maxSize ;
7
+ this . size = 0 ;
8
+ }
9
+
10
+ isEmpty ( ) {
11
+ return this . size === 0 ;
12
+ }
13
+
14
+ hasRoom ( ) {
15
+ return this . size < this . maxSize ;
16
+ }
17
+
18
+ enqueue ( data ) {
19
+ if ( this . hasRoom ( ) ) {
20
+ this . queue . addToTail ( data ) ;
21
+ this . size ++ ;
22
+ } else {
23
+ throw new Error ( "Queue is full" ) ;
8
24
}
9
-
10
- isEmpty ( ) {
11
- return this . size === 0 ;
12
- }
13
-
14
- hasRoom ( ) {
15
- return this . size < this . maxSize ;
16
- }
17
-
18
- enqueue ( data ) {
19
- if ( this . hasRoom ( ) ) {
20
- this . queue . addToTail ( data ) ;
21
- this . size ++ ;
22
-
23
- } else {
24
- throw new Error ( "Queue is full" ) ;
25
- }
25
+ }
26
+
27
+ dequeue ( ) {
28
+ if ( ! this . isEmpty ( ) ) {
29
+ const data = this . queue . removeHead ( ) ;
30
+ this . size -- ;
31
+ return data ;
32
+ } else {
33
+ throw new Error ( "Queue is empty" ) ;
26
34
}
35
+ }
27
36
28
- dequeue ( ) {
29
- if ( ! this . isEmpty ( ) ) {
30
- const data = this . queue . removeHead ( ) ;
31
- this . size -- ;
32
- return data ;
33
- } else {
34
- throw new Error ( "Queue is empty" ) ;
35
- }
36
- }
37
+ peek ( ) {
38
+ return ! this . isEmpty ( ) ? this . queue . head . data : null ;
39
+ }
37
40
38
- peek ( ) {
39
- return ! this . isEmpty ( ) ? this . queue . head . data : null ;
40
- }
41
-
42
- print ( ) {
43
- return this . queue . printList ( ) ;
44
- }
45
- }
41
+ print ( ) {
42
+ return this . queue . printList ( ) ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments