@@ -21,7 +21,7 @@ class Queue {
2121 }
2222
2323 rear ( ) {
24- return data [ this . rearIndex ] ;
24+ return data [ this . rearIndex - 1 ] ;
2525 }
2626
2727 enqueue ( element ) {
@@ -76,7 +76,6 @@ class Stack {
7676 // push method to add a record to the stack
7777 push ( record ) {
7878 if ( ! this . isFull ( ) ) {
79- console . log ( `Pushing ${ record } to the stack!` ) ;
8079 this . myStack . push ( record ) ;
8180 } else {
8281 console . log ( 'Sorry! The Stack is full!' ) ;
@@ -86,7 +85,6 @@ class Stack {
8685 // pop method to remove an element from the stack
8786 pop ( ) {
8887 if ( ! this . isEmpty ( ) ) {
89- console . log ( `Popped element is: ${ this . myStack [ this . myStack . length - 1 ] } ` ) ;
9088 return this . myStack . pop ( ) ;
9189 } else {
9290 console . log ( 'Sorry! The Stack is empty' ) ;
@@ -100,7 +98,18 @@ class Stack {
10098}
10199
102100const reverse = ( myQueue ) => {
101+ const stack = new Stack ( 10 ) ;
102+ let len = myQueue . rearIndex ;
103103
104+ for ( let i = len ; i > 0 ; i -- ) {
105+ let currentElement = myQueue . dequeue ( ) ;
106+ stack . push ( currentElement ) ;
107+ }
108+
109+ for ( let i = 0 ; i < len ; i ++ ) {
110+ let currentElement = stack . pop ( ) ;
111+ myQueue . enqueue ( currentElement ) ;
112+ }
104113} ;
105114
106115const myQueue = new Queue ( 4 ) ;
@@ -113,6 +122,7 @@ myQueue.enqueue (4);
113122console . log ( "\n/* ===== Displaying Initial Queue ===== */" ) ;
114123myQueue . displayQueue ( ) ;
115124
125+ reverse ( myQueue ) ;
116126
117127console . log ( "\n/* ===== Displaying Final Queue ===== */" ) ;
118128myQueue . displayQueue ( ) ;
0 commit comments