Skip to content

Commit

Permalink
refactor(linkedlist): use array-link push/pop syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagraber committed Aug 28, 2023
1 parent c11748e commit 525cf9a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
19 changes: 10 additions & 9 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
"sourceType": "module"
},
"rules": {
"semi": "off",
"@typescript-eslint/semi": "off",
"member-delimiter-style": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/member-delimiter-style": "off",
"prefer-function-type": "off",
"@typescript-eslint/prefer-readonly": "off",
"@typescript-eslint/semi": "off",
"@typescript-eslint/space-before-function-paren": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"explicit-function-return-type": "off",
"generator-star-spacing": "off",
"member-delimiter-style": "off",
"no-eq-null": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"strict-boolean-expressions": "off",
"no-template-curly-in-string": "off",
"explicit-function-return-type": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/ban-ts-comment": "off"
"prefer-function-type": "off",
"semi": "off",
"strict-boolean-expressions": "off"
}
}
21 changes: 10 additions & 11 deletions src/data-structures/sequences/linked-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface List<T> {
class LinkedList<T> implements Iterable<T> {
private list: List<T> | undefined;

private readonly equalsImpl: TypedEqualityFunction<T> = equals;
private equalsImpl: TypedEqualityFunction<T> = equals;

/**
* Creates a LinkedList ~ O(1)
Expand All @@ -35,7 +35,6 @@ class LinkedList<T> implements Iterable<T> {
* Returns size ~ O(1)
*/
size(): number {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (this.list) return this.list.size;
return 0;
}
Expand All @@ -52,7 +51,7 @@ class LinkedList<T> implements Iterable<T> {
*/
fromArray(arr: T[]): LinkedList<T> {
for (const val of arr) {
this.addBack(val);
this.push(val);
}
return this;
}
Expand All @@ -75,7 +74,7 @@ class LinkedList<T> implements Iterable<T> {
* @param {T} val - value to add to head of list
* @return {boolean}
*/
addFront(val: T): boolean {
unshift(val: T): boolean {
const newNode = new LinkedListNode(val);

if (this.list) {
Expand All @@ -100,7 +99,7 @@ class LinkedList<T> implements Iterable<T> {
* @param {T} val - value to add to tail of list
* @return {boolean}
*/
addBack(val: T): boolean {
push(val: T): boolean {
const newNode = new LinkedListNode(val);

if (this.list) {
Expand Down Expand Up @@ -128,11 +127,11 @@ class LinkedList<T> implements Iterable<T> {
*/
addAt(i: number, val: T): boolean {
if (i === 0) {
return this.addFront(val);
return this.unshift(val);
}

if (i === this.size()) {
return this.addBack(val);
return this.push(val);
}

if (i < 0 || i >= this.size() || !this.list) return false;
Expand Down Expand Up @@ -232,7 +231,7 @@ class LinkedList<T> implements Iterable<T> {
* Removes node at head ~ O(1)
* @return {T | null} - value of removed head node if list is defined
*/
removeFront(): T | null {
shift(): T | null {
if (!this.list) return null;

// extract val of head so we can return it later
Expand All @@ -257,7 +256,7 @@ class LinkedList<T> implements Iterable<T> {
* Removes node at tail ~ O(1)
* @return {T | null} - value of removed head
*/
removeBack(): T | null {
pop(): T | null {
if (!this.list) return null;

// extract the val of tail so we can return it later
Expand Down Expand Up @@ -286,9 +285,9 @@ class LinkedList<T> implements Iterable<T> {
if (!this.list) return null;

if (i === 0) {
return this.removeFront();
return this.shift();
} else if (i === this.size() - 1) {
return this.removeBack();
return this.pop();
}

if (i < 0 || i >= this.list.size) return null;
Expand Down
98 changes: 49 additions & 49 deletions test/data-structures/sequences/linked-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Linked List - simple number', () => {

list = new LinkedList(equals);

list.addFront(1);
list.unshift(1);

expect(list.size()).toBe(1);
expect(list.indexOf(1)).toBe(0);
Expand All @@ -28,8 +28,8 @@ describe('Linked List - simple number', () => {
it('Accessing and removal methods return null on empty lists', () => {
expect(list.peekFront()).toBe(null);
expect(list.peekBack()).toBe(null);
expect(list.removeFront()).toBe(null);
expect(list.removeBack()).toBe(null);
expect(list.shift()).toBe(null);
expect(list.pop()).toBe(null);
expect(list.remove(3)).toBe(null);
expect(list.removeAt(3)).toBe(null);
expect(list.removeAt(-1)).toBe(null);
Expand All @@ -39,21 +39,21 @@ describe('Linked List - simple number', () => {

describe('Adding nodes', () => {
it('adds node to head of list', () => {
list.addFront(8);
list.unshift(8);
expect(list.size()).toBe(1);
expect(list.indexOf(8)).toBe(0);

list.addFront(3);
list.unshift(3);
expect(list.size()).toBe(2);
expect(list.indexOf(3)).toBe(0);
});

it('adds to tail of list', () => {
list.addBack(8);
list.push(8);
expect(list.size()).toBe(1);
expect(list.indexOf(8)).toBe(0);

list.addBack(3);
list.push(3);
expect(list.size()).toBe(2);
expect(list.indexOf(3)).toBe(list.size() - 1);
});
Expand Down Expand Up @@ -81,16 +81,16 @@ describe('Linked List - simple number', () => {
});

it('returns false when adding at index out of bounds', () => {
list.addFront(1);
list.unshift(1);
expect(list.addAt(3, 2)).toBe(false);
});
});

describe('Finding nodes', () => {
it('gets nodes', () => {
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.push(1);
list.push(2);
list.push(3);

expect(list.get(0)).toBe(1);
expect(list.get(1)).toBe(2);
Expand All @@ -100,9 +100,9 @@ describe('Linked List - simple number', () => {
it('gets index of nodes', () => {
expect(list.indexOf(1)).toBe(-1);

list.addBack(1);
list.addBack(2);
list.addBack(3);
list.push(1);
list.push(2);
list.push(3);

expect(list.indexOf(1)).toBe(0);
expect(list.indexOf(2)).toBe(1);
Expand All @@ -112,64 +112,64 @@ describe('Linked List - simple number', () => {

it('finds out if list contains node', () => {
expect(list.contains(1)).toBe(false);
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.push(1);
list.push(2);
list.push(3);

expect(list.contains(1)).toBe(true);
expect(list.contains(3)).toBe(true);
expect(list.contains(8)).toBe(false);
});

it('peeks head', () => {
list.addFront(1);
list.unshift(1);
expect(list.peekFront()).toBe(1);

list.addFront(2);
list.unshift(2);
expect(list.peekFront()).toBe(2);
});

it('peeks tail', () => {
list.addBack(1);
list.push(1);
expect(list.peekBack()).toBe(1);

list.addBack(2);
list.push(2);
expect(list.peekBack()).toBe(2);
});
});

describe('Removing nodes', () => {
it('removes from head', () => {
list.addBack(8);
list.addBack(3);
list.push(8);
list.push(3);

list.removeFront();
list.shift();
expect(list.size()).toBe(1);
expect(list.peekFront()).toBe(3);

list.removeFront();
list.shift();
expect(list.size()).toBe(0);
});

it('removes from tail', () => {
list.addBack(8);
list.addBack(3);
list.push(8);
list.push(3);

list.removeBack();
list.pop();
expect(list.size()).toBe(1);
expect(list.peekFront()).toBe(8);

list.removeBack();
list.pop();
expect(list.size()).toBe(0);
});

it('removes at specific index', () => {
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.addBack(4);
list.addBack(5);
list.addBack(6);
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
list.push(6);

const val = list.removeAt(0);
expect(val).toBe(1);
Expand Down Expand Up @@ -197,9 +197,9 @@ describe('Linked List - simple number', () => {
});

it('returns null when attempting to remove out of bounds node', () => {
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.push(1);
list.push(2);
list.push(3);

const val1 = list.removeAt(4);
expect(val1).toBe(null);
Expand All @@ -211,9 +211,9 @@ describe('Linked List - simple number', () => {
});

it('removes node with specific value', () => {
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.push(1);
list.push(2);
list.push(3);

const val1 = list.remove(1);
expect(val1).toBe(1);
Expand All @@ -229,14 +229,14 @@ describe('Linked List - simple number', () => {
});

it('clears the list', () => {
list.addBack(1);
list.addBack(2);
list.addBack(3);
list.addBack(4);
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.clear();
expect(list.isEmpty()).toBe(true);

list.addFront(1);
list.unshift(1);
list.clear();
expect(list.isEmpty()).toBe(true);

Expand Down Expand Up @@ -300,9 +300,9 @@ describe('Linked list - complex object', () => {

list = new LinkedList();

list.addBack(peugeot);
list.addBack(ferrari);
list.addBack(honda);
list.push(peugeot);
list.push(ferrari);
list.push(honda);
});

it('gets the index of a particular car', () => {
Expand Down

0 comments on commit 525cf9a

Please sign in to comment.