File tree 2 files changed +41
-11
lines changed
2 files changed +41
-11
lines changed Original file line number Diff line number Diff line change @@ -6,29 +6,24 @@ class Queue {
6
6
this . arrSize = parseInt ( fullSize ) ;
7
7
}
8
8
enqueue ( item ) {
9
- if ( this . rear === this . arrSize - 1 ) {
10
- console . log ( 'array full' ) ;
9
+ if ( this . rear === this . arrSize - 1 ) { // true => full queue
11
10
return ;
12
11
}
13
- if ( this . front = - 1 ) { this . front = 0 } ;
12
+ if ( this . front = - 1 ) { this . front = 0 } ; // enqueue action
14
13
this . rear += 1 ;
15
14
this . queue [ this . rear ] = item ;
16
- console . log ( this . queue , this . front , this . rear ) ;
17
15
}
18
16
dequeue ( ) {
19
- if ( this . arrSize === this . front || this . front === - 1 ) {
20
-
21
- console . log ( 'the queue is empty' , this . queue ) ;
17
+ if ( this . arrSize === this . front || this . front === - 1 ) { // true => empty queue
22
18
return ;
23
19
}
24
- this . queue [ this . front ] = null ;
25
- if ( this . front >= this . rear ) {
20
+ this . queue [ this . front ] = null ; // dequeue action
21
+ if ( this . front >= this . rear ) { // if empty => reset
26
22
this . rear = - 1 ;
27
23
this . front = - 1 ;
28
- } else {
24
+ } else { // else increase the head or front
29
25
this . front += 1
30
26
}
31
- console . log ( this . queue , this . front , this . rear ) ;
32
27
}
33
28
}
34
29
const newQueue = new Queue ( 5 ) ;
Original file line number Diff line number Diff line change
1
+ // stack data structure
2
+
3
+ class Stack {
4
+ constructor ( maxSize ) {
5
+ this . stack = [ ] ;
6
+ this . maxSize = maxSize ;
7
+ }
8
+ pushing ( item ) {
9
+ if ( this . _isFull ( ) ) {
10
+ console . log ( 'stack is full' ) ;
11
+ return - 1 ;
12
+ }
13
+ return this . stack . push ( item ) ;
14
+ }
15
+ _isEmpty ( ) {
16
+ return this . stack . length <= 0 ;
17
+ }
18
+ _isFull ( ) {
19
+ return this . stack . length >= this . maxSize ;
20
+ }
21
+ peek ( ) {
22
+ return this . stack [ this . stack . length - 1 ] ;
23
+ }
24
+ poping ( ) {
25
+ if ( this . _isEmpty ( ) ) {
26
+ console . log ( 'stack is empty' ) ;
27
+ return - 1 ;
28
+ }
29
+ return this . stack . pop ( ) ;
30
+ }
31
+ }
32
+ const stack = new Stack ( 5 ) ;
33
+ stack . pushing ( 6 ) ;
34
+ stack . peek ( ) ;
35
+ stack . poping ( ) ;
You can’t perform that action at this time.
0 commit comments