Skip to content

Commit

Permalink
add popHead and Extend functions to linked list data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
p-flock committed Jan 14, 2020
1 parent 0801694 commit c75fdac
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/common/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ module.exports = function () {
}
return node;
};

list.popHead = function () {
var result = list.head;
if (list.head != null) {
list.head = list.head.next;
if (list.head == null) {
list.tail = null;
} else {
list.head.previous = null;
}
}
return result;
};

// merges two linked lists and return a pointer to the head of the merged list
// the head will be the head of list and the tail the tail of l2
list.extend = function (l2) {
if (list.head == null) {
return l2;
}
if (l2.head == null) {
return list;
}
list.tail.next = l2.head;
l2.head.previous = list.tail;
list.tail = l2.tail;

return list;
};

list.remove = function (ptr) {
var prev = ptr.previous;
var next = ptr.next;
Expand Down Expand Up @@ -83,4 +113,4 @@ module.exports = function () {
};
*/
return list;
};
};

0 comments on commit c75fdac

Please sign in to comment.