Skip to content

feat: linked-list add length property #8

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 3 commits into from
Jan 8, 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
50 changes: 35 additions & 15 deletions src/linked-list/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@ describe('LinkedList class', () => {
expect(linkedList).toBeInstanceOf(Object)
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
expect(linkedList.length).toBe(0)
})

describe('Method append()', () => {
it('should be a chain method', () => {
linkedList
.append(1)
.append(2)
.append(3)
expect(linkedList.head).toBeDefined()
expect(linkedList.tail).toBeDefined()
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(3)
})

it('should append a node to linkedlist', () => {
expect(linkedList.length).toBe(0)
linkedList.append(1)
expect(linkedList.length).toBe(1)

expect(linkedList.head).toBeDefined()
expect(linkedList.head).toBeInstanceOf(Node)
Expand All @@ -44,6 +36,18 @@ describe('LinkedList class', () => {
expect(linkedList.head).toBe(linkedList.tail)
})

it('should be a chain method', () => {
linkedList
.append(1)
.append(2)
.append(3)
expect(linkedList.head).toBeDefined()
expect(linkedList.tail).toBeDefined()
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(3)
expect(linkedList.length).toBe(3)
})

it('should append 2 values be linked', () => {
linkedList.append(1).append(2)

Expand Down Expand Up @@ -90,6 +94,7 @@ describe('LinkedList class', () => {
linkedList.prepend(2)

expect(linkedList.toString()).toBe('2,1')
expect(linkedList.length).toBe(2)
})

it('should handle head & tail correctly', () => {
Expand All @@ -102,6 +107,7 @@ describe('LinkedList class', () => {
expect(linkedList.toString()).toBe('1,2,3,4,5')
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(5)
expect(linkedList.length).toBe(5)
})
})

Expand Down Expand Up @@ -152,6 +158,7 @@ describe('LinkedList class', () => {

it('should return null if the list is empty()', () => {
expect(linkedList.delete(1)).toBe(null)
expect(linkedList.length).toBe(0)
})

it('should delete nothing if not found value', () => {
Expand All @@ -162,6 +169,7 @@ describe('LinkedList class', () => {

const deletedNode = linkedList.delete(6)
expect(deletedNode).toBe(null)
expect(linkedList.length).toBe(3)
expect((linkedList.head as Node).value).toBe(1)
expect(((linkedList.head as Node).next as Node).value).toBe(2)
expect((linkedList.tail as Node).value).toBe(3)
Expand All @@ -176,6 +184,7 @@ describe('LinkedList class', () => {

const deletedNode = linkedList.delete(3)
expect((deletedNode as Node).value).toBe(3)
expect(linkedList.length).toBe(2)
expect((linkedList.head as Node).value).toBe(1)
expect(((linkedList.head as Node).next as Node).value).toBe(2)
expect((linkedList.tail as Node).value).toBe(2)
Expand All @@ -184,6 +193,7 @@ describe('LinkedList class', () => {
linkedList.append(3)

const deletedNode2 = linkedList.delete(1)
expect(linkedList.length).toBe(2)
expect((deletedNode2 as Node).value).toBe(1)
expect((linkedList.head as Node).value).toBe(2)
expect((linkedList.tail as Node).value).toBe(3)
Expand All @@ -194,6 +204,7 @@ describe('LinkedList class', () => {

expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
expect(linkedList.length).toBe(0)
})
})

Expand All @@ -207,7 +218,7 @@ describe('LinkedList class', () => {
.append(5)
expect((linkedList.deleteHead() as Node).value).toBe(1)
expect(linkedList.toString()).toBe('2,3,4,5')

expect(linkedList.length).toBe(4)
expect((linkedList.head as Node).value).toBe(2)
expect((linkedList.tail as Node).value).toBe(5)
})
Expand All @@ -228,6 +239,7 @@ describe('LinkedList class', () => {
it('should delete tail node and handle head & tail correctly', () => {
linkedList.append(1).deleteTail()
expect(linkedList.toString()).toBe('')
expect(linkedList.length).toBe(0)
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)

Expand All @@ -238,16 +250,15 @@ describe('LinkedList class', () => {
.append(4)
.append(5)
linkedList.deleteTail()
expect(linkedList.length).toBe(4)
expect(linkedList.toString()).toBe('1,2,3,4')
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(4)
})

it('should return null when list is empty', () => {
expect(linkedList.deleteTail()).toBe(null)
linkedList.append(1)
linkedList.append(2)
expect((linkedList.deleteHead() as Node).value).toBe(1)
expect(linkedList.length).toBe(0)
})
})

Expand Down Expand Up @@ -279,6 +290,7 @@ describe('LinkedList class', () => {
linkedList.insert(5, 1)

expect(linkedList.toString()).toBe('1,5,2,1,3')
expect(linkedList.length).toBe(5)
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(3)
})
Expand All @@ -291,6 +303,7 @@ describe('LinkedList class', () => {
.append(3)
linkedList.insert(5, 3)

expect(linkedList.length).toBe(5)
expect(linkedList.toString()).toBe('1,2,1,3,5')
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(5)
Expand All @@ -304,6 +317,7 @@ describe('LinkedList class', () => {
.append(3)
linkedList.insert(5, 1, 'before')

expect(linkedList.length).toBe(5)
expect(linkedList.toString()).toBe('5,1,2,1,3')
expect((linkedList.head as Node).value).toBe(5)
expect((linkedList.tail as Node).value).toBe(3)
Expand All @@ -317,6 +331,7 @@ describe('LinkedList class', () => {
.append(3)
linkedList.insert(5, 3, 'before')

expect(linkedList.length).toBe(5)
expect(linkedList.toString()).toBe('1,2,1,5,3')
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(3)
Expand All @@ -329,6 +344,7 @@ describe('LinkedList class', () => {
.append(3)
linkedList.insert(5, 4, 'before')

expect(linkedList.length).toBe(3)
expect(linkedList.toString()).toBe('1,2,3')
})

Expand All @@ -337,11 +353,13 @@ describe('LinkedList class', () => {
expect(linkedList.toString()).toBe('')
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
expect(linkedList.length).toBe(0)

linkedList.insert(5, 3, 'before')
expect(linkedList.toString()).toBe('')
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
expect(linkedList.length).toBe(0)
})
})

Expand All @@ -354,6 +372,7 @@ describe('LinkedList class', () => {
const reversaledList = linkedList.reversal()
expect(reversaledList).toBeDefined()
expect(reversaledList).toBeInstanceOf(LinkedList)
expect(linkedList.length).toBe(3)
expect(reversaledList.toString()).toBe('3,2,1')
expect((reversaledList.head as Node).value).toBe(3)
expect((reversaledList.tail as Node).value).toBe(1)
Expand All @@ -366,6 +385,7 @@ describe('LinkedList class', () => {
expect(reversaledList.toString()).toBe('')
expect(reversaledList.head).toBe(null)
expect(reversaledList.tail).toBe(null)
expect(linkedList.length).toBe(0)
})
})
})
18 changes: 18 additions & 0 deletions src/linked-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const stringifyDefault = (value: any) => {
export class LinkedList {
public head: Node | null
public tail: Node | null
private _length: number

constructor() {
this.head = null
this.tail = null
this._length = 0
}

public get length() {
return this._length
}

/**
Expand All @@ -19,6 +25,8 @@ export class LinkedList {
* @returns {LinkedList} 当前链表
*/
public append(value: any): LinkedList {
this._length++

const newNode = new Node(value)

if (!this.head) {
Expand All @@ -42,6 +50,8 @@ export class LinkedList {
* @returns {LinkedList} 当前链表
*/
public prepend(value: any): LinkedList {
this._length++

const newNode = new Node(value)

if (!this.head) {
Expand Down Expand Up @@ -88,6 +98,7 @@ export class LinkedList {
}

if (currentNode) {
this._length++
const newNode = new Node(value, currentNode.next)
if (currentNode === this.tail) {
this.tail = newNode
Expand All @@ -99,6 +110,7 @@ export class LinkedList {

if (currentNode) {
if (targetValue === currentNode.value) {
this._length++
const newNode = new Node(value, this.head)
this.head = newNode
} else {
Expand All @@ -113,6 +125,7 @@ export class LinkedList {
}

if (prevNode) {
this._length++
const newNode = new Node(value, prevNode.next)
prevNode.next = newNode
}
Expand All @@ -139,6 +152,7 @@ export class LinkedList {
// 查看head是不是需要删除的node
while (this.head && this.head.value === value) {
deletedNode = this.head
this._length--
this.head = this.head.next
}

Expand All @@ -148,6 +162,7 @@ export class LinkedList {
while (currentNode !== null && currentNode.next) {
if (currentNode.next.value === value) {
deletedNode = currentNode.next
this._length--
currentNode.next = currentNode.next.next
} else {
currentNode = currentNode.next
Expand All @@ -171,6 +186,7 @@ export class LinkedList {
const headNode = this.head

if (headNode !== null) {
this._length--
deletedNode = headNode
if (this.head === this.tail) {
this.head = null
Expand All @@ -193,13 +209,15 @@ export class LinkedList {
if (this.head !== null) {
if (this.head === this.tail) {
deletedNode = this.head
this._length--
this.head = null
this.tail = null
} else {
let currentNode = this.head
while (currentNode.next !== null) {
if (currentNode.next.next === null) {
deletedNode = currentNode.next.next
this._length--
currentNode.next = null
} else {
currentNode = currentNode.next
Expand Down