Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing last element and referencing of next #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Chapter6/chap6-4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function remove(item) {
var currNode = this.find(item);
if (!(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
// if we try to remove last element in the list via this method it will fail as previous of null.
if (current.next !== null) {
currNode.next.previous = currNode.previous;
}
currNode.next = null;
currNode.previous = null;
}
Expand Down Expand Up @@ -72,6 +75,10 @@ function insert(newElement, item) {
var current = this.find(item);
newNode.next = current.next;
newNode.previous = current;
// if there is next element than it should have previous reference to current one or else it would preserve reference to it's previous element in th list
if (newNode.next) {
newNode.next.previous = newNode;
}
current.next = newNode;
}

Expand Down