-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e17aee
commit 7ae115d
Showing
6 changed files
with
355 additions
and
3 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
dist/linked-lists/doubly-linked-list/DoublyLinkedList.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Node { | ||
constructor(val) { | ||
this.val = val; | ||
this.next = null; | ||
this.prev = null; | ||
} | ||
} | ||
class DoublyLinkedList { | ||
constructor() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
push(val) { | ||
const newNode = new Node(val); | ||
if (this.tail === null) | ||
this.head = this.tail = newNode; | ||
else { | ||
newNode.prev = this.tail; | ||
this.tail.next = newNode; | ||
this.tail = newNode; | ||
} | ||
this.length++; | ||
return this.length; | ||
} | ||
pop() { | ||
this.length--; | ||
if (this.head === null) | ||
return; | ||
const oldTail = this.tail; | ||
if (this.head === this.tail) { | ||
this.head = null; | ||
this.tail = null; | ||
return oldTail; | ||
} | ||
this.tail = this.tail.prev; | ||
this.tail.next = null; | ||
return oldTail; | ||
} | ||
unshift(val) { | ||
const newNode = new Node(val); | ||
if (this.head === null) | ||
this.head = this.tail = newNode; | ||
else { | ||
this.head.prev = newNode; | ||
newNode.next = this.head; | ||
this.head = newNode; | ||
} | ||
this.length++; | ||
return newNode; | ||
} | ||
shift() { | ||
if (this.head === null) | ||
return; | ||
const oldHead = this.head; | ||
if (this.head === this.tail) { | ||
this.head = null; | ||
this.tail = null; | ||
return oldHead; | ||
} | ||
this.head = this.head.next; | ||
this.head.prev = null; | ||
this.length--; | ||
return oldHead; | ||
} | ||
get(index) { | ||
if (index < 0 || index >= this.length) | ||
return; | ||
let counter = index < this.length / 2 ? 0 : this.length - 1; | ||
let current = index < this.length / 2 ? this.head : this.tail; | ||
while (index !== counter) { | ||
current = index < this.length / 2 ? current.next : current.prev; | ||
index < this.length / 2 ? counter++ : counter--; | ||
} | ||
return current; | ||
} | ||
set(val, index) { | ||
const selectedNode = this.get(index); | ||
if (!selectedNode) | ||
return false; | ||
selectedNode.val = val; | ||
return true; | ||
} | ||
insert(val, index) { | ||
if (index < 0 || index > this.length) | ||
return false; | ||
if (index === 0) | ||
return !!this.unshift(val); | ||
if (index === this.length) | ||
return !!this.push(val); | ||
const newNode = new Node(val); | ||
const prev = this.get(index - 1); | ||
newNode.next = prev.next; | ||
newNode.prev = prev; | ||
prev.next.prev = newNode; | ||
prev.next = newNode; | ||
this.length++; | ||
return true; | ||
} | ||
remove(index) { | ||
if (index < 0 || index > this.length) | ||
return false; | ||
if (index === 0) | ||
return !!this.shift(); | ||
if (index === this.length) | ||
return !!this.pop(); | ||
const selectedNode = this.get(index); | ||
selectedNode.prev.next = selectedNode.next; | ||
selectedNode.next.prev = selectedNode.prev; | ||
this.length--; | ||
return true; | ||
} | ||
reverse() { | ||
let node = this.head; | ||
this.head = this.tail; | ||
this.tail = node; | ||
for (let i = 0; i < this.length; i++) { | ||
const tempPrev = node.prev; | ||
node.prev = node.next; | ||
node.next = tempPrev; | ||
node = node.prev; | ||
} | ||
return this; | ||
} | ||
getHead() { | ||
return this.head; | ||
} | ||
getTail() { | ||
return this.tail; | ||
} | ||
} | ||
exports.default = DoublyLinkedList; |
145 changes: 145 additions & 0 deletions
145
dist/linked-lists/singly-linked-list/SinglyLinkedList.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Node = void 0; | ||
class Node { | ||
constructor(val) { | ||
this.val = val; | ||
this.next = null; | ||
} | ||
} | ||
exports.Node = Node; | ||
class SinglyLinkedList { | ||
constructor() { | ||
this.length = 0; | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
; | ||
push(val) { | ||
const newNode = new Node(val); | ||
this.length++; | ||
if (this.tail !== null) { | ||
this.tail.next = newNode; | ||
this.tail = this.tail.next; | ||
return; | ||
} | ||
this.tail = newNode; | ||
this.head = this.tail; | ||
return this; | ||
} | ||
pop() { | ||
if (this.head === null) | ||
return; | ||
this.length--; | ||
let currentNode = this.head; | ||
let pre = currentNode; | ||
while (currentNode.next !== null) { | ||
pre = currentNode; | ||
currentNode = currentNode.next; | ||
} | ||
this.tail = pre; | ||
this.tail.next = null; | ||
if (this.length === 0) { | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
return currentNode; | ||
} | ||
unshift(val) { | ||
let newNode = new Node(val); | ||
if (this.head === null) { | ||
this.head = newNode; | ||
this.tail = newNode; | ||
} | ||
else { | ||
newNode.next = this.head; | ||
this.head = newNode; | ||
} | ||
this.length++; | ||
return this; | ||
} | ||
shift() { | ||
if (this.head === null) | ||
return; | ||
const head = this.head; | ||
this.head = this.head.next; | ||
this.length--; | ||
if (this.length === 0) { | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
return head; | ||
} | ||
getLength() { | ||
return this.length; | ||
} | ||
get(index) { | ||
if (index >= this.length || index < 0) | ||
return -1; | ||
let counter = 0; | ||
let current = this.head; | ||
while (counter !== index) { | ||
current = current.next; | ||
counter++; | ||
} | ||
return current; | ||
} | ||
set(val, index) { | ||
const node = this.get(index); | ||
if (node === -1) | ||
return false; | ||
node.val = val; | ||
return true; | ||
} | ||
insert(val, index) { | ||
if (this.length < index || index < 0) | ||
return false; | ||
if (index === 0) | ||
this.unshift(val); | ||
else if (this.length === index) | ||
this.push(val); | ||
else { | ||
const newNode = new Node(val); | ||
const prevNode = this.get(index - 1); | ||
const nextNode = prevNode.next; | ||
prevNode.next = newNode; | ||
newNode.next = nextNode; | ||
this.length++; | ||
} | ||
return true; | ||
} | ||
remove(index) { | ||
if (this.length <= index || index < 0) | ||
return; | ||
if (index === 0) | ||
return this.shift(); | ||
if (this.length - 1 === index) | ||
return this.pop(); | ||
const prevNode = this.get(index - 1); | ||
const nextNode = prevNode.next; | ||
prevNode.next = nextNode === null || nextNode === void 0 ? void 0 : nextNode.next; | ||
this.length--; | ||
return nextNode; | ||
} | ||
reverse() { | ||
let node = this.head; | ||
this.head = this.tail; | ||
this.tail = node; | ||
let prev = null; | ||
let next; | ||
for (let i = 0; i < this.length; i++) { | ||
next = node === null || node === void 0 ? void 0 : node.next; | ||
node.next = prev; | ||
prev = node; | ||
node = next; | ||
} | ||
return this; | ||
} | ||
getTail() { | ||
return this.tail; | ||
} | ||
getHead() { | ||
return this.head; | ||
} | ||
} | ||
exports.default = SinglyLinkedList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const SinglyLinkedList_1 = require("../linked-lists/singly-linked-list/SinglyLinkedList"); | ||
class StackLinkedList { | ||
constructor() { | ||
this.first = null; | ||
this.last = null; | ||
this.size = 0; | ||
} | ||
push(val) { | ||
const newNode = new SinglyLinkedList_1.Node(val); | ||
if (this.last !== null) | ||
this.last.next = newNode; | ||
if (this.last === null) | ||
this.last = this.first = newNode; | ||
this.last = newNode; | ||
return ++this.size; | ||
} | ||
pop() { | ||
if (this.first === null) | ||
return null; | ||
if (this.first === this.last) | ||
this.last = null; | ||
this.first = this.first.next; | ||
this.size--; | ||
return this.first; | ||
} | ||
peek() { | ||
return this.first; | ||
} | ||
isEmpty() { | ||
return !this.first; | ||
} | ||
} | ||
exports.default = StackLinkedList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { throws } from "assert"; | ||
import { compileFunction } from "vm"; | ||
|
||
class Node <T>{ | ||
val: T; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class Node<T> { | ||
export class Node<T> { | ||
public val: T; | ||
public next: Node<T> | null; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Node } from "../linked-lists/singly-linked-list/SinglyLinkedList"; | ||
|
||
class StackLinkedList<T> { | ||
first: Node<T> | null; | ||
last: Node<T> | null; | ||
size: number; | ||
|
||
constructor(){ | ||
this.first = null; | ||
this.last = null; | ||
this.size = 0; | ||
} | ||
|
||
push(val: T): number{ | ||
const newNode = new Node(val); | ||
if(this.last !== null) this.last.next = newNode; | ||
if(this.last === null) this.last = this.first = newNode; | ||
this.last = newNode; | ||
return ++this.size; | ||
} | ||
|
||
pop(): Node<T> | null { | ||
if(this.first === null) return null; | ||
if(this.first === this.last) this.last = null; | ||
this.first = this.first.next; | ||
this.size--; | ||
return this.first; | ||
} | ||
|
||
peek(): Node<T> | null { | ||
return this.first; | ||
} | ||
|
||
isEmpty(): boolean { | ||
return !this.first; | ||
} | ||
} | ||
|
||
|
||
export default StackLinkedList; |