1
+ /**
2
+ * Queue reversal
3
+ * @author MadhavBahlMD
4
+ * @date 15/02/2019
5
+ * Method
6
+ * - Dequeue the elements from queue and keep pushing them in stack
7
+ * - Pop the elements from stack and enqueue them in the original queue
8
+ */
9
+
10
+ // Class declaration for queue data structure
11
+ class Queue {
12
+ constructor ( size ) {
13
+ this . capacty = size ;
14
+ this . data = [ ] ;
15
+ this . frontIndex = 0 ;
16
+ this . rearIndex = 0 ;
17
+ }
18
+
19
+ front ( ) {
20
+ return data [ this . frontIndex ] ;
21
+ }
22
+
23
+ rear ( ) {
24
+ return data [ this . rearIndex ] ;
25
+ }
26
+
27
+ enqueue ( element ) {
28
+ if ( this . rearIndex >= this . capacty ) {
29
+ console . log ( "Overflow!" ) ;
30
+ return - 1 ;
31
+ }
32
+ this . data . unshift ( element ) ;
33
+ // console.log (element + ' added to the queue');
34
+ this . rearIndex ++ ;
35
+ }
36
+
37
+ dequeue ( element ) {
38
+ if ( this . rearIndex === 0 ) {
39
+ console . log ( "Underflow!" ) ;
40
+ return - 1 ;
41
+ }
42
+ // console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
43
+ this . rearIndex -- ;
44
+ return this . data . pop ( ) ;
45
+ }
46
+
47
+ displayQueue ( ) {
48
+ let toBeDisplayed = '-> ' ;
49
+ for ( let element of this . data ) {
50
+ toBeDisplayed += element + ' -> ' ;
51
+ }
52
+ console . log ( toBeDisplayed ) ;
53
+ }
54
+ }
55
+
56
+ // Class declaration for stack data structure
57
+ class Stack {
58
+ // constructor to initialize the stack and it's capacity
59
+ constructor ( limit ) {
60
+ this . myStack = [ ] ;
61
+ this . top = limit ;
62
+ }
63
+
64
+ // isEmpty method to check whether the stack is empty
65
+ isEmpty ( ) {
66
+ if ( this . myStack . length === 0 ) return true ;
67
+ else return false ;
68
+ }
69
+
70
+ // isFull method to check whether the stack is full
71
+ isFull ( ) {
72
+ if ( this . myStack . length === this . top ) return true ;
73
+ else return false ;
74
+ }
75
+
76
+ // push method to add a record to the stack
77
+ push ( record ) {
78
+ if ( ! this . isFull ( ) ) {
79
+ console . log ( `Pushing ${ record } to the stack!` ) ;
80
+ this . myStack . push ( record ) ;
81
+ } else {
82
+ console . log ( 'Sorry! The Stack is full!' ) ;
83
+ }
84
+ }
85
+
86
+ // pop method to remove an element from the stack
87
+ pop ( ) {
88
+ if ( ! this . isEmpty ( ) ) {
89
+ console . log ( `Popped element is: ${ this . myStack [ this . myStack . length - 1 ] } ` ) ;
90
+ return this . myStack . pop ( ) ;
91
+ } else {
92
+ console . log ( 'Sorry! The Stack is empty' ) ;
93
+ }
94
+ }
95
+
96
+ // peek method to view the top element
97
+ peek ( ) {
98
+ console . log ( `Current element is: ${ this . myStack [ this . myStack . length - 1 ] } ` ) ;
99
+ }
100
+ }
101
+
102
+ const reverse = ( myQueue ) => {
103
+
104
+ } ;
105
+
106
+ const myQueue = new Queue ( 4 ) ;
107
+
108
+ myQueue . enqueue ( 1 ) ;
109
+ myQueue . enqueue ( 2 ) ;
110
+ myQueue . enqueue ( 3 ) ;
111
+ myQueue . enqueue ( 4 ) ;
112
+
113
+ console . log ( "\n/* ===== Displaying Initial Queue ===== */" ) ;
114
+ myQueue . displayQueue ( ) ;
115
+
116
+
117
+ console . log ( "\n/* ===== Displaying Final Queue ===== */" ) ;
118
+ myQueue . displayQueue ( ) ;
0 commit comments