{"action":"opened","issue":{"url":"https://api.github.com/repos/roojay520/Notes/issues/26","repository_url":"https://api.github.com/repos/roojay520/Notes","labels_url":"https://api.github.com/repos/roojay520/Notes/issues/26/labels{/name}","comments_url":"https://api.github.com/repos/roojay520/Notes/issues/26/comments","events_url":"https://api.github.com/repos/roojay520/Notes/issues/26/events","html_url":"https://github.com/roojay520/Notes/issues/26","id":388177308,"node_id":"MDU6SXNzdWUzODgxNzczMDg=","number":26,"title":"学习 JavaScript 数据结构与算法(day1): 栈","user":{"login":"roojay520","id":11495164,"node_id":"MDQ6VXNlcjExNDk1MTY0","avatar_url":"https://avatars3.githubusercontent.com/u/11495164?v=4","gravatar_id":"","url":"https://api.github.com/users/roojay520","html_url":"https://github.com/roojay520","followers_url":"https://api.github.com/users/roojay520/followers","following_url":"https://api.github.com/users/roojay520/following{/other_user}","gists_url":"https://api.github.com/users/roojay520/gists{/gist_id}","starred_url":"https://api.github.com/users/roojay520/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roojay520/subscriptions","organizations_url":"https://api.github.com/users/roojay520/orgs","repos_url":"https://api.github.com/users/roojay520/repos","events_url":"https://api.github.com/users/roojay520/events{/privacy}","received_events_url":"https://api.github.com/users/roojay520/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-12-06T11:44:46Z","updated_at":"2018-12-06T11:44:46Z","closed_at":null,"author_association":"OWNER","body":"> 栈是一种遵从后进先出（LIFO）原则的有序集合。新添加的或待删除的元素都保存在栈的\r\n同一端，称作栈顶，另一端就叫栈底。在栈里，新元素都靠近栈顶，旧元素都接近栈底。\r\n\r\nTS 版本:\r\n```typescript\r\n/**\r\n * @author roojay\r\n * @date 2018.12.6\r\n * @push(item(s)): 添加一个 (或几个) 新元素到栈顶.\r\n * @pop(): 移除栈顶的元素，同时返回被移除的元素.\r\n * @peek(): 返回栈顶的元素，不对栈做任何修改 (这个方法不会移除栈顶的元素，仅仅返回它) .\r\n * @isEmpty(): 如果栈里没有任何元素就返回 true，否则返回 false.\r\n * @clear(): 移除栈里的所有元素.\r\n * @size(): 返回栈里的元素个数.这个方法和数组的 length 属性很类似\r\n */\r\nclass Stack<T> {\r\n  private items: T[]\r\n\r\n  constructor() {\r\n    this.items = [];\r\n  }\r\n\r\n  // 添加元素\r\n  push(...item: T[]) {\r\n    this.items.push(...item)\r\n  }\r\n\r\n  // 移除元素\r\n  pop() {\r\n    this.items.pop()\r\n  }\r\n\r\n  // 返回栈顶的元素, 并不会实际删除\r\n  peek(): T {\r\n    return this.items.slice(-1)[0]\r\n  }\r\n\r\n  // 判断为空\r\n  isEmpty(): boolean {\r\n    return !this.items.length\r\n  }\r\n\r\n  // 清空栈\r\n  clear() {\r\n    this.items = []\r\n  }\r\n\r\n  // 返回栈的元素个数\r\n  size(): number {\r\n    return this.items.length\r\n  }\r\n\r\n  // 打印栈内元素\r\n  print() {\r\n    console.log(this.items)\r\n  }\r\n}\r\n\r\nconst numStack = new Stack<number>()\r\nnumStack.push(1, 2, 3, 5)\r\nnumStack.print()\r\nconsole.log(numStack.peek())\r\nconsole.log(numStack.size())\r\nnumStack.clear()\r\n\r\nconst strStack = new Stack<string>()\r\nstrStack.push('a')\r\nconsole.log(strStack.isEmpty())\r\nstrStack.print()\r\nnumStack.clear()\r\n```\r\n\r\n* JS 版本\r\n```javascript\r\nclass Stack {\r\n  constructor() {\r\n    this.items = [];\r\n  }\r\n\r\n  // 添加元素\r\n  push(...item) {\r\n    this.items.push(...item);\r\n  }\r\n\r\n  // 移除元素\r\n  pop() {\r\n    this.items.pop();\r\n  }\r\n\r\n  // 返回栈顶的元素, 并不会实际删除\r\n  peek() {\r\n    return this.items.slice(-1)[0];\r\n  }\r\n\r\n  // 判断为空\r\n  isEmpty() {\r\n    return !this.items.length;\r\n  }\r\n\r\n  // 清空栈\r\n  clear() {\r\n    this.items = [];\r\n  }\r\n\r\n  // 返回栈的元素个数\r\n  size() {\r\n    return this.items.length;\r\n  }\r\n\r\n  // 打印栈内元素\r\n  print() {\r\n    console.log(this.items);\r\n  }\r\n\r\n}\r\n\r\nconst stack = new Stack();\r\nstack.push(1, 2, 3, 5, 'a');\r\nstack.print();\r\nconsole.log(stack.peek());\r\nconsole.log(stack.size());\r\nstack.clear();\r\n```"}}
{"actor":{"display_login":"roojay520"}}