File tree Expand file tree Collapse file tree 2 files changed +41
-11
lines changed
Expand file tree Collapse file tree 2 files changed +41
-11
lines changed Original file line number Diff line number Diff line change @@ -6,29 +6,24 @@ class Queue {
66 this . arrSize = parseInt ( fullSize ) ;
77 }
88 enqueue ( item ) {
9- if ( this . rear === this . arrSize - 1 ) {
10- console . log ( 'array full' ) ;
9+ if ( this . rear === this . arrSize - 1 ) { // true => full queue
1110 return ;
1211 }
13- if ( this . front = - 1 ) { this . front = 0 } ;
12+ if ( this . front = - 1 ) { this . front = 0 } ; // enqueue action
1413 this . rear += 1 ;
1514 this . queue [ this . rear ] = item ;
16- console . log ( this . queue , this . front , this . rear ) ;
1715 }
1816 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
2218 return ;
2319 }
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
2622 this . rear = - 1 ;
2723 this . front = - 1 ;
28- } else {
24+ } else { // else increase the head or front
2925 this . front += 1
3026 }
31- console . log ( this . queue , this . front , this . rear ) ;
3227 }
3328}
3429const 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