Question

In: Computer Science

This all has to be in javascript Use the following variables and data structures to answer...

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)?

Solutions

Expert Solution

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


Related Solutions

This all has to be in javascript Use this linked list to answer the questions below:...
This all has to be in javascript Use this linked list to answer the questions below: var list = new LinkedList(); list.append(1); list.append(2); list.append(3); list.append(4); 1. We are going to extend LinkedList with a sum method. As the name implies, all elements in the linked list should be summed together. First, write a few tests that will eventually show your code works correctly (after you implement the function in (2)). Note: see the LinkedList implementation above for more details. Hint:...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
Please answer all the questions. Thank you Use the following data to answer the questions below:...
Please answer all the questions. Thank you Use the following data to answer the questions below: Column 1 Column 2 Column 3 Y in $ C in $ 0 500 500 850 1,000 1,200 1,500 1,550 2,000 1,900 2,500 2,250 3,000 2,600 What is mpc and mps? Compute mpc and mps. Assume investment equals $ 100, government spending equals $ 75, exports equal $ 50 and imports equal $ 35. Compute the aggregate expenditure in column 3. Draw a graph...
please answer following questions Explain  the presence or absence of the following structures in all plant groups:...
please answer following questions Explain  the presence or absence of the following structures in all plant groups: spores, waxy cuticle, true stomata, vascular tissue (xylem and phloem), leaves, roots, cones, seeds, flowers/fruit.   Explain flower anatomy and describe the function of the following structures: anther, pollen, filament, stamen, carpel, stigma, style, ovary, ovule, sepals, petals, receptacle.   Explain a eudicot from a monocot.   Explain the parts of an angiosperm leaf: nodes, internodes, petiole, blade, terminal bud. Explain a compound leaf verses a simple...
Homework 4: Probability and z scores Use the data to answer the following questions Show all...
Homework 4: Probability and z scores Use the data to answer the following questions Show all work (*round 2 decimals places) What is the mean & SD for the distribution of scores? What is the corresponding z score for each raw score? What is the probability of getting a raw score greater than 4? What is the probability of getting a raw score less than 2? What is the probability of a raw score between 4 & 6? What is...
Which of the following variables yields data that would be suitable for use in a histogram?...
Which of the following variables yields data that would be suitable for use in a histogram? __________ the time concentrating on a math problem color of hair brand of TV gender of a student
Use the following data to answer the questions below:
Use the following data to answer the questions below:             Q                 VC             MC           AVC              1                 $10              ___            ___                                   2                16              ___            ___                       3                20              ___            ___                                   4                25              ___            ___              5                31              ___            ___              6                  38              ___            ___                  7                  46              ___            ___8                  55              ___            ___9                  65              ___            ___         a. Calculate the marginal cost and average variable cost for each level of production.b. How much would the firm produce if it could sell its product for...
Use the following to answer questions 20-22 Amy Co. has the following data related to an...
Use the following to answer questions 20-22 Amy Co. has the following data related to an item of inventory: Inventory, May 1 40 units @ $100 Purchase, May 7 70 units @ $80 Purchase, May 16 170 units @ $60 Ending Inventory 10 units 20. The value assigned to ending inventory if Amy uses LIFO is 21. The value assigned to cost of goods sold if Amy uses FIFO is 22. The value assigned to ending inventory if Amy uses...
The following table provides data for a project. Use the data to answer the following questions...
The following table provides data for a project. Use the data to answer the following questions Activity Immediate Predecessor Time Estimates (weeks) Optimistic Most Likely Pessimistic Expected Variance A - 6 7 14 B - 8 10 12 C A 2 3 4 D A 6 7 8 E B,C 5 5.5 9 F B,C 5 7 9 G D,E 4 6 8 H F 2.5 3 3.5 Draw a network to represent the above project. What is the critical...
Consider the following set of dependent and independent variables. Use this data to complete parts a...
Consider the following set of dependent and independent variables. Use this data to complete parts a and b below. Construct a​ 95% confidence interval for the regression coefficient for x1 y   x1   x2 10   2   15 14   7   8 15   5   12 17   9   12 22   6   0 23   12   9 29   13   5 33   20   2 a. The​ 95% confidence interval for the true population coefficient B1 is _____ to _____. (Round to three decimal places as​ needed.) b....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT