/*! * doubly-linked-list-adt - Doubly Linked List ADT for browser and nodejs * @version v0.1.1 - Sat Aug 23 2014 * @link https://github.com/pasangsherpa/doubly-linked-list-adt * @author Pasang Sherpa (https://github.com/pasangsherpa) * @license MIT */ !function(){"use strict";var e=function(){function e(e){this.element=e||null,this.next=this.previous=null}function n(n){var t=new e(n);null===c?c=p=t:(p.next=t,t.previous=p,p=t),a++}function t(n){var t=new e(n);null===c?c=p=t:(t.next=c,c.previous=t,c=t),a++}function r(){if(f())throw new Error("removeFirst(): List is empty.");var e=c.element;return c.next.previous=null,c=c.next,e}function u(){if(f())throw new Error("removeLast(): List is empty.");var e=p.element;return p.previous.next=null,p=p.previous,e}function i(e){if(f())throw new Error("remove(): List is empty.");var n,t,r=l(e>=0?e:a-1);return t=r.next,n=r.previous,1===a?c=p=null:0===e?(t.previous=null,c=t):!e||e>=a-1?(n.next=null,p=n):(n.next=t,t.previous=n),a--,r.element}function o(){if(f())throw new Error("first(): List is empty.");return c.element}function l(e){if(0>e||e>a||null===c)return null;var n,t,r=a/2;if(e>r)for(n=p,t=a-1;t>e;t--)n=n.previous;else for(n=c,t=0;e>t;t++)n=n.next;return n}function s(e){return l(e).element}function f(){return 0===a}function v(){return a}function m(){function e(){return a>u}function n(){if(!e())throw new Error("next(): No such element.");u||(r=c),t=r;var n=r.element;return r=r.next,u++,n}var t=null,r=null,u=0;return{hasNext:e,next:n}}var c,p,a=0;return c=p=null,{add:n,addFront:t,remove:i,removeFirst:r,removeLast:u,first:o,get:s,isEmpty:f,size:v,iterator:new m}};"function"==typeof define&&define.amd?define(function(){return e}):"undefined"!=typeof module&&module.exports?module.exports=e:window.DoublyLinkedList=e}();