Created
November 25, 2022 13:10
-
-
Save giovannamoeller/57830ac210f33b784785792eb7f03692 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ListNode { | |
| constructor(value) { | |
| this.value = value | |
| this.next = null | |
| } | |
| } | |
| class LinkedList { | |
| head = null | |
| tail = null | |
| count = 0 | |
| constructor() {} | |
| insertFirstPosition(value) { | |
| const node = new ListNode(value) | |
| this.count++ | |
| if (this.head == null) { | |
| this.head = node | |
| this.tail = node | |
| return | |
| } | |
| const currentHead = this.head | |
| this.head = node | |
| this.head.next = currentHead | |
| } | |
| insertLastPosition(value) { | |
| const node = new ListNode(value) | |
| this.count++ | |
| if (this.tail == null) { | |
| this.tail = node | |
| this.head = node | |
| return | |
| } | |
| const currentTail = this.tail | |
| currentTail.next = node | |
| this.tail = node | |
| } | |
| insertAtNPosition(value, n) { | |
| const node = new ListNode(value) | |
| let currentCount = 0, currentNode = this.head, previousNode, indexFound = false | |
| while (currentNode) { | |
| if (currentCount === n) { | |
| indexFound = true | |
| previousNode.next = node | |
| node.next = currentNode | |
| break | |
| } | |
| currentCount++ | |
| previousNode = currentNode | |
| currentNode = currentNode.next | |
| } | |
| indexFound ? this.count++ : console.log(`Erro para inserir o valor ${value}. O índice a inserir deve estar entre 0 e ${this.count - 1}`) | |
| } | |
| deleteFirstPosition() { | |
| if (this.head == null) { | |
| console.log('Não existe nenhum elemento na lista para deletar') | |
| return | |
| } | |
| this.head = this.head.next | |
| this.count-- | |
| } | |
| deleteLastPosition() { | |
| let currentNode = this.head | |
| while (currentNode) { | |
| if (currentNode.next == this.tail) { | |
| this.tail = currentNode | |
| this.tail.next = null | |
| break | |
| } | |
| currentNode = currentNode.next | |
| } | |
| this.count-- | |
| } | |
| deleteAtNPosition(value, n) { | |
| const node = new ListNode(value) | |
| let currentCount = 0, elementFound = false, currentNode = this.head, previousNode | |
| while (currentNode) { | |
| if (currentCount === n) { | |
| elementFound = true | |
| previousNode.next = node | |
| node.next = currentNode | |
| break | |
| } | |
| currentCount++ | |
| previousNode = currentNode | |
| currentNode = currentNode.next | |
| } | |
| elementFound ? this.count-- : console.log(`Não foi encontrado nenhum elemento para remoção no índice ${n}`) | |
| } | |
| updateFirstPosition(value) { | |
| if (this.head == null) { | |
| console.log('Não existe nenhum elemento na lista para atualizar') | |
| return | |
| } | |
| this.head.value = value | |
| } | |
| updateLastPosition(value) { | |
| this.tail.value = value | |
| } | |
| updateAtNPosition(value, n) { | |
| let currentCount = 0, elementFound = false | |
| let currentNode = this.head | |
| while (currentNode) { | |
| if (currentCount === n) { | |
| elementFound = true | |
| currentNode.value = value | |
| break | |
| } | |
| currentCount++ | |
| currentNode = currentNode.next | |
| } | |
| if (!elementFound) { console.log(`Não existe nenhum elemento no índice ${n}`)} | |
| } | |
| findElementAtIndex(n) { | |
| let currentNode = this.head, currentCount = 0 | |
| while(currentNode) { | |
| if (currentCount == n) { | |
| return n | |
| } | |
| currentCount++ | |
| currentNode = currentNode.next | |
| } | |
| return null | |
| } | |
| firstIndexOfElement(element) { | |
| let currentNode = this.head, currentCount = 0 | |
| while(currentNode) { | |
| if (currentNode.value == element) { | |
| return currentCount | |
| } | |
| currentCount++ | |
| currentNode = currentNode.next | |
| } | |
| return -1 | |
| } | |
| printList() { | |
| let str = "", currentNode = this.head | |
| while (currentNode) { // currentNode != null | |
| str += currentNode.value + (currentNode.next && " -> ") | |
| currentNode = currentNode.next | |
| } | |
| console.log(str) | |
| } | |
| getListLength() { | |
| return this.count | |
| } | |
| isEmpty() { | |
| return this.count === 0 | |
| } | |
| } | |
| const linkedList = new LinkedList() | |
| console.log(linkedList.isEmpty()) | |
| linkedList.insertFirstPosition(8) | |
| linkedList.insertFirstPosition(4) | |
| linkedList.insertFirstPosition(10) | |
| linkedList.insertFirstPosition(6) | |
| linkedList.insertFirstPosition(3) | |
| linkedList.insertFirstPosition(2) | |
| linkedList.insertAtNPosition(50, 10) | |
| linkedList.deleteLastPosition() | |
| console.log(linkedList.getListLength()) | |
| console.log(linkedList.isEmpty()) | |
| console.log(linkedList.firstIndexOfElement(2)) | |
| console.log(linkedList.findElementAtIndex(3)) | |
| linkedList.printList() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment