Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. function Queue () {
  2. collection = [];
  3. this.print = function() {
  4. console.log(collection);
  5. };
  6. this.enqueue = function(element) {
  7. collection.push(element);
  8. };
  9. this.dequeue = function() {
  10. return collection.shift();
  11. };
  12. this.front = function() {
  13. return collection[0];
  14. };
  15. this.size = function() {
  16. return collection.length;
  17. };
  18. this.isEmpty = function() {
  19. return (collection.length === 0);
  20. };
  21. }
  22.  
  23. var q = new Queue();
  24. q.enqueue('a');
  25. q.enqueue('b');
  26. q.enqueue('c');
  27. q.print();
  28. q.dequeue();
  29. console.log(q.front());
  30. q.print();
  31.  
  32.  
  33. function PriorityQueue () {
  34. var collection = [];
  35. this.printCollection = function() {
  36. (console.log(collection));
  37. };
  38. this.enqueue = function(element){
  39. if (this.isEmpty()){
  40. collection.push(element);
  41. } else {
  42. var added = false;
  43. for (var i=0; i<collection.length; i++){
  44. if (element[1] < collection[i][1]){ //checking priorities
  45. collection.splice(i,0,element);
  46. added = true;
  47. break;
  48. }
  49. }
  50. if (!added){
  51. collection.push(element);
  52. }
  53. }
  54. };
  55. this.dequeue = function() {
  56. var value = collection.shift();
  57. return value[0];
  58. };
  59. this.front = function() {
  60. return collection[0];
  61. };
  62. this.size = function() {
  63. return collection.length;
  64. };
  65. this.isEmpty = function() {
  66. return (collection.length === 0);
  67. };
  68. }
  69.  
  70. var pq = new PriorityQueue();
  71. pq.enqueue(['Beau Carnes', 2]);
  72. pq.enqueue(['Quincy Larson', 3]);
  73. pq.enqueue(['Ewa Mitulska-Wójcik', 1])
  74. pq.enqueue(['Briana Swift', 2])
  75. pq.printCollection();
  76. pq.dequeue();
  77. console.log(pq.front());
  78. pq.printCollection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement