@roger_npm_999/double-linked-list
Version:
Doubly linked list implementation
57 lines (56 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ListNode_1 = require("./ListNode");
var LinkedList = /** @class */ (function () {
function LinkedList() {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.size = 0;
this.head = ListNode_1.NULL_NODE;
this.tail = ListNode_1.NULL_NODE;
if (values.length === 0) {
return;
}
for (var i = 0; i < values.length; i++) {
var value = values[i];
if (i === 0) {
var listNode = new ListNode_1.ListNode(value);
listNode.list = this;
this.head = listNode;
this.tail = listNode;
this.size++;
}
else {
this.append(value);
}
}
}
/*
Appends a value to the end of the list and returns new node
*/
LinkedList.prototype.append = function (value) {
var listNode = new ListNode_1.ListNode(value);
listNode.list = this;
listNode.prev = this.tail;
listNode.prev.next = listNode;
this.tail = listNode;
this.size++;
return listNode;
};
/*
Removes last value in list and returns removed node
*/
LinkedList.prototype.pop = function () {
var removed = this.tail;
removed.prev.next = ListNode_1.NULL_NODE;
this.tail = removed.prev;
removed.prev = ListNode_1.NULL_NODE;
removed.list = null;
this.size--;
return removed;
};
return LinkedList;
}());
exports.LinkedList = LinkedList;