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