Skip to content

Commit

Permalink
feat: add Queue and Stack (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 14, 2024
1 parent e54b63e commit 64f9397
Show file tree
Hide file tree
Showing 11 changed files with 912 additions and 61 deletions.
38 changes: 13 additions & 25 deletions src/DataStructures/DoublyLinkedList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @class DLLNode
* @class
* @name DLLNode
* @classdesc Represents a node in a doubly linked list.
*
* @memberof DataStructures
*/
class Node {
/**
Expand All @@ -19,21 +18,21 @@ class Node {
}

/**
* @class
* @name DoublyLinkedList
* @classdesc
* Represents a doubly linked list data structure.
*
* @memberof DataStructures
* @see https://en.wikipedia.org/wiki/Doubly_linked_list
*
* @example
* new DoublyLinkedList();
* new DoublyLinkedList("Beep");
* new DoublyLinkedList([10,20,30]);
*
* @param {Array|*} value - The value to initialize the list with (optional).
*/
class DoublyLinkedList {
/**
* Creates a new DoublyLinkedList instance.
*
* @example
* new DoublyLinkedList();
* new DoublyLinkedList("Beep");
* new DoublyLinkedList([10,20,30]);
*
* @param {Array|*} value - The value to initialize the list with (optional).
*/
constructor(value) {
this.head = null; // first node to be added
this.tail = null; // last node to be added
Expand All @@ -53,7 +52,6 @@ class DoublyLinkedList {
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*
* @memberof DoublyLinkedList
* @method print
*/
print() {
let temp = this.head;
Expand All @@ -75,7 +73,6 @@ class DoublyLinkedList {
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*
* @memberof DoublyLinkedList
* @method clear
*/
clear() {
this.head = null;
Expand All @@ -96,7 +93,6 @@ class DoublyLinkedList {
* @returns {Number|Node} The value at the specified index, or the Node if returnNode is true.
*
* @memberof DoublyLinkedList
* @method get
*/
get(index, returnNode = false) {
if (index < 0 || index >= this.size) return undefined;
Expand Down Expand Up @@ -129,7 +125,6 @@ class DoublyLinkedList {
* @returns {Boolean} True if the value was set successfully, false otherwise.
*
* @memberof DoublyLinkedList
* @method set
*/
set(index, value) {
const node = this.get(index, true);
Expand All @@ -150,7 +145,6 @@ class DoublyLinkedList {
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*
* @memberof DoublyLinkedList
* @method unshift
*/
unshift(value) {
if (!this.head) return this.push(value);
Expand All @@ -176,7 +170,6 @@ class DoublyLinkedList {
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*
* @memberof DoublyLinkedList
* @method push
*/
push(value) {
const newNode = new Node(value);
Expand Down Expand Up @@ -206,7 +199,6 @@ class DoublyLinkedList {
* @returns {DoublyLinkedList} The current DoublyLinkedList instance.
*
* @memberof DoublyLinkedList
* @method insert
*/
insert(index, value) {
if (index === 0) return this.unshift(value);
Expand Down Expand Up @@ -236,7 +228,6 @@ class DoublyLinkedList {
* @returns {Number} The value that was removed.
*
* @memberof DoublyLinkedList
* @method shift
*/
shift() {
if (this.size === 0) return;
Expand Down Expand Up @@ -265,7 +256,6 @@ class DoublyLinkedList {
* @returns {Number} The value that was removed.
*
* @memberof DoublyLinkedList
* @method pop
*/
pop() {
if (this.size === 0) return;
Expand Down Expand Up @@ -295,7 +285,6 @@ class DoublyLinkedList {
* @returns {Number} The value that was removed.
*
* @memberof DoublyLinkedList
* @method remove
*/
remove(index) {
if (index < 0 || index >= this.size) return false;
Expand Down Expand Up @@ -332,7 +321,6 @@ class DoublyLinkedList {
* @returns {Array} The array representation of the linked list.
*
* @memberof DoublyLinkedList
* @method toArray
*/
toArray() {
const arr = [];
Expand Down
20 changes: 12 additions & 8 deletions src/DataStructures/LinkedList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @class LLNode
* @class
* @name LLNode
* @classdesc Represents a node in a linked list.
*
* @memberof DataStructures
*/
class Node {
/**
Expand All @@ -16,15 +15,20 @@ class Node {
}

/**
* @class
* @name LinkedList
* @classdesc
* Represents a linked list data structure.
*
* @memberof DataStructures
* @see https://en.wikipedia.org/wiki/Linked_list
*
* @param {*} value - The value to initialize the list with (optional).
*
* @property {LLNode} head - The first node in the list
* @property {LLNode} tail - The last node in the list
* @property {Number} size - The number of nodes in the list
*/
class LinkedList {
/**
* Creates a new LinkedList instance.
* @param {*} value - The value to initialize the list with (optional).
*/
constructor(value) {
this.head = null; // first node to be added
this.tail = null; // last node to be added
Expand Down
195 changes: 195 additions & 0 deletions src/DataStructures/Queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**
* @name QueueNode
* @classdesc Represents a node to be used in a Queue class
*/
export class Node {
/**
* Creates a new Node instance.
* @param {*} value - The value to be stored in the node.
*/
constructor(value) {
if (!value) throw new Error("Node value cannot be undefined.");
this.value = value;
this.next = null;
}
}

/**
* @class
* @name Queue
* @classdesc
* Represents a Queue data structure
*
* @see https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
*
* @example
* new Queue();
* new Queue("Beep");
* new Queue([10,20,30]);
*
* @param {Array|*} value - The value to initialize the queue with (optional).
*
* @property {QueueNode} first - The first node in the queue
* @property {QueueNode} last - The last node in the queue
* @property {Number} size - The number of nodes in the queue
*/
export class Queue {
constructor(value) {
this.first = null; // First node to be out
this.last = null; // Last node to be out
this.size = 0;

if (Array.isArray(value)) value.forEach((v) => this.enqueue(v));
else if (value !== undefined) this.enqueue(value);
}

/**
* Adds a new value to the end of the queue
*
* @example
* const q = new Queue([10,20]);
* q.enqueue(30); // [10,20,30]
*
* @param {Number} value - The value to add.
* @returns {Queue} The current Queue instance.
*
* @memberof Queue
*/
enqueue(value) {
const newNode = new Node(value);

if (this.size === 0) {
this.first = newNode;
this.last = newNode;
} else {
this.last.next = newNode;
this.last = newNode;
}

this.size++;
return this;
}

/**
* Removes and returns the first value in the queue
*
* @example
* const q = new Queue([10,20,30]);
* q.dequeue(); // [20,30]
*
* @returns {Queue} The current Queue instance.
*
* @memberof Queue
*/
dequeue(returnNode = false) {
let temp = this.first;

if (this.size === 0) return undefined;
else if (this.size === 1) {
this.first = null;
this.last = null;
} else {
this.first = this.first.next;
temp.next = null;
}

this.size--;
return returnNode ? temp : temp.value;
}

/**
* Returns the first value in the queue
*
* @example
* const q = new Queue([10,20,30]);
* q.peek(); // 10
*
* @param {Boolean} returnNode - Whether to return the node or just the value.
* @returns {*|Node} The value in the first node.
*
* @memberof Queue
*/
peek(returnNode = false) {
if (this.size === 0) return undefined;
return returnNode ? this.first : this.first.value;
}

/**
* Clears the queue
*
* @example
* const q = new Queue([10,20,30]);
* q.clear(); // []
*
* @returns {Queue} The current Queue instance.
* @memberof Queue
*/
clear() {
this.first = null;
this.last = null;
this.size = 0;

return this;
}

/**
* Prints the values of the queue
*
* @example
* const q = new Queue([10,20,30]);
* q.print(); // 10,20,30
*
* @returns {Queue} The current Queue instance.
*
* @memberof Queue
*/
print() {
let node = this.first;

while (node) {
console.log(node.value);
node = node.next;
}

return this;
}

/**
* Checks if the queue is empty
*
* @example
* const q = new Queue([10,20,30]);
* q.isEmpty(); // false
*
* @returns {Boolean} Whether or not the queue is empty.
*
* @memberof Queue
*/
isEmpty() {
return this.size === 0;
}

/**
* Returns a new array of the values in the queue
*
* @example
* const q = new Queue([10,20,30]);
* q.toArray(); // [10,20,30]
*
* @returns {Array} An array of the values in the queue.
*
* @memberof Queue
*/
toArray() {
const arr = [];

let node = this.first;

for (let i = 0; i < this.size; i++) {
arr.push(node.value);
node = node.next;
}

return arr;
}
}

0 comments on commit 64f9397

Please sign in to comment.