Skip to content

Commit

Permalink
organize ll methods
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 23, 2023
1 parent fdf8f20 commit b24936c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 77 deletions.
51 changes: 25 additions & 26 deletions src/DataStructures/LinkedList.js
Expand Up @@ -57,6 +57,19 @@ export class LinkedList {
return true;
}

// ADD a new value to the START of the list
unshift(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;

if (!this.tail) this.tail = newNode;

this.size++;

return this;
}

// ADD a new value to the END of the list
push(value) {
const newNode = new Node(value);
Expand All @@ -72,23 +85,23 @@ export class LinkedList {
return this;
}

// ADD a new value to the START of the list
unshift(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
// ADD a new value to the given index of the list
insert(value, index) {
return this;
}

if (!this.tail) this.tail = newNode;
// REMOVE a value to the START of the list
shift() {
if (!this.size) return undefined;

this.size++;
const removedValue = this.head.value;

return this;
}
this.head = this.head.next;

this.size--;

// ADD a new value to the given index of the list
insert(value, index) {
return this;
if (this.size === 0) this.clear();
return removedValue;
}

// REMOVE a value from the END of the list
Expand All @@ -111,20 +124,6 @@ export class LinkedList {
return temp.value;
}

// REMOVE a value to the START of the list
shift() {
if (!this.size) return undefined;

const removedValue = this.head.value;

this.head = this.head.next;

this.size--;

if (this.size === 0) this.clear();
return removedValue;
}

// REMOVE a value in the given index of the list
remove(value, index) {
return this;
Expand Down
102 changes: 51 additions & 51 deletions tests/DataStructures/LinkedList.test.js
Expand Up @@ -120,26 +120,6 @@ describe("LinkedList.js", () => {
});
});

describe("push()", () => {
it("adds items to the end of the linked list", () => {
const ll = new LinkedList(100);

expect(ll.push(50)).toBeInstanceOf(LinkedList);
expect(ll.size).toBe(2);
expect(ll.head.value).toBe(100);
expect(ll.head.next.value).toBe(50);
expect(ll.tail.value).toBe(50);

ll.push(25);
expect(ll.size).toBe(3);
expect(ll.head.value).toBe(100);
expect(ll.head.next.next.value).toBe(25);
expect(ll.tail.value).toBe(25);

expect(ll.toArray()).toEqual([100, 50, 25]);
});
});

describe("unshift()", () => {
it("should add a new element at the start", () => {
const ll = new LinkedList(0);
Expand Down Expand Up @@ -179,45 +159,23 @@ describe("LinkedList.js", () => {
});
});

describe("pop()", () => {
it("remove the last element from the linked list", () => {
describe("push()", () => {
it("adds items to the end of the linked list", () => {
const ll = new LinkedList(100);
ll.push(50);
ll.push(25);
ll.push(13);

expect(ll.pop()).toBe(13);
expect(ll.size).toBe(3);
expect(ll.head.value).toBe(100);
expect(ll.tail.value).toBe(25);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100, 50, 25]);

expect(ll.pop()).toBe(25);
expect(ll.push(50)).toBeInstanceOf(LinkedList);
expect(ll.size).toBe(2);
expect(ll.head.value).toBe(100);
expect(ll.head.next.value).toBe(50);
expect(ll.tail.value).toBe(50);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100, 50]);

expect(ll.pop()).toBe(50);
expect(ll.size).toBe(1);
ll.push(25);
expect(ll.size).toBe(3);
expect(ll.head.value).toBe(100);
expect(ll.tail.value).toBe(100);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100]);

expect(ll.pop()).toBe(100);
expect(ll.size).toBe(0);
expect(ll.head).toBe(null);
expect(ll.tail).toBe(null);
expect(ll.toArray()).toEqual([]);
expect(ll.head.next.next.value).toBe(25);
expect(ll.tail.value).toBe(25);

expect(ll.pop()).toBe(undefined);
expect(ll.size).toBe(0);
expect(ll.head).toBe(null);
expect(ll.tail).toBe(null);
expect(ll.toArray()).toEqual([]);
expect(ll.toArray()).toEqual([100, 50, 25]);
});
});

Expand Down Expand Up @@ -257,6 +215,48 @@ describe("LinkedList.js", () => {
});
});

describe("pop()", () => {
it("remove the last element from the linked list", () => {
const ll = new LinkedList(100);
ll.push(50);
ll.push(25);
ll.push(13);

expect(ll.pop()).toBe(13);
expect(ll.size).toBe(3);
expect(ll.head.value).toBe(100);
expect(ll.tail.value).toBe(25);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100, 50, 25]);

expect(ll.pop()).toBe(25);
expect(ll.size).toBe(2);
expect(ll.head.value).toBe(100);
expect(ll.tail.value).toBe(50);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100, 50]);

expect(ll.pop()).toBe(50);
expect(ll.size).toBe(1);
expect(ll.head.value).toBe(100);
expect(ll.tail.value).toBe(100);
expect(ll.tail.next).toBe(null);
expect(ll.toArray()).toEqual([100]);

expect(ll.pop()).toBe(100);
expect(ll.size).toBe(0);
expect(ll.head).toBe(null);
expect(ll.tail).toBe(null);
expect(ll.toArray()).toEqual([]);

expect(ll.pop()).toBe(undefined);
expect(ll.size).toBe(0);
expect(ll.head).toBe(null);
expect(ll.tail).toBe(null);
expect(ll.toArray()).toEqual([]);
});
});

describe("toArray()", () => {
it("should return an array with the values of the linked list", () => {
const ll = new LinkedList(0);
Expand Down

0 comments on commit b24936c

Please sign in to comment.