123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- "use strict";
- const DoublyLinkedList = require("./DoublyLinkedList");
- const DequeIterator = require("./DequeIterator");
- class Deque {
- constructor() {
- this._list = new DoublyLinkedList();
- }
-
- shift() {
- if (this.length === 0) {
- return undefined;
- }
- const node = this._list.head;
- this._list.remove(node);
- return node.data;
- }
-
- unshift(element) {
- const node = DoublyLinkedList.createNode(element);
- this._list.insertBeginning(node);
- }
-
- push(element) {
- const node = DoublyLinkedList.createNode(element);
- this._list.insertEnd(node);
- }
-
- pop() {
- if (this.length === 0) {
- return undefined;
- }
- const node = this._list.tail;
- this._list.remove(node);
- return node.data;
- }
- [Symbol.iterator]() {
- return new DequeIterator(this._list);
- }
- iterator() {
- return new DequeIterator(this._list);
- }
- reverseIterator() {
- return new DequeIterator(this._list, true);
- }
-
- get head() {
- if (this.length === 0) {
- return undefined;
- }
- const node = this._list.head;
- return node.data;
- }
-
- get tail() {
- if (this.length === 0) {
- return undefined;
- }
- const node = this._list.tail;
- return node.data;
- }
- get length() {
- return this._list.length;
- }
- }
- module.exports = Deque;
|