Skip to content

feat: linked-list implementation. #1

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 4 commits into from
Jan 7, 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"semantic-release": "semantic-release",
"semantic-release-prepare": "ts-node tools/semantic-release-prepare",
"precommit": "lint-staged",
"travis-deploy-once": "travis-deploy-once"
"travis-deploy-once": "travis-deploy-once",
"format": "prettier 'src/**/*.js' 'src/**/*.jsx' 'src/**/*.ts' 'src/**/*.tsx' 'src/**/*.scss' 'src/**/*.css' --write"
},
"lint-staged": {
"{src,test}/**/*.ts": [
Expand Down Expand Up @@ -116,4 +117,4 @@
"typescript": "^3.0.3",
"travis-deploy-once": "^5.0.9"
}
}
}
17 changes: 6 additions & 11 deletions src/linked-list/Node.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
export interface INode {
value: any;
next: INode | null;
}

export class Node implements INode {
public value: any;
public next: INode | null;
export class Node {
public value: any
public next: Node | null

constructor(value: any, next: INode | null = null) {
this.value = value;
this.next = next;
constructor(value: any, next: Node | null = null) {
this.value = value
this.next = next
}
}
13 changes: 11 additions & 2 deletions src/linked-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

## 功能

已完成:

- append
- prepend
- find
- delete
- deleteTail
- deleteHead
- isEmpty
- insert(TODO)
- toString
- toArray
- insert

## 复杂度

Expand All @@ -20,6 +27,8 @@ TODO(时间和空间复杂度)

## 问题

就目前实现来说,使用TypeScript来实现链表,在实例化链表并append一个值之后,TS并不能推导出链表head不为空,即使我们知道一定不为空。
1.链表是否需要length属性和引入索引(index)进行position定位(就像JS Array一样有index)

2.就目前实现来说,使用TypeScript来实现链表,在实例化链表并append一个值之后,TS并不能推导出链表head不为空,即使我们知道一定不为空。
即使在append方法通过一些方法解决这个问题,在append之后并delete这个值,TS还是不能推导出链表的head节点是否为空。
这个问题需要再一步思考:)
37 changes: 0 additions & 37 deletions src/linked-list/__tests__/Node.test.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/linked-list/__tests__/Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Node } from '../Node'

describe('Linked lise Node class', () => {
it('should create a list node with value', () => {
const newNode = new Node(1)
expect(newNode).toBeInstanceOf(Object)
expect(newNode.value).toBe(1)
expect(newNode.next).toBe(null)
})

it('shoule create a list node with Object 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 list nodes', () => {
const node2 = new Node(2)
const node1 = new Node(1, node2)

expect(node1.next).toBe(node2)
expect(node2.next).toBe(null)
expect(node1.value).toBe(1)
expect(node2.value).toBe(2)
})
})
Loading