File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Java script/Data Structures Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class CircularQueue {
2
+ constructor ( size ) {
3
+
4
+ this . queue = [ ] ;
5
+ this . read = 0 ;
6
+ this . write = 0 ;
7
+ this . max = size - 1 ;
8
+ this . count = 0 ;
9
+
10
+ while ( size > 0 ) {
11
+ this . queue . push ( null ) ;
12
+ size -- ;
13
+ }
14
+ }
15
+ checkEmpty ( ) {
16
+ var count = 0 ;
17
+ for ( let i = 0 ; i <= this . max ; i ++ ) {
18
+ if ( this . queue [ i ] == null ) {
19
+ count ++ ;
20
+ }
21
+ }
22
+ if ( count == this . max + 1 ) {
23
+ return true ;
24
+ }
25
+ return false ;
26
+ }
27
+ checkFull ( ) {
28
+ var count = 0 ;
29
+ for ( let i = 0 ; i <= this . max ; i ++ ) {
30
+ if ( this . queue [ i ] == null ) {
31
+ count ++ ;
32
+ }
33
+ }
34
+ if ( count > 0 ) {
35
+ return false ;
36
+ }
37
+ return true ;
38
+ }
39
+
40
+ print ( ) {
41
+ return this . queue ;
42
+ }
43
+
44
+ enqueue ( item ) {
45
+ if ( ! this . checkFull ( ) ) {
46
+ this . queue [ this . write ] = item ;
47
+ this . write = ( this . write + 1 ) % ( this . max + 1 ) ;
48
+ }
49
+ }
50
+
51
+
52
+ dequeue ( ) {
53
+ if ( ! this . checkEmpty ( ) ) {
54
+ let val = this . queue [ this . read ] ;
55
+ this . queue [ this . read ] = null ;
56
+ this . read = ( this . read + 1 ) % ( this . max + 1 ) ;
57
+ return val ;
58
+ }
59
+ else return null ;
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments