Skip to content

Commit

Permalink
add basic ds (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscarz90 authored Sep 25, 2024
1 parent fe7bbb0 commit 5516b49
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/data-structure/src/index.ts
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';
49 changes: 49 additions & 0 deletions libs/data-structure/src/lib/Graph/Graph.ts
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');
}
}
9 changes: 9 additions & 0 deletions libs/data-structure/src/lib/Graph/Node.ts
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 = [];
}
}
2 changes: 2 additions & 0 deletions libs/data-structure/src/lib/Graph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Graph';
export * from './Node';
9 changes: 9 additions & 0 deletions libs/data-structure/src/lib/LinkedList/LinkedList.test.ts
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);
})
});
118 changes: 118 additions & 0 deletions libs/data-structure/src/lib/LinkedList/LinkedList.ts
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;
}
}
11 changes: 11 additions & 0 deletions libs/data-structure/src/lib/LinkedList/Node.ts
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;
}
}
1 change: 1 addition & 0 deletions libs/data-structure/src/lib/LinkedList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './LinkedList';
27 changes: 27 additions & 0 deletions libs/data-structure/src/lib/Queue/Queue.ts
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;
}
}
1 change: 1 addition & 0 deletions libs/data-structure/src/lib/Queue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Queue';
27 changes: 27 additions & 0 deletions libs/data-structure/src/lib/Stack/Stack.ts
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;
}
}
1 change: 1 addition & 0 deletions libs/data-structure/src/lib/Stack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Stack';

0 comments on commit 5516b49

Please sign in to comment.