Skip to content

feat: stack implementation with array #5

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 1 commit 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
3 changes: 3 additions & 0 deletions src/stack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 栈

栈(stack),也称堆栈,在计算机科学领域中,栈是一种线性数据结构,它只能在数据串列或阵列的一端进行入栈(push)和出栈(pop)操作,按照后进先出(LIFO)原理进行运作
71 changes: 71 additions & 0 deletions src/stack/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Stack } from '../index'

describe('Stack', () => {
let stack: Stack

beforeEach(() => {
stack = new Stack()
})

it('should create a empty Stack', () => {
const stack: Stack = new Stack()
expect(stack).toBeDefined()
expect(stack).toBeInstanceOf(Stack)
expect(stack.toString()).toBe('')
})

it('shoule push value', () => {
/** */
stack.push(1)
expect(stack.toString()).toBe('1')
stack.push(1)
expect(stack.toString()).toBe('1,1')
})

it('shoule pop value', () => {
stack.push(1)
expect(stack.toString()).toBe('1')
stack.pop()
expect(stack.toString()).toBe('')
})

it('shoule push/pop value in LIFO order', () => {
stack.push(1)
stack.push(2)
stack.push(3)
expect(stack.toString()).toBe('1,2,3')
expect(stack.pop()).toBe(3)
expect(stack.toString()).toBe('1,2')
expect(stack.pop()).toBe(2)
expect(stack.toString()).toBe('1')
expect(stack.pop()).toBe(1)
expect(stack.toString()).toBe('')
})

it('shoule return isEmpty', () => {
expect(stack.isEmpty()).toBeTruthy()
stack.push(1)
expect(stack.isEmpty()).toBeFalsy()
stack.push(2)
expect(stack.isEmpty()).toBeFalsy()
stack.pop()
stack.pop()
expect(stack.isEmpty()).toBeTruthy()
})

it('shoule set size correctly', () => {
expect(stack.size).toBe(0)
stack.push(1)
expect(stack.size).toBe(1)
stack.push(1)
expect(stack.size).toBe(2)
stack.pop()
stack.pop()
expect(stack.size).toBe(0)
})

it('shoule return null when list is empty', () => {
expect(stack.isEmpty()).toBeTruthy()
expect(stack.pop()).toBe(null)
})
})
34 changes: 34 additions & 0 deletions src/stack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 栈的数组实现
*/

export class Stack {
private list: any[]

constructor() {
this.list = []
}

public get size() {
return this.list.length
}

public push(item: any) {
this.list.push(item)
}

public pop() {
if (this.list.length === 0) {
return null
}
return this.list.pop()
}

public isEmpty(): boolean {
return this.list.length === 0
}

public toString(): string {
return this.list.toString()
}
}