0% found this document useful (0 votes)
5 views

Javascript Code

Uploaded by

seralathan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Javascript Code

Uploaded by

seralathan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class ListNode {

constructor (val) {
this.val = val;
this.next = null;
}
}

class Queue {

// Implementing this with dummy nodes would be easier!


constructor() {
this.left = null;
this.right = null;
}

enqueue(val) {
const newNode = new ListNode(val);
if (this.right != null) {
// Queue is not empty
this.right.next = newNode;
this.right = this.right.next;
} else {
// Queue is empty
this.left = newNode;
this.right = newNode;
}
}

dequeue() {
if (this.left == null) {
// Queue is empty
return;
}
// Remove left node and return value
const val = this.left.val;
this.left = this.left.next;
return val;
}

print() {
let cur = this.left;
let s = "";
while(cur != null) {
s+= cur.val + "->";
cur = cur.next;
}
console.log(s)
}
}

You might also like