Skip to content

Instantly share code, notes, and snippets.

@surajitbasak109
Created October 12, 2022 08:00
Show Gist options
  • Save surajitbasak109/bdb7372b0e037ec0647051a9262e558e to your computer and use it in GitHub Desktop.
Save surajitbasak109/bdb7372b0e037ec0647051a9262e558e to your computer and use it in GitHub Desktop.
ADT for Singly Linked List
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
push(value) {
const node = new Node(value);
if (!this.head) this.head = this.tail = node;
else {
this.tail.next = node;
this.tail = node;
}
this.size++;
return this;
}
pop() {
// return undefined only when head is null
if (this.head == null) return undefined;
// copy head to current variable and copy current to newTail variable
let current = this.head,
newTail = current.next;
// until current has next node
while (current.next) {
// assign current to newTail
newTail = current;
// assign current to its next node
current = current.next;
}
// assign the tail to newtail
this.tail = newTail;
// assign the tails next node to null
this.tail.next = null;
// decrement the size
this.size--;
// if size is 0, set its head and tail to null
if (this.size == 0) this.head = this.tail = null;
// return the current
return current;
}
shift() {
if (this.head == null) return undefined;
let current = this.head;
this.head = current.next;
this.size--;
if (this.size == 0) this.tail = null;
return current;
}
unshift(value) {
let newNode = new Node(value);
if (this.head == null) this.head = this.tail = newNode;
else {
newNode.next = this.head;
this.head = newNode;
}
this.size++;
return this;
}
get(index) {
if (index < 0 || index >= this.size) return null;
let current = this.head;
while (current != null && index > 0) {
current = current.next;
index--;
}
return current;
}
set(index, value) {
let node = this.get(index);
if (node) {
node.value = value;
return true;
} else {
return false;
}
}
insert(index, value) {
if (index < 0 || index > this.size) return false;
if (index == 0) return !!this.unshift(value);
if (index == this.size) return !!this.push(value);
let newNode = new Node(value);
let prev = this.get(index - 1);
let temp = prev.next;
prev.next = newNode;
newNode.next = temp;
this.size++;
return true;
}
remove(index) {
if (index < 0 || index >= this.size) return undefined;
if (index == 0) return this.shift();
if (index == this.size - 1) return this.pop();
let prev = this.get(index - 1);
let removed = prev.next;
prev.next = removed.next;
this.size--;
return removed;
}
reverse() {
let current = this.head,
prev = null,
next = null;
while (current) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
return this;
}
print() {
console.log(JSON.stringify(this.head, null, 2));
}
get length() {
return this.size;
}
}
const sll = new SinglyLinkedList();
sll.push(5).push(9);
sll.print();
console.log(sll.pop());
sll.push(10).push(1).push(6);
console.log(sll.pop());
sll.print();
console.log(sll.shift());
console.log(sll.shift());
console.log(sll.shift());
console.log(sll.shift());
sll.unshift(9).unshift(10).unshift(15).unshift(152).print();
console.log(sll.get(1));
console.log(sll.insert(0, 189));
console.log('Prev State');
sll.print();
console.log(sll.remove(1));
console.log('Current State');
sll.print();
sll.reverse();
sll.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment