Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathqueueReversal.js
More file actions
Latest commit
128 lines (108 loc) · 3.03 KB
/
Copy pathqueueReversal.js
File metadata and controls
128 lines (108 loc) · 3.03 KB
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
classQueue{
constructor(size){
this.capacty=size;
this.data=[];
this.frontIndex=0;
this.rearIndex=0;
}
front(){
returndata[this.frontIndex];
}
rear(){
returndata[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--;
returnthis.data.pop();
}
displayQueue(){
lettoBeDisplayed='-> ';
for(letelementofthis.data){
toBeDisplayed+=element+' -> ';
}
console.log(toBeDisplayed);
}
}
// Class declaration for stack data structure
classStack{
// 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)returntrue;
elsereturnfalse;
}
// isFull method to check whether the stack is full
isFull(){
if(this.myStack.length===this.top)returntrue;
elsereturnfalse;
}
// 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()){
returnthis.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]}`);
}
}
constreverse=(myQueue)=>{
conststack=newStack(10);
letlen=myQueue.rearIndex;
for(leti=len;i>0;i--){
letcurrentElement=myQueue.dequeue();
stack.push(currentElement);
}
for(leti=0;i<len;i++){
letcurrentElement=stack.pop();
myQueue.enqueue(currentElement);
}
};
constmyQueue=newQueue(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();