Skip to content

Feature doubly linked list #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2019
Merged
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
13 changes: 13 additions & 0 deletions src/doubly-linked-list/Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type INodePointer = Node | null

export class Node {
public prev: Node | null
public next: Node | null
public value: any

constructor(value: any, prev: Node | null = null, next: Node | null = null) {
this.value = value
this.prev = prev
this.next = next
}
}
48 changes: 48 additions & 0 deletions src/doubly-linked-list/__tests__/Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Node } from '../Node'

describe('Doubly linked list Node', () => {
it('should create an node with value', () => {
const node = new Node(1)
expect(node).toBeDefined()
expect(node).toBeInstanceOf(Node)
expect(node.value).toBe(1)
expect(node.next).toBe(null)
expect(node.prev).toBe(null)
})

it('should create node with any value', () => {
const objectValue = { value: 1, text: 'object' }
const arrayValue = [1, 2, 3, 4]
const functionValue = () => {
/** do nothing */
}

const valueList = [objectValue, arrayValue, functionValue]

valueList.forEach(value => {
const newNode = new Node(value)
expect(newNode.value).toEqual(value)
})
})

it('should link nodes togather', () => {
const first = new Node(1)
const second = new Node(2, first)
const three = new Node(3, second, first)

expect(first).toBeDefined()
expect(first.next).toBe(null)
expect(first.prev).toBe(null)
expect(first.value).toBe(1)

expect(second).toBeDefined()
expect(second.next).toBe(null)
expect(second.prev).toBe(first)
expect(second.value).toBe(2)

expect(three).toBeDefined()
expect(three.next).toBe(first)
expect(three.prev).toBe(second)
expect(three.value).toBe(3)
})
})
Loading