Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Node } from './node';

export class CircularLinkedList<T> {
private head: Node<T> | null;
private tail: Node<T> | null;

constructor() {
this.head = null;
this.tail = null;
}

add(data: T): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
newNode.next = this.head;
} else {
if (this.tail) {
this.tail.next = newNode;
}
this.tail = newNode;
this.tail.next = this.head;
}
}

toArray(): T[] {
const result: T[] = [];
if (!this.head) return result;

let current: Node<T> | null = this.head;
do {
result.push(current.data);
current = current.next;
} while (current && current !== this.head);

return result;
}

getHead(): Node<T> | null {
return this.head;
}
}
9 changes: 9 additions & 0 deletions src/modules/linked-list/circular-linked-list/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Node<T> {
data: T;
next: Node<T> | null;

constructor(data: T) {
this.data = data;
this.next = null;
}
}
35 changes: 35 additions & 0 deletions src/modules/linked-list/doubly-linked-list/doubly-linked-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Node } from './node';

export class DoublyLinkedList<T> {
private head: Node<T> | null;
private tail: Node<T> | null;

constructor() {
this.head = null;
this.tail = null;
}

add(data: T): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
if (this.tail) {
this.tail.next = newNode;
}
this.tail = newNode;
}
}

toArray(): T[] {
const result: T[] = [];
let current = this.head;
while (current) {
result.push(current.data);
current = current.next;
}
return result;
}
}
11 changes: 11 additions & 0 deletions src/modules/linked-list/doubly-linked-list/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class Node<T> {
data: T;
next: Node<T> | null;
prev: Node<T> | null;

constructor(data: T) {
this.data = data;
this.next = null;
this.prev = null;
}
}
9 changes: 9 additions & 0 deletions src/modules/linked-list/singly-linked-list/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Node<T> {
data: T;
next: Node<T> | null;

constructor(data: T) {
this.data = data;
this.next = null;
}
}
33 changes: 33 additions & 0 deletions src/modules/linked-list/singly-linked-list/singly-linked-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Node } from './node';

export class SinglyLinkedList<T> {
private head: Node<T> | null;

constructor() {
this.head = null;
}

add(data: T): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}

toArray(): T[] {
const result: T[] = [];
let current = this.head;
while (current) {
result.push(current.data);
current = current.next;
}
return result;
}
}

9 changes: 9 additions & 0 deletions src/modules/linked-list/xor-linked-list/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Node<T> {
data: T;
xorPointer: number;

constructor(data: T) {
this.data = data;
this.xorPointer = 0;
}
}
67 changes: 67 additions & 0 deletions src/modules/linked-list/xor-linked-list/xor-linked-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Node } from './node';

function xor(a: number, b: number): number {
return a ^ b;
}

export class XORLinkedList<T> {
private head: Node<T> | null;
private tail: Node<T> | null;
private nodes: Map<number, Node<T>>;
private idCounter: number;

constructor() {
this.head = null;
this.tail = null;
this.nodes = new Map<number, Node<T>>();
this.idCounter = 1;
}

private getNodeId(node: Node<T> | null): number {
if (!node) return 0;
for (let [id, n] of this.nodes.entries()) {
if (n === node) return id;
}
const newId = this.idCounter++;
this.nodes.set(newId, node);
return newId;
}

private getNodeById(id: number): Node<T> | null {
return this.nodes.get(id) || null;
}

add(data: T): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
const tailId = this.getNodeId(this.tail);
newNode.xorPointer = tailId;
if (this.tail) {
this.tail.xorPointer = xor(this.tail.xorPointer, this.getNodeId(newNode));
}
this.tail = newNode;
}
this.getNodeId(newNode);
}

toArray(): T[] {
const result: T[] = [];
if (!this.head) return result;

let current: Node<T> | null = this.head;
let prevId = 0;
let nextId: number;

while (current !== null) {
result.push(current.data);
nextId = xor(prevId, current.xorPointer);
prevId = this.getNodeId(current);
current = this.getNodeById(nextId);
}

return result;
}
}
37 changes: 37 additions & 0 deletions tests/circular-linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CircularLinkedList } from '../src/modules/linked-list/circular-linked-list/circular-linked-list';
import { Node } from '../src/modules/linked-list/circular-linked-list/node';

describe('CircularLinkedList', () => {
it('should initialize empty', () => {
const list = new CircularLinkedList<number>();
expect(list.toArray()).toEqual([]);
});

it('should add elements', () => {
const list = new CircularLinkedList<number>();
list.add(1);
list.add(2);
list.add(3);
expect(list.toArray()).toEqual([1, 2, 3]);
});

it('should maintain circular link', () => {
const list = new CircularLinkedList<number>();
list.add(1);
list.add(2);
list.add(3);

const head = list.getHead();
if (head) {
let current = head;
let count = 0;
do {
current = current.next as Node<number>;
count++;
} while (current && current !== head && count <= 3);

expect(count).toEqual(3);
expect(current).toBe(head);
}
});
});
16 changes: 16 additions & 0 deletions tests/doubly-linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DoublyLinkedList } from '../src/modules/linked-list/doubly-linked-list/doubly-linked-list';

describe('DoublyLinkedList', () => {
it('should initialize empty', () => {
const list = new DoublyLinkedList<number>();
expect(list.toArray()).toEqual([]);
});

it('should add elements', () => {
const list = new DoublyLinkedList<number>();
list.add(1);
list.add(2);
list.add(3);
expect(list.toArray()).toEqual([1, 2, 3]);
});
});
16 changes: 16 additions & 0 deletions tests/singly-linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SinglyLinkedList } from '../src/modules/linked-list/singly-linked-list/singly-linked-list';

describe('SinglyLinkedList', () => {
it('should initialize empty', () => {
const list = new SinglyLinkedList<number>();
expect(list.toArray()).toEqual([]);
});

it('should add elements', () => {
const list = new SinglyLinkedList<number>();
list.add(1);
list.add(2);
list.add(3);
expect(list.toArray()).toEqual([1, 2, 3]);
});
});
16 changes: 16 additions & 0 deletions tests/xor-linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { XORLinkedList } from '../src/modules/linked-list/xor-linked-list/xor-linked-list';

describe('XORLinkedList', () => {
it('should initialize empty', () => {
const list = new XORLinkedList<number>();
expect(list.toArray()).toEqual([]);
});

it('should add elements', () => {
const list = new XORLinkedList<number>();
list.add(1);
list.add(2);
list.add(3);
expect(list.toArray()).toEqual([1, 2, 3]);
});
});