Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue {
- constructor(len) {
- this.array = [];
- this.arrayLen = len;
- this.front = null;
- this.rear = null;
- this.start();
- }
- start() {
- for (let i = 1; i <= this.arrayLen; i++) {
- this.array[i] = null;
- }
- }
- arrayN() {
- let count = 0;
- for (let i = 1; i <= this.arrayLen; i++) {
- if (this.array[i] == null) {
- continue;
- } else {
- count = count + 1;
- }
- }
- return count;
- }
- insert(data) {
- if (
- (this.front == 1 && this.rear == this.arrayLen) ||
- this.front == this.rear + 1
- ) {
- return "OVERFLOW";
- }
- if (this.front == null) {
- this.front = 1;
- this.rear = 1;
- } else if (this.rear == this.arrayLen) {
- this.rear = 1;
- } else {
- this.rear = this.rear + 1;
- }
- this.array[this.rear] = data;
- }
- remove() {
- if (this.front == null) {
- return "UNDERFLOW";
- } else {
- this.array[this.front] = null;
- }
- if (this.front == this.rear) {
- this.front = null;
- this.rear = null;
- } else if (this.front == this.arrayLen) {
- this.front = 1;
- } else {
- this.front = this.front + 1;
- }
- }
- isEmpty() {
- for (let i = 1; i <= this.arrayLen; i++) {
- if (this.array[i] != null) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement