-
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
Showing
13 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
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 +1,5 @@ | ||
export * from './lib/data-structure'; | ||
export * from './lib/Graph'; | ||
export * from './lib/LinkedList'; | ||
export * from './lib/Queue'; | ||
export * from './lib/Stack'; |
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,49 @@ | ||
import { Node } from './Node'; | ||
|
||
export class Graph<T> { | ||
directed: boolean; | ||
nodes: Array<Node>; | ||
|
||
constructor(directed = false) { | ||
this.directed = directed; | ||
this.nodes = []; | ||
} | ||
|
||
addNode(value: T) { | ||
this.nodes.push(new Node(value)); | ||
} | ||
|
||
removeNode(value: T) { | ||
const isNotThisNode = (nodeValue: T) => { | ||
return (node: Node) => node.value !== nodeValue; | ||
}; | ||
|
||
// filter main node's list | ||
this.nodes = this.nodes.filter(isNotThisNode(value)); | ||
// filter every node's edges | ||
this.nodes.forEach((node: Node) => { | ||
node.edges = node.edges.filter(isNotThisNode(value)); | ||
}); | ||
} | ||
|
||
getNode(value: T): Node | undefined { | ||
return this.nodes.find((node: Node) => node.value === value); | ||
} | ||
|
||
addEdge(firstValue: T, secondValue: T) { | ||
const firstNode: Node | undefined = this.getNode(firstValue); | ||
const secondNode: Node | undefined = this.getNode(secondValue); | ||
|
||
if (firstNode && secondNode) { | ||
firstNode.edges.push(secondNode); | ||
|
||
if (!this.directed) { | ||
secondNode.edges.push(firstNode); | ||
} | ||
|
||
return; | ||
} | ||
|
||
throw new Error('node values not found'); | ||
} | ||
} |
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,9 @@ | ||
export class Node { | ||
value: any; | ||
edges: Array<Node>; | ||
|
||
constructor(value: any) { | ||
this.value = value; | ||
this.edges = []; | ||
} | ||
} |
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,2 @@ | ||
export * from './Graph'; | ||
export * from './Node'; |
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,9 @@ | ||
import { LinkedList } from './LinkedList' | ||
describe('LinkedList', ()=> { | ||
it('initalize empty list', ()=> { | ||
const list = new LinkedList(); | ||
|
||
expect(list).toBeInstanceOf(LinkedList); | ||
expect(list.length).toBe(0); | ||
}) | ||
}); |
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,118 @@ | ||
import { Node } from './Node'; | ||
|
||
export class LinkedList<T> { | ||
head: Node | null; | ||
tail: Node | null; | ||
length: number; | ||
|
||
constructor() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
|
||
push(value: T) { | ||
const node = new Node(value); | ||
|
||
if (this.isEmpty()) { | ||
this.head = node; | ||
this.tail = node; | ||
} else { | ||
this.tail.next = node; | ||
this.tail = node; | ||
} | ||
|
||
this.length++; | ||
} | ||
|
||
pop(): Node | null { | ||
if (this.isEmpty()) return null; | ||
|
||
if (this.length === 1) { | ||
const nodeToRemove = this.head; | ||
this.head = null; | ||
this.tail = null; | ||
this.length--; | ||
|
||
return nodeToRemove; | ||
} | ||
|
||
let currentNode = this.head; | ||
const nodeToRemove = this.tail; | ||
let secondLastNode = null; | ||
|
||
while (currentNode) { | ||
if (currentNode.next === this.tail) { | ||
secondLastNode = currentNode; | ||
break; | ||
} | ||
|
||
currentNode = currentNode.next; | ||
} | ||
|
||
secondLastNode.next = null; | ||
this.tail = secondLastNode; | ||
this.length--; | ||
|
||
return nodeToRemove; | ||
} | ||
|
||
get(index: number): Node | null { | ||
if (index < 0 || index >= this.length || this.isEmpty()) return null; | ||
|
||
if (index === 0) return this.head; | ||
|
||
if (index === this.length - 1) return this.tail; | ||
|
||
let currentNode = this.head; | ||
let iterator = 0; | ||
|
||
while (iterator < index) { | ||
iterator++; | ||
currentNode = currentNode.next; | ||
} | ||
|
||
return currentNode; | ||
} | ||
|
||
delete(index: number): Node | null { | ||
if (index < 0 || index >= this.length || this.isEmpty()) return null; | ||
|
||
let nodeToRemove; | ||
|
||
if (index === 0) { | ||
nodeToRemove = this.head; | ||
this.head = nodeToRemove.next; | ||
this.length--; | ||
return nodeToRemove; | ||
} | ||
|
||
let iterator = 1; | ||
nodeToRemove = this.head.next; | ||
let secondLastNode = this.head; | ||
|
||
while (iterator < index) { | ||
if (iterator === index) { | ||
break; | ||
} | ||
|
||
nodeToRemove = nodeToRemove.next; | ||
secondLastNode = secondLastNode.next; | ||
iterator++; | ||
} | ||
|
||
if (nodeToRemove === this.tail) { | ||
this.tail = secondLastNode; | ||
} | ||
|
||
secondLastNode.next = nodeToRemove.next; | ||
|
||
this.length--; | ||
|
||
return nodeToRemove; | ||
} | ||
|
||
isEmpty(): boolean { | ||
return this.length === 0; | ||
} | ||
} |
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,11 @@ | ||
export class Node { | ||
data: any; | ||
next: Node | null; | ||
previous: Node | null; | ||
|
||
constructor(data: any) { | ||
this.data = data; | ||
this.next = null; | ||
this.previous = 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 @@ | ||
export * from './LinkedList'; |
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,27 @@ | ||
export class Queue<T> { | ||
queue: Array<T>; | ||
|
||
constructor() { | ||
this.queue = []; | ||
} | ||
|
||
enqueue(data: T) { | ||
this.queue.push(data); | ||
} | ||
|
||
dequeue() { | ||
this.queue.shift(); | ||
} | ||
|
||
peek(): T { | ||
return this.queue[0]; | ||
} | ||
|
||
isEmpty(): boolean { | ||
return this.length === 0; | ||
} | ||
|
||
get length(): number { | ||
return this.queue.length; | ||
} | ||
} |
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 @@ | ||
export * from './Queue'; |
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,27 @@ | ||
export class Stack<T> { | ||
stack: Array<T>; | ||
|
||
constructor() { | ||
this.stack = []; | ||
} | ||
|
||
pop() { | ||
return this.stack.pop(); | ||
} | ||
|
||
push(item: T) { | ||
this.stack.push(item); | ||
} | ||
|
||
peek(): T { | ||
return this.stack[this.stack.length - 1]; | ||
} | ||
|
||
isEmpty(): boolean { | ||
return this.stack.length === 0; | ||
} | ||
|
||
get length(): number { | ||
return this.stack.length; | ||
} | ||
} |
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 @@ | ||
export * from './Stack'; |
File renamed without changes.