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
66 changes: 36 additions & 30 deletions packages/singly-linked-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ _I wholehearedly acknowledge that the basic data structure space is populated wi

## Basic Usage

Install with npm :
Install:

```bash
npm install singly-linked-list --save
npm install singly-linked-list
```

```bash
yarn add singly-linked-list
```

Basic usage example below. _Note: it does not cover all the available methods, rather just highlights the main functionality to get up and running with this data structure. For a description of all the methods, see the API section._

```javascript
var LinkedList = require('singly-linked-list');
var list = new LinkedList();
import LinkedList from 'singly-linked-list';
const list = new LinkedList();

list.isEmpty();
// --> true
Expand All @@ -47,8 +51,8 @@ list.insert('data item 4');
// 'data item 1', ... ,'data item 4'

// alternatively, the list can be initialized with an array
var initialData = ['data item 1', 'data item 2', 'data item 3', 'data item 4'];
var populatedList = new LinkedList(initialData);
const initialData = ['data item 1', 'data item 2', 'data item 3', 'data item 4'];
const populatedList = new LinkedList(initialData);
// populatedList contains:
// 'data item 1', ... ,'data item 4'

Expand Down Expand Up @@ -83,100 +87,102 @@ list.isEmpty();

**Available methods for a singly-linked-list instance:**

- ### getHeadNode()
- `getHeadNode()`

Returns the first node in the list

- ### getTailNode()
- `getTailNode()`

Returns the last node in the list

- ### isEmpty()
- `isEmpty()`

Determines if the list is empty or not. Returns true if is empty, false otherwise.

- ### getSize()
- `getSize()`

Returns the size of the list, or number of nodes

- ### clear()
- `clear()`

Clears the list of all nodes/data

- ### insert(data)
- `insert(data)`

Inserts a node (with the provided `data`) to the end of the list

- ### insertFirst(data)
- `insertFirst(data)`

Inserts a node (with the provided `data`) to the front of the list

- ### insertAt(index, data)
- `insertAt(index, data)`

Inserts a node (with the provided `data`) at the `index` indicated.

- ### insertBefore(nodeData, dataToInsert)
- `insertBefore(nodeData, dataToInsert)`

Inserts a node (with the `dataToInsert`) _before_ the first node containing `nodeData`

- ### insertAfter(nodeData, dataToInsert)
- `insertAfter(nodeData, dataToInsert)`

Inserts a node (with the `dataToInsert`) _after_ the first node containing `nodeData`

- ### remove()
- `remove()`

Removes the tail node from the list

- ### removeFirst()
- `removeFirst()`

Removes the head node from the list

- ### removeAt(index)
- `removeAt(index)`

Removes the node at the `index` provided

- ### removeNode(nodeData)
- `removeNode(nodeData)`

Removes the first node that contains the `nodeData` provided

- ### indexOf(nodeData)
- `indexOf(nodeData)`

Returns the index of the first node containing the provided `nodeData`. If a node cannot be found containing the provided data, null is returned.

- ### contains(nodeData)
- `contains(nodeData)`

Determines whether or not the list contains the provided `nodeData`

- ### find(nodeData)
- `find(nodeData)`

Returns the fist node containing the provided `nodeData`. If a node cannot be found containing the provided data, null is returned.

- ### findAt(index)
- `findAt(index)`

Returns the node at the location provided by `index`

- ### forEach(fn)
- `forEach(fn)`

Utility function to iterate over the list and call the `fn` provided on each node, or element, of the list

- ### toArray()
- `toArray()`

Returns an array of all the data contained in the list

- ### printList()
- `printList()`

Prints to the console the data property of each node in the list

**Available methods for an individual node instance:**

- ### getData()
- `getData()`

Returns the data of the the node

- ### hasNext()
- `hasNext()`

Returns whether or not the node has a pointer to the next node

- ### toString()
- `toString()`

Returns a string represenation of the node. If the data is an object, it returns the JSON.stringify version of the object. Otherwise, it simply returns the data

---
Expand Down
2 changes: 1 addition & 1 deletion packages/singly-linked-list/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "singly-linked-list",
"version": "1.4.1",
"version": "2.0.0",
"description": "Javascript implementation of singly linked list data structure",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
31 changes: 13 additions & 18 deletions packages/singly-linked-list/src/__tests__/list-node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import ListNode from '../lib/list-node';

describe('List Node', () => {
[null, undefined, '', 'test data', 42].forEach(data => {
it(`exists when instantiated with ${data}`, () => {
const node = new ListNode(data);
expect(node).toBeTruthy();
});
it.each([null, undefined, '', 'test data', 42])('exists when instantiated with %s', (data) => {
const node = new ListNode(data);
expect(node).toBeTruthy();
});

[
it.each( [
{
data: 'test data',
type: 'string'
Expand All @@ -21,13 +19,11 @@ describe('List Node', () => {
data: null,
type: 'object'
}
].forEach(testInput => {
it(`returns the correct (${testInput.type}) data for ${testInput.data}`, () => {
let node = new ListNode(testInput.data);
])('returns the correct $type data for $data', (input) => {
let node = new ListNode(input.data);
let data = node.getData();
expect(typeof data).toEqual(testInput.type);
expect(data).toEqual(testInput.data);
});
expect(typeof data).toEqual(input.type);
expect(data).toEqual(input.data);
});

it('returns the correct (object) data', () => {
Expand All @@ -52,7 +48,7 @@ describe('List Node', () => {
expect(firstNode.hasNext()).toBeFalsy();
});

[
it.each([
{
data: { name: 'test item', number: 1 },
expected: '{"name":"test item","number":1}'
Expand All @@ -65,10 +61,9 @@ describe('List Node', () => {
data: 42,
expected: '42'
}
].forEach(testInput => {
it(`returns a proper string representation for ${JSON.stringify(testInput.data)}`, () => {
const node = new ListNode(testInput.data);
expect(node.toString()).toEqual(testInput.expected);
});
])('returns a proper string for json data', (input) => {
const node = new ListNode(input.data);
expect(node.toString()).toEqual(input.expected);
});
});

17 changes: 13 additions & 4 deletions packages/singly-linked-list/src/__tests__/list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import LinkedList from '../index';

type TestObject = {
id?: number,
name?: string,
key?: string,
value?: string
};

type TestData = TestObject | string;

// Utility function to populate the list with dummy data.
// The number of nodes added will be specified by the 'numNodes'
// parameter.
const populateList = (aList: LinkedList, numNodes: number): void => {
const populateList = (aList: LinkedList<TestData>, numNodes: number): void => {
for (let i = 0; i < numNodes; i++) {
aList.insert('test item ' + (i + 1));
}
Expand All @@ -12,14 +21,14 @@ const populateList = (aList: LinkedList, numNodes: number): void => {
// Utility function to populate the list with dummy data.
// The number of nodes added will be specified by the 'numNodes'
// parameter.
const populateArray = (arr: any[], numItems: number): void => {
const populateArray = (arr: string[], numItems: number): void => {
for (let i = 0; i < numItems; i++) {
arr.push('test item ' + (i + 1));
}
};

describe('Linked List', () => {
let list: LinkedList;
let list: LinkedList<TestData>;

beforeEach(() => {
list = new LinkedList();
Expand Down Expand Up @@ -318,7 +327,7 @@ describe('Linked List', () => {
});

describe('pre-initialized list', () => {
let populatedList: LinkedList;
let populatedList: LinkedList<TestData>;

beforeEach(() => {
const arr: any[] = [];
Expand Down
Loading