In: Computer Science
This all has to be in javascript
Use the following variables and data structures to answer this question.
var queue = new Queue();
var stack = new Stack();
1. Enqueue the numbers 1, 2, 3, 4, and 5 in the queue. Then print the contents of the queue to the console. Note: queue and stack both have a print() method.
2. Dequeue each number from the queue and push it onto the stack. Then print the contents of the stack to the console. Note: the queue should be empty after this operation.
3. Pop all the numbers from the stack and put them back in the queue. Then print the contents of the queue to the console. Note: the stack should be empty after this operation.
4. What is different in the output of (c) compared to (a)?
1.
class Queue
{
constructor() { this.items = []; }
enqueue(val) { this.items.push(val); }
dequeue()
{
if(this.isEmpty()) return "Error: Underflow";
return this.items.shift();
}
isEmpty() { return this.items.length == 0; }
print() //Method for printing Queue
{
var str = "";
for(var i = 0; i < this.items.length; i++)
str += this.items[i] +" ";
return str;
}
}
class Stack {
constructor() { this.items = []; }
push(val) { this.items.push(val); }
pop()
{ if (this.items.length == 0)
return "Error: Underflow";
return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
isEmpty() { return this.items.length == 0; }
print()
{
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
}
var queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
console.log("Contents of Queue: "+queue.print());
2.
class Queue
{
constructor() { this.items = []; }
enqueue(val) { this.items.push(val); }
dequeue()
{
if(this.isEmpty()) return "Error: Underflow";
return this.items.shift();
}
isEmpty() { return this.items.length == 0; }
print() //Method for printing Queue
{
var str = "";
for(var i = 0; i < this.items.length; i++)
str += this.items[i] +" ";
return str;
}
}
class Stack {
constructor() { this.items = []; }
push(val) { this.items.push(val); }
pop()
{ if (this.items.length == 0)
return "Error: Underflow";
return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
isEmpty() { return this.items.length == 0; }
print()
{
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
}
var queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
console.log("Contents of Queue: "+queue.print());
var stack = new Stack();
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
console.log("Contents of stack: "+ stack.print());
3.
class Queue
{
constructor() { this.items = []; }
enqueue(val) { this.items.push(val); }
dequeue()
{
if(this.isEmpty()) return "Error: Underflow";
return this.items.shift();
}
isEmpty() { return this.items.length == 0; }
print() //Method for printing Queue
{
var str = "";
for(var i = 0; i < this.items.length; i++)
str += this.items[i] +" ";
return str;
}
}
class Stack {
constructor() { this.items = []; }
push(val) { this.items.push(val); }
pop()
{ if (this.items.length == 0)
return "Error: Underflow";
return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
isEmpty() { return this.items.length == 0; }
print()
{
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
}
var queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
console.log("Contents of Queue: "+queue.print());
var stack = new Stack();
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
stack.push(queue.dequeue());
console.log("Contents of stack: "+ stack.print());
queue.enqueue(stack.pop());
queue.enqueue(stack.pop());
queue.enqueue(stack.pop());
queue.enqueue(stack.pop());
queue.enqueue(stack.pop());
console.log("Contents of Queue: "+queue.print());
4.
The order is reversed when compared to a) c)
Because queue follows First In First Out
Where as Stack follows First In Last Out