diff --git a/modulo8/Estrutura-de-dados/package.json b/modulo8/Estrutura-de-dados/package.json new file mode 100644 index 0000000..b5b330c --- /dev/null +++ b/modulo8/Estrutura-de-dados/package.json @@ -0,0 +1,14 @@ +{ + "scripts": { + "array": "clear && tsnd --transpile-only --ignore-watch node_modules ./src/Array", + "linked-list": "clear && tsnd --transpile-only --ignore-watch node_modules ./src/LinkedList", + "stack": "clear && tsnd --transpile-only --ignore-watch node_modules ./src/Stack", + "queue": "clear && tsnd --transpile-only --ignore-watch node_modules ./src/Queue" + }, + "devDependencies": { + "@types/node": "^15.3.0", + "ts-node-dev": "^1.1.6", + "typescript": "^4.2.4" + } + } + \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/src/Array/index.ts b/modulo8/Estrutura-de-dados/src/Array/index.ts new file mode 100644 index 0000000..0e4fe39 --- /dev/null +++ b/modulo8/Estrutura-de-dados/src/Array/index.ts @@ -0,0 +1,50 @@ +const labenuGroups: string[] = [ + "Newton", + "Bouman", + "Sagan", + "Hamilton", + "Julian", + "Melo", + "Turing", + "Jackson", + "Tang", + "Dumont " + ] + + + // const matriz = [ + // [1,0], + // [0,1], + // [0,0] + // ] + + // for(let linha of matriz){ + // for(let coluna of linha){ + // // lógica + // } + // } + + + const customSplice = ( + array: any[], + index: number, + deleteCount: number = 1 + ): any => { + const removedItems = [] + + for (let i = index; i < index + deleteCount; i++) { + removedItems.push(array[i]) + } + + for (let i = index; i < array.length; i++) { + array[i] = array[i + deleteCount] + } + + array.length -= deleteCount + + return removedItems + } + + const item = customSplice(labenuGroups, 1, 3) + + // console.log({ labenuGroups, item }); \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/src/LinkedList/ListNode.ts b/modulo8/Estrutura-de-dados/src/LinkedList/ListNode.ts new file mode 100644 index 0000000..ee9c675 --- /dev/null +++ b/modulo8/Estrutura-de-dados/src/LinkedList/ListNode.ts @@ -0,0 +1,10 @@ +export class ListNode { + constructor( + public value: any, + public next: ListNode | null = null + ) { } + + + } + + export const node = new ListNode("segunda", new ListNode("terça")) \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/src/LinkedList/index.ts b/modulo8/Estrutura-de-dados/src/LinkedList/index.ts new file mode 100644 index 0000000..f2792a7 --- /dev/null +++ b/modulo8/Estrutura-de-dados/src/LinkedList/index.ts @@ -0,0 +1,49 @@ +import { ListNode, node } from "./ListNode"; + +export class LinkedList { + constructor( + public start: ListNode | null = null + ) { } + + public addToTail = ( + value: any + ): void | undefined => { + + if (!this.start) { + this.start = new ListNode(value) + return + } + + let currentNode = this.start + + while (currentNode.next !== null) { + currentNode = currentNode.next + } + + currentNode.next = new ListNode(value) + + } + + public find = ( + value: any + ): ListNode | null => { + + let currentNode = this.start + + while( + currentNode !== null && + currentNode.value !== value + ){ + currentNode = currentNode.next + } + + return currentNode + } +} + +const list = new LinkedList() + +list.addToTail("segunda") +list.addToTail("terça") +list.addToTail("quarta") +list.addToTail("quinta") \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/src/Queue/index.ts b/modulo8/Estrutura-de-dados/src/Queue/index.ts new file mode 100644 index 0000000..ab2d67c --- /dev/null +++ b/modulo8/Estrutura-de-dados/src/Queue/index.ts @@ -0,0 +1,35 @@ +import { LinkedList } from "../LinkedList"; +import { ListNode } from "../LinkedList/ListNode"; + +export class Queue { + constructor( + public items: LinkedList = new LinkedList() + ) { } + + public isEmpty = (): boolean => this.items.start === null + + public enqueue = ( + value: any + ): void => { + this.items.addToTail(value) + } + + public dequeue = (): ListNode | null => { + + if (this.items.start === null) return null + + const removedItem = { ...this.items.start } + + this.items.start = this.items.start.next + + return removedItem + } +} + +const requests = new Queue() + +requests.enqueue("request 1") +requests.enqueue("request 2") +requests.enqueue("request 3") + +const request = requests.dequeue() \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/src/Stack/index.ts b/modulo8/Estrutura-de-dados/src/Stack/index.ts new file mode 100644 index 0000000..e179e5a --- /dev/null +++ b/modulo8/Estrutura-de-dados/src/Stack/index.ts @@ -0,0 +1,33 @@ +export class Stack { + constructor( + public frames: any[] = [] + ) { } + + public isEmpty = () => this.frames.length === 0 + + public push = ( + value: any + ): void => { + + const index = this.frames.length + + this.frames[index] = value + } + + public pop = ():any=>{ + + const index = this.frames.length - 1 + + const removedItem = this.frames[index] + + this.frames.length-- + + return removedItem + } + } + + const stack = new Stack() + + stack.push("segunda") + stack.push("terça") + stack.push("quarta") \ No newline at end of file diff --git a/modulo8/Estrutura-de-dados/tsconfig.json b/modulo8/Estrutura-de-dados/tsconfig.json new file mode 100644 index 0000000..245e0bf --- /dev/null +++ b/modulo8/Estrutura-de-dados/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "sourceMap": true, + "outDir": "./build", + "rootDir": "./src", + "removeComments": true, + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + } + } \ No newline at end of file