diff --git a/semana23/aulaEstrutura/exercicio1.ts b/semana23/aulaEstrutura/exercicio1.ts new file mode 100644 index 0000000..f33da26 --- /dev/null +++ b/semana23/aulaEstrutura/exercicio1.ts @@ -0,0 +1,25 @@ +export class LinkedList { + constructor(public start?: LinkedListNode) {} + + public appendToTail(value: number) { + if (!this.start) { + this.start = new LinkedListNode(value); + } else { + let node: LinkedListNode = this.start; + while (node && node.getNext() !== undefined) { + node = node.getNext()!; + } + node.setNext(new LinkedListNode(value)); + } + } + + public print(): void { + let node: LinkedListNode | undefined = this.start; + let i = 1; + while (node !== undefined) { + console.log(`Elemento ${i}: `, node!.getData()); + node = node!.getNext(); + i++; + } + } + } \ No newline at end of file diff --git a/semana23/aulaEstrutura/exercicio2.ts b/semana23/aulaEstrutura/exercicio2.ts new file mode 100644 index 0000000..7afa96f --- /dev/null +++ b/semana23/aulaEstrutura/exercicio2.ts @@ -0,0 +1,31 @@ +class Stack { + constructor( + public frames: LinkedList = new LinkedList() + ) { } + + public isEmpty = (): boolean => this.frames.start === null + + public push = ( + value: any + ): void => { + this.frames.addToTail(value) + } + + public pop = (): any => { + + if(!this.isEmpty()) return null + + let previousNode: ListNode | null = null + let currentNode: ListNode | null = this.frames.start + + while(currentNode!.next){ + previousNode = currentNode + currentNode = currentNode!.next + } + + previousNode!.next = null + + return currentNode + + } + } \ No newline at end of file diff --git a/semana23/aulaEstrutura/exercicio3.ts b/semana23/aulaEstrutura/exercicio3.ts new file mode 100644 index 0000000..9dda9f4 --- /dev/null +++ b/semana23/aulaEstrutura/exercicio3.ts @@ -0,0 +1,27 @@ +class Queue { + constructor( + public items: any[] = [] + ) { } + + public isEmpty = (): boolean => this.items.length === 0 + + public enqueue = ( + value: any + ): void => { + const index = this.items.length + this.items[index] = value + } + + public dequeue = (): ListNode | null => { + + const removedItem = this.items[0] + + for (let i = 0; i < this.items.length; i++) { + this.items[i] = this.items[i + 1]; + } + + this.items.length-- + + return removedItem + } + } \ No newline at end of file