Skip to content

Commit

Permalink
Add Queue class
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Aug 31, 2021
1 parent baf3ae9 commit 88efb09
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { test } from './stack.js'
import { test } from './queue.js'

test()
123 changes: 123 additions & 0 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Stack } from './stack.js'

class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}

class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
peek() {
return this.first;
}
enqueue(value) {
if (!this.length) {
this.first = this.last = new Node(value);
} else {
this.last.next = new Node(value);
this.last = this.last.next;
}
this.length++;
}
dequeue() {
if (!this.length) return null;

const first = this.first;
this.first = this.first.next;
this.length--;

if (!this.length) this.last = null;

return first;
}
array() {
const arr = [];
let current = this.first;
while(current) {
arr.push(current.value);
current = current.next;
}
return arr;
}
}

class QueueStack {
constructor() {
this.stack = new Stack();
}
enqueue(value) {
this.stack.push(value);
}
dequeue() {
if (this.stack.isEmpty()) return null;

const temp = new Stack();
while (this.stack.length > 1) {
temp.push(this.stack.pop().value);
}
const first = this.stack.pop();
while (temp.length > 0) {
this.stack.push(temp.pop().value);
}
return first;
}
peek() {
// TODO:
}
isEmpty() {
return this.stack.isEmpty();
}
array() {
return this.stack.array();
}
get length() {
return this.stack.length;
}
}

export function test() {
// const queue = new Queue();
const queue = new QueueStack();
const n = 10;
for (let i = 0; i < n; i++) {
queue.enqueue(i);
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());

console.log("\ndequeue 1 item");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());

console.log("\ndequeue everything except last");
while (queue.length > 1) {
queue.dequeue();
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());

console.log("\ndequeue last");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());

console.log("\ndequeue empty queue");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());

console.log("\nenqueue " + n + " items");
queue.enqueue(0);
for (let i = 1; i < 10; i++) {
queue.enqueue(i);
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
}
76 changes: 51 additions & 25 deletions src/stack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}

Expand All @@ -15,53 +15,79 @@ export class Stack {
return this.top;
}
push(value) {
const newNode = new Node(value);
if (this.isEmpty()) {
this.top = new Node(value);
this.bottom = this.top;
this.bottom = this.top = newNode;
} else {
this.top.next = new Node(value);
this.top = this.top.next;
const temp = this.top;
this.top = newNode;
newNode.prev = temp;
}
this.length++;
return this;
}
pop() {
if (this.isEmpty()) return null;

if (this.length === 1) {
this.bottom = this.top = null;
} else {
const current = this.traverse(this.length-2);
this.top = current;
this.top.next = null;
}

const top = this.top;

this.top = this.top.prev;
this.length--;
return this;
if (this.isEmpty) this.bottom = null;

return top;
}
isEmpty() {
return !this.length;
}
traverse(index) {
let current = this.bottom;
for (let i = 0; i < index; i++) {
current = current.next;
}
return current;
}
array() {
const arr = [];
let current = this.bottom;
let current = this.top;
while (current) {
arr.push(current.value);
current = current.next;
current = current.prev;
}
return arr;
return arr.reverse();
}
}

export class StackArray {
constructor() {
this.arr = [];
}
peek() {
// if (!this.arr.length) return null;
return this.arr[this.arr.length-1];
}
push(value) {
this.arr.push(value);
return this;
}
pop() {
// if (!this.arr.length) return null;
return this.arr.pop();
}
isEmpty() {
return !this.arr.length;
}

array() {
return this.arr;
}
get length() {
return this.arr.length;
}
get bottom() {
return this.arr[0]
}
get top() {
return this.arr[this.arr.length-1];
}
}

export function test() {
const stack = new Stack();
// const stack = new Stack();
const stack = new StackArray();
const n = 10;
for (let i = 0; i < n; i++) {
stack.push(i);
Expand Down

0 comments on commit 88efb09

Please sign in to comment.