All files doubly-linked-list.ts

100% Statements 121/121
100% Branches 30/30
100% Functions 18/18
100% Lines 116/116

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252                                                            42x           1x 16x 16x 16x   16x 18x   18x 9x 9x   9x 9x 9x     18x     16x 22x   22x 7x 7x   15x 15x 15x     22x     16x 5x   4x 2x 2x   2x 2x     4x     16x 4x   3x 1x 1x   2x 2x     3x     16x 5x   5x 9x 5x 5x     4x 2x   1x 1x 1x     4x     1x     16x 1x 1x 1x     16x 13x   13x 17x 6x     11x     7x     16x 5x     16x 1x 1x   1x 6x 6x 6x       16x 1x 1x 1x   1x 3x 3x 3x     1x     16x 2x     16x 3x   3x 1x     2x 1x 1x     1x   1x 1x 1x 1x 1x   1x     16x 3x   3x 1x     2x 1x 1x     1x   1x 1x 1x 1x 1x   1x     16x   21x     20x     13x                                       1x    
export interface Node<NodeValueType> {
  value: NodeValueType;
  next: Node<NodeValueType> | null;
  prev: Node<NodeValueType> | null;
}
 
export interface IDoublyLinkedList<NodeValueType> {
  head: Node<NodeValueType> | null;
  tail: Node<NodeValueType> | null;
  size: number;
 
  addFront: (value: NodeValueType) => void;
  addEnd: (value: NodeValueType) => void;
  removeFront: () => void;
  removeEnd: () => void;
  remove: (value: NodeValueType) => boolean;
  clear: () => void;
  contains: (value: NodeValueType) => boolean;
  forEach: (fn: ForEachFunction<NodeValueType>) => void;
  copyToArray: () => NodeValueType[];
  isEmpty: () => boolean;
  addBefore: (valueBefore: NodeValueType, value: NodeValueType) => boolean;
  addAfter: (valueAfter: NodeValueType, value: NodeValueType) => boolean;
  getNodeByValue: (value: NodeValueType) => Node<NodeValueType> | null;
 
  __proto__: null;
}
 
export type ForEachFunction<NodeValueType> = (value: NodeValueType, index: number) => void;
 
export const createNode = <NodeValueType>(value: NodeValueType): Node<NodeValueType> => ({
  value,
  next: null,
  prev: null,
});
 
export const createDoublyLinkedList = <NodeValueType>(): IDoublyLinkedList<NodeValueType> => {
  let head: Node<NodeValueType> | null = null;
  let tail: Node<NodeValueType> | null = null;
  let size = 0;
 
  const addFront = (value: NodeValueType) => {
    const node: Node<NodeValueType> = createNode(value);
 
    if (head == null) {
      head = node;
      tail = node;
    } else {
      head.prev = node;
      node.next = head;
      head = node;
    }
 
    size += 1;
  };
 
  const addEnd = (value: NodeValueType) => {
    const node: Node<NodeValueType> = createNode<NodeValueType>(value);
 
    if (head == null || tail == null) {
      head = node;
      tail = node;
    } else {
      tail.next = node;
      node.prev = tail;
      tail = node;
    }
 
    size += 1;
  };
 
  const removeFront = () => {
    if (!head) return;
 
    if (!head.next) {
      head = null;
      tail = null;
    } else {
      head.next.prev = null;
      head = head.next;
    }
 
    size -= 1;
  };
 
  const removeEnd = () => {
    if (!tail) return;
 
    if (!tail.prev) {
      head = null;
      tail = null;
    } else {
      tail.prev.next = null;
      tail = tail.prev;
    }
 
    size -= 1;
  };
 
  const remove = (value: NodeValueType): boolean => {
    let current: Node<NodeValueType> | null = head;
 
    while (current != null) {
      if (!Object.is(current.value, value)) {
        current = current.next;
        continue;
      }
 
      if (!current.prev) removeFront();
      else if (!current.next) removeEnd();
      else {
        current.prev.next = current.next;
        current.next.prev = current.prev;
        size -= 1;
      }
 
      return true;
    }
 
    return false;
  };
 
  const clear = () => {
    head = null;
    tail = null;
    size = 0;
  };
 
  const getNodeByValue = (value: NodeValueType): Node<NodeValueType> | null => {
    let current: Node<NodeValueType> | null = head;
 
    while (current != null) {
      if (Object.is(current.value, value)) {
        return current;
      }
      
      current = current.next;
    }
 
    return null;
  }
 
  const contains = (value: NodeValueType): boolean => {
    return Boolean(getNodeByValue(value));
  };
 
  const forEach = (fn: ForEachFunction<NodeValueType>) => {
    let current: Node<NodeValueType> | null = head;
    let i = 0;
 
    while (current != null) {
      fn(current.value, i);
      current = current.next;
      i += 1;
    }
  };
 
  const copyToArray = (): NodeValueType[] => {
    let current: Node<NodeValueType> | null = head;
    let index = 0;
    const result = Array(size);
 
    while (current != null) {
      result[index] = current.value;
      current = current.next;
      index += 1;
    }
 
    return result;
  }
 
  const isEmpty = (): boolean => {
    return Boolean(size === 0);
  }
 
  const addBefore = (valueBefore: NodeValueType, value: NodeValueType): boolean => {
    const nodeBefore = getNodeByValue(valueBefore);
 
    if (!nodeBefore) {
      return false;
    }
 
    if (nodeBefore === head) {
      addFront(value);
      return true;
    }
 
    const newNode: Node<NodeValueType> = createNode(value);
 
    nodeBefore!.prev!.next = newNode;
    newNode.prev = nodeBefore.prev;
    nodeBefore.prev = newNode;
    newNode.next = nodeBefore;
    size += 1;
 
    return true;
  }
 
  const addAfter = (valueAfter: NodeValueType, value: NodeValueType): boolean => {
    const nodeAfter = getNodeByValue(valueAfter);
 
    if (!nodeAfter) {
      return false;
    }
 
    if (nodeAfter === tail) {
      addEnd(value);
      return true;
    }
 
    const newNode: Node<NodeValueType> = createNode(value);
 
    nodeAfter!.next!.prev = newNode;
    newNode.next = nodeAfter.next;
    nodeAfter.next = newNode;
    newNode.prev = nodeAfter;
    size += 1;
 
    return true;
  }
 
  return {
    get head() {
      return head;
    },
    get tail() {
      return tail;
    },
    get size() {
      return size;
    },
    addFront,
    addEnd,
    removeFront,
    removeEnd,
    remove,
    clear,
    contains,
    forEach,
    copyToArray,
    getNodeByValue,
    isEmpty,
    addBefore,
    addAfter,
 
    __proto__: null // Delete inherited fields from proto
  };
}
 
export default createDoublyLinkedList;