Advertisement
gregoriusjimmy

Untitled

Nov 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Queue {
  2.   constructor(len) {
  3.     this.array = [];
  4.     this.arrayLen = len;
  5.     this.front = null;
  6.     this.rear = null;
  7.     this.start();
  8.   }
  9.   start() {
  10.     for (let i = 1; i <= this.arrayLen; i++) {
  11.       this.array[i] = null;
  12.     }
  13.   }
  14.   arrayN() {
  15.     let count = 0;
  16.     for (let i = 1; i <= this.arrayLen; i++) {
  17.       if (this.array[i] == null) {
  18.         continue;
  19.       } else {
  20.         count = count + 1;
  21.       }
  22.     }
  23.     return count;
  24.   }
  25.  
  26.   insert(data) {
  27.     if (
  28.       (this.front == 1 && this.rear == this.arrayLen) ||
  29.       this.front == this.rear + 1
  30.     ) {
  31.       return "OVERFLOW";
  32.     }
  33.     if (this.front == null) {
  34.       this.front = 1;
  35.       this.rear = 1;
  36.     } else if (this.rear == this.arrayLen) {
  37.       this.rear = 1;
  38.     } else {
  39.       this.rear = this.rear + 1;
  40.     }
  41.     this.array[this.rear] = data;
  42.   }
  43.  
  44.   remove() {
  45.     if (this.front == null) {
  46.       return "UNDERFLOW";
  47.     } else {
  48.       this.array[this.front] = null;
  49.     }
  50.  
  51.     if (this.front == this.rear) {
  52.       this.front = null;
  53.       this.rear = null;
  54.     } else if (this.front == this.arrayLen) {
  55.       this.front = 1;
  56.     } else {
  57.       this.front = this.front + 1;
  58.     }
  59.   }
  60.   isEmpty() {
  61.     for (let i = 1; i <= this.arrayLen; i++) {
  62.       if (this.array[i] != null) {
  63.         return false;
  64.       }
  65.     }
  66.     return true;
  67.   }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement