2017-10-10 7 views
1

ES6 스타일을 사용하여 작업은 연결된 목록을 작성하고 npm linter 테스트를 통과시키는 것입니다.JavaScript 연결 목록 : 메서드 removeHead() 및 contains() not working

/* eslint-disable no-undef, no-prototype-builtins */ 
const LinkedList = require('../src/linked-list'); 

let list; 

describe('LinkedList',() => { 
    beforeEach(() => { 
    list = new LinkedList(); 
    }); 

    it('should have the methods "addToTail", "removeHead", and "contains"',() => { 
    const hasAddToTail = 
Object.getPrototypeOf(list).hasOwnProperty('addToTail'); 
    const hasRemoveHead = 
Object.getPrototypeOf(list).hasOwnProperty('removeHead'); 
    const hasContains = 
Object.getPrototypeOf(list).hasOwnProperty('contains'); 
    expect(hasAddToTail).toBe(true); 
    expect(hasRemoveHead).toBe(true); 
    expect(hasContains).toBe(true); 
    }); 

    it('should update the tail value when a new node is added',() => { 
    list.addToTail(1); 
    expect(list.tail.value).toBe(1); 
    list.addToTail(2); 
    expect(list.tail.value).toBe(2); 
    }); 

    it('should keep the same head after adding nodes',() => { 
    list.addToTail(1); 
    expect(list.head.value).toBe(1); 
    list.addToTail(2); 
    expect(list.head.value).toBe(1); 
    }); 

    it('should return true from contains if a matching value is found and false otherwise',() => { 
    list.addToTail(1); 
    list.addToTail(2); 
    list.addToTail('hello'); 
    list.addToTail(true); 
    expect(list.contains('hello')).toBe(true); 
    expect(list.contains('asdf')).toBe(false); 
    }); 

    it('should remove head when removeHead is invoked',() => { 
    list.addToTail(1); 
    list.addToTail(2); 
    expect(list.head.value).toBe(1); 
    list.removeHead(); 
    expect(list.head.value).toBe(2); 
    list.removeHead(); 
    expect(list.head).toBe(null); 
    }); 

    it('should return the head that is removed when removeHead is invoked',() => { 
    list.addToTail(1); 
    expect(list.removeHead()).toBe(1); 
    }); 

    it('should not contain removed values',() => { 
    list.addToTail(1); 
    list.addToTail(2); 
    list.addToTail(3); 
    list.addToTail(4); 
    list.removeHead(); 
    expect(list.contains(1)).toBe(false); 
    }); 
}); 

유일한 두 가지 오류는 다음과 같습니다 그것을 테스트 파일이 여기에

● LinkedList › should remove head when removeHead is invoked 

    TypeError: Cannot read property 'value' of null 

     at Object.<anonymous> (tests/linked-list.test.js:48:21) 

    LinkedList 
    ✓ should have the methods "addToTail", "removeHead", and "contains" (5ms) 
    ✓ should update the tail value when a new node is added (2ms) 
    ✓ should keep the same head after adding nodes (2ms) 
    ✕ should return true from contains if a matching value is found and false otherwise (4ms) 
    ✕ should remove head when removeHead is invoked (2ms) 
    ✓ should return the head that is removed when removeHead is invoked (1ms) 
    ✓ should not contain removed values (1ms) 

: 그리고 여기에 내가 작성한 코드입니다 :

class LinkedList { 
    constructor() { 
    this.head = null; 
    this.tail = null; 
    // Do not modify anything inside of the constructor 
    } 
    // Wraps the given value in a node object and adds the node to the tail of the list 
    // If the list is empty, the new element is considered the tail as 
well as the head 
    // If there is one element in the list before the new element is added, the new element becomes the tail of the list 
    addToTail(value) { 
    const newNode = { 
     value, 
     next: null 
    }; 
    if (!(this.head) && !(this.tail)) { 
     this.head = newNode; 
     this.tail = newNode; 
    } else this.tail = newNode; 
    } 
    // Removes the current head node from the list, replacing it with the next element in the list 
    // Returns the value of the removed node 
    removeHead() { 
    const removedHead = this.head.value; 
    this.head = this.head.next; 
    return removedHead; 
    } 
    // Checks the linked list for the given value 
    // Returns true if the the value is found in the list, false otherwise 
    contains(value) { 
    let found = false; 
    let checkedNode = this.head; 
    while (checkedNode) { 
     if (checkedNode.value === value) { 
     found = true; 
     } 
     checkedNode = checkedNode.next; 
    } return found; 
    } 
} 

그리고 여기 linting 오류입니다 헤드가 삭제되지 않고 적절한 boolean가 돌려 주어지지 않은 것을 나타냅니다. 나는 왜 그런지 상실하고 있습니다.

+0

테스트가 같은 점은 무엇입니까? – Timo

+0

코드를 살펴보십시오. 어디에서'value' 속성에 접근하려고합니까? 문제는 거기에있을 것입니다. –

답변

2

새 요소를 삽입 할 때 next 참조를 설정하지 마십시오.

대신이 시도 :

if (!(this.head) && !(this.tail)) { 
    this.head = newNode; 
    this.tail = newNode; 
} else { 
    this.tail.next = newNode; 
    this.tail = newNode; 
} 
+0

그게 효과가 있어요. 내가 얼마나 오랫동안 머리에 맞고 있는지 잘 모를거야. 대단히 감사합니다. –