Linked list using JavaScript
A linked list is a linear data structure that includes a series of connected nodes. Here each node stores the data and the address of the next node.
Linked list methods:
- size() - Returns the number of nodes present in the linked list.
- clear() - Empties out the list.
- getLast() - Returns the last node of the linked list.
- getFirst() - Returns the first node of the linked list.
class ListNode {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor(head = null) {
this.head = head;
}
size() {
let count = 0;
let node = this.head;
while (node) {
count++;
node = node.next;
}
return count;
}
clear() {
this.head = null;
}
getLast() {
let lastNode = this.head;
if(lastNode) {
while (lastNode.next) {
lastNode = lastNode.next;
}
}
return lastNode;
}
getFirst() {
return this.head;
}
}
let node1 = new ListNode(2);
let node2 = new ListNode(5);
node1.next = node2;
let list = new LinkedList(node1);
list.head.data;
list.head.next.data;
list.size();
list.getFirst();
list.getLast();
References:
Print fibonacci sequence using recursion
Steps:
The fibonnaci sequence is the integer sequence where the first
two terms are 0
and 1
. After that, the next term is defined as
the sum of the previous two terms.
Hence the nth term is sum of n-1
term and n-2
term.
function fibonnaci(num) {
if (num < 2) {
return num;
} else {
return fibonnaci(num - 1) + fibonnaci(num - 2);
}
}
let arr = [];
for(let i = 0; i < 10; i++) {
arr.push(fibonnaci(i));
}
console.log(arr);
References:
Implement Queue using JS
Steps: Queue has following methods
- Enqueue - Add element to the end of queue
- Dequeue - Remove element from the front of queue
- isEmpty - Check if queue is empty
- isFull - Check if queue is full
- Peek - Get the value from front of queue without removing it
class Queue {
constructor() {
this.items = {};
this.headIndex = 0;
this.tailIndex = 0;
}
enqueue(element) {
this.items[this.tailIndex] = element;
this.tailIndex++;
}
dequeue() {
let el = this.items[this.headIndex];
delete this.items[this.headIndex];
if (this.isEmpty()) {
this.headIndex = 0;
this.tailIndex = 0;
} else {
this.headIndex++;
}
return el;
}
peek() {
return this.items[this.headIndex];
}
isEmpty() {
return (this.tailIndex - this.headIndex === 0);
}
clear() {
this.items = {};
this.headIndex = 0;
this.tailIndex = 0;
}
}
let q = new Queue();
q.enqueue(20);
q.enqueue(4);
q.items;
q.dequeue();
q.items;
q.clear();
q.items;
References:
Implement Stack using JS
Steps:
- Stack has methods add, remove, peek, size, clear
class Stack {
constructor() {
this.items = [];
}
add(element) {
this.items.push(element);
}
remove() {
if(this.items.length > 0) {
return this.items.pop();
}
}
peek() {
return this.items[this.items.length - 1];
}
size() {
return this.items.length;
}
clear() {
this.items = [];
}
}
let s = new Stack();
s.add(2);
s.add(3);
s.size();
s.items;
s.remove();
s.peek();
s.items;
s.clear();
s.items;
References:
Find sum of natural numbers using recursion
Steps:
- Create program using recursion
- Call the function recursively until number is 1.
function sum(num) {
let val = 0;
if (num > 0) {
val = num + sum(num - 1);
} else {
val = num;
}
return val;
}
sum(6);