Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node {
  2.     constructor(data, next) {
  3.         this.data = data
  4.         this.next = next
  5.     }
  6. }
  7.  
  8. class LinkedList {
  9.     constructor() {
  10.         this.head = null
  11.         this.length = 0
  12.     }
  13.     validIndex(index) {
  14.         if (index < 0 || index > this.length) return false
  15.         else return true
  16.     }
  17.     unshift(data) {
  18.         const node = new Node(data, this.head)
  19.         this.head = node
  20.         this.length++
  21.     }
  22.     getFirst() {
  23.         return this.head
  24.     }
  25.     getLast() {
  26.         if (this.head) {
  27.             let node = this.head
  28.             while (node.next) node = node.next
  29.             return node
  30.         }
  31.     }
  32.     clear() {
  33.         this.head = null
  34.         this.length = 0
  35.     }
  36.     shift() {
  37.         if (this.head) {
  38.             const node = this.head
  39.             this.head = this.head.next
  40.             this.length--
  41.             return node
  42.         }
  43.     }
  44.     pop() {
  45.         if (this.head) {
  46.             if (this.length === 0) return
  47.             if (this.length === 1) return this.shift()
  48.             const last = this.getLast()
  49.             let node = this.head
  50.             while (node !== last) node = node.next
  51.             node.next = null
  52.             this.length--
  53.             return last
  54.         }
  55.     }
  56.     push(data) {
  57.         if (!this.head) return this.unshift(data)
  58.         else this.getLast().next = new Node(data)
  59.         this.length++
  60.     }
  61.     get(index) {
  62.         if (this.validIndex(index)) {
  63.             let node = this.head
  64.             for (let i = 0; i < index; i++) {
  65.                 node = node.next
  66.             }
  67.             return node
  68.         }
  69.     }
  70.     set(index, data) {
  71.         if (this.validIndex(index)) {
  72.             const node = this.get(index)
  73.             node.data = data
  74.             return true
  75.         }
  76.     }
  77.     remove(index) {
  78.         if (this.validIndex(index)) {
  79.             if (index === 0) return this.shift()
  80.  
  81.             const node = this.get(index)
  82.             const prevNode = this.get(index - 1)
  83.  
  84.             prevNode.next = node.next
  85.             this.length--
  86.             if (this.length === 0) this.head = null
  87.             return node
  88.         }
  89.     }
  90.     insert(index, data) {
  91.         if (this.validIndex(index)) {
  92.             if (index !== 0) {
  93.                 const node = this.get(index - 1)
  94.                 node.next = new Node(data, node.next)
  95.             } else this.head = new Node(data, this.head)
  96.             this.length++
  97.             return true
  98.         } else return false
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement