|  | 
| 1 | 1 | var Stack = require('./../util/Stack'); | 
| 2 | 2 | 
 | 
| 3 |  | -var myQueue = function() { | 
| 4 |  | -  this.front = new Stack(); | 
| 5 |  | -  this.back = new Stack(); | 
| 6 |  | -  this.backUp = true; | 
| 7 |  | -}; | 
|  | 3 | +class MyQueue { | 
|  | 4 | +  constructor() { | 
|  | 5 | +    this.front = new Stack(); | 
|  | 6 | +    this.back = new Stack(); | 
|  | 7 | +    this.backUp = true; | 
|  | 8 | +  } | 
| 8 | 9 | 
 | 
| 9 |  | -myQueue.prototype.add = function(value) { | 
| 10 |  | -  if (!this.backUp) { | 
| 11 |  | -    while (!this.front.isEmpty()) { | 
| 12 |  | -      this.back.push(this.front.pop()); | 
|  | 10 | +  add(value) { | 
|  | 11 | +    if (!this.backUp) { | 
|  | 12 | +      while (!this.front.isEmpty()) { | 
|  | 13 | +        this.back.push(this.front.pop()); | 
|  | 14 | +      } | 
|  | 15 | +      this.backUp = true; | 
| 13 | 16 |     } | 
| 14 |  | -    this.backUp = true; | 
|  | 17 | +    this.back.push(value); | 
| 15 | 18 |   } | 
| 16 |  | -  this.back.push(value); | 
| 17 |  | -}; | 
| 18 | 19 | 
 | 
| 19 |  | -myQueue.prototype.remove = function() { | 
| 20 |  | -  if (this.backUp) { | 
| 21 |  | -    while(!this.back.isEmpty()) { | 
| 22 |  | -      this.front.push(this.back.pop()); | 
|  | 20 | +  remove() { | 
|  | 21 | +    if (this.backUp) { | 
|  | 22 | +      while(!this.back.isEmpty()) { | 
|  | 23 | +        this.front.push(this.back.pop()); | 
|  | 24 | +      } | 
|  | 25 | +      this.backUp = false; | 
| 23 | 26 |     } | 
| 24 |  | -    this.backUp = false; | 
|  | 27 | +    return this.front.pop(); | 
| 25 | 28 |   } | 
| 26 |  | -  return this.front.pop(); | 
| 27 |  | -}; | 
| 28 | 29 | 
 | 
| 29 |  | -myQueue.prototype.peek = function() { | 
| 30 |  | -  if (this.backUp) { | 
| 31 |  | -    while(!this.back.isEmpty()) { | 
| 32 |  | -      this.front.push(this.back.pop()); | 
|  | 30 | +  peek() { | 
|  | 31 | +    if (this.backUp) { | 
|  | 32 | +      while(!this.back.isEmpty()) { | 
|  | 33 | +        this.front.push(this.back.pop()); | 
|  | 34 | +      } | 
|  | 35 | +      this.backUp = false; | 
| 33 | 36 |     } | 
| 34 |  | -    this.backUp = false; | 
|  | 37 | +    return this.front.peek(); | 
| 35 | 38 |   } | 
| 36 |  | -  return this.front.peek(); | 
| 37 |  | -}; | 
| 38 | 39 | 
 | 
| 39 |  | -myQueue.prototype.isEmpty = function() { | 
| 40 |  | -  return this.front.isEmpty() && this.back.isEmpty(); | 
| 41 |  | -}; | 
|  | 40 | +  isEmpty() { | 
|  | 41 | +    return this.front.isEmpty() && this.back.isEmpty(); | 
|  | 42 | +  } | 
|  | 43 | +} | 
| 42 | 44 | 
 | 
| 43 | 45 | /* TEST */ | 
| 44 |  | -var m = new myQueue(); | 
|  | 46 | +var m = new MyQueue(); | 
| 45 | 47 | console.log(m.isEmpty(), true); | 
| 46 | 48 | 
 | 
| 47 | 49 | m.add('a'); | 
|  | 
0 commit comments