Avatar of dfujiwara

dfujiwara's solution

to Linked List in the TypeScript Track

Published at Oct 12 2019 · 0 comments
Instructions
Test suite
Solution

Implement a doubly linked list.

Like an array, a linked list is a simple linear data structure. Several common data types can be implemented using linked lists, like queues, stacks, and associative arrays.

A linked list is a collection of data elements called nodes. In a singly linked list each node holds a value and a link to the next node. In a doubly linked list each node also holds a link to the previous node.

You will write an implementation of a doubly linked list. Implement a Node to hold a value and pointers to the next and previous nodes. Then implement a List which holds references to the first and last node and offers an array-like interface for adding and removing items:

  • push (insert value at back);
  • pop (remove value at back);
  • shift (remove value at front).
  • unshift (insert value at front);

To keep your implementation simple, the tests will not cover error conditions. Specifically: pop or shift will never be called on an empty list.

If you want to know more about linked lists, check Wikipedia.

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.

Source

Classic computer science topic

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

linked-list.test.ts

import LinkedList from './linked-list'

describe('LinkedList', () => {
  it('add/extract elements to the end of the list with push/pop', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.push(20)
    expect(list.pop()).toBe(20)
    expect(list.pop()).toBe(10)
  })

  xit('extract elements from the beginning of the list with shift', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.push(20)
    expect(list.shift()).toBe(10)
    expect(list.shift()).toBe(20)
  })

  xit('add/extract elements from the beginning of the list with unshift/shift', () => {
    const list = new LinkedList<number>()
    list.unshift(10)
    list.unshift(20)
    expect(list.shift()).toBe(20)
    expect(list.shift()).toBe(10)
  })

  xit('unshift/pop', () => {
    const list = new LinkedList<number>()
    list.unshift(10)
    list.unshift(20)
    expect(list.pop()).toBe(10)
    expect(list.pop()).toBe(20)
  })

  xit('example', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.push(20)
    expect(list.pop()).toBe(20)
    list.push(30)
    expect(list.shift()).toBe(10)
    list.unshift(40)
    list.push(50)
    expect(list.shift()).toBe(40)
    expect(list.pop()).toBe(50)
    expect(list.shift()).toBe(30)
  })

  xit('can count its elements', () => {
    const list = new LinkedList<number>()
    expect(list.count()).toBe(0)
    list.push(10)
    expect(list.count()).toBe(1)
    list.push(20)
    expect(list.count()).toBe(2)
  })

  xit('sets head/tail after popping last element', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.pop()
    list.unshift(20)
    expect(list.count()).toBe(1)
    expect(list.pop()).toBe(20)
  })

  xit('sets head/tail after shifting last element', () => {
    const list = new LinkedList<number>()
    list.unshift(10)
    list.shift()
    list.push(20)
    expect(list.count()).toBe(1)
    expect(list.shift()).toBe(20)
  })

  xit('deletes the element with the specified value from the list', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.push(20)
    list.push(30)
    list.delete(20)
    expect(list.count()).toBe(2)
    expect(list.pop()).toBe(30)
    expect(list.shift()).toBe(10)
  })

  xit('deletes the only element', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.delete(10)
    expect(list.count()).toBe(0)
  })

  xit('delete does not modify the list if the element is not found', () => {
    const list = new LinkedList<number>()
    list.push(10)
    list.delete(20)
    expect(list.count()).toBe(1)
  })
})
export default class LinkedList<T> {
    head: Node<T> | undefined
    tail: Node<T> | undefined
    push(data: T) {
        const newNode = new Node(data)
        if (!this.tail) {
            this.head = newNode
        } else {
            this.tail.next = newNode
            newNode.prev = this.tail
        }
        this.tail = newNode
    }
    pop(): T | undefined {
        if (!this.tail) {
            return undefined
        }
        if (this.tail.prev) {
            this.tail.prev.next = undefined
        }
        const data = this.tail.data
        if (this.head === this.tail) {
            this.head = undefined
        }
        this.tail = this.tail.prev
        return data
    }
    shift(): T | undefined {
       if (!this.head) {
           return undefined
       }
       const prevHead = this.head;
       if (this.head === this.tail) {
           this.tail = undefined
           this.head = undefined
       } else {
           this.head = this.head.next;
       }
       return prevHead.data
    }
    unshift(data: T) {
        const newNode = new Node(data)
        newNode.next = this.head
        if (this.head) {
            this.head.prev = newNode
        } else {
            this.tail = newNode
        }
        this.head = newNode
    }
    count(): number {
        let num = 0
        let current = this.head
        while (current) {
            num += 1
            current = current.next
        }
        return num
    }
    delete(data: T) {
        let current = this.head
        while (current) {
            if (current.data !== data) {
                current = current.next
                continue
            }
            const next = current.next
            if (current.next) {
                current.next.prev = current.prev
            }
            if (current.prev) {
                current.prev.next = current.next
            }
            if (current === this.head) {
                this.head = this.head.next
            }
            if (current === this.tail) {
                this.tail = this.tail.prev
            }
            current = next
        }
    }
}

class Node<T> {
    data: T
    next: Node<T> | undefined
    prev: Node<T> | undefined

    constructor(data: T) {
        this.data = data
        this.next = undefined
        this.prev = undefined
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?