/
LinkedList.min.js
2 lines (2 loc) · 4.91 KB
/
LinkedList.min.js
1
2
/* JSLinkedList v1.0.0 (c) 2013, Pachi Systems Inc / Brian Milton */
(function(b,d){var a=0;function c(h){this._length=0;this._head=null;this._tail=null;if(h){for(var e in h){var f={data:null,next:null};if(this._head===null){f.data=h[e];this._head=f;this._tail=f;a++;this._length++}else{f.data=h[e];var g=this._tail;g.next=f;this._tail=f;a++;this._length++}}}}c.prototype.add=function(e,g){if(g){var j={data:g,next:null};var k=this._head;var f=0;var h;if(e>-1&&e<this._length){while(f++<e){h=k;k=k.next}j.next=k;h.next=j}else{return false}this._length++;a++;return true}else{var j={data:e,next:null};var k;if(this._head===null){this._head=j;this._tail=j}else{k=this._tail;k.next=j;this._tail=j}this._length++;a++;return true}return false};c.prototype.addAll=function(f,i){if(i){if(f>-1&&f<this._length){for(var e in i){this.add(f,i[e]);f++}return true}else{return false}}else{for(var e in f){var h=this._tail;var g={data:f[e],next:null};h.next=g;this._tail=g;this._length++;a++}return true}},c.prototype.addFirst=function(f){var e={data:f,next:this._head};this._head=e;this._length++;a++;return true};c.prototype.addLast=function(e){return this.add(e)};c.prototype.clear=function(){if(this._length===0){return}var e={data:null,next:null};this._head=null;this._tail=null;this._length=0;a++;return};c.prototype.clone=function(){return JSON.parse(JSON.stringify(this))};c.prototype.contains=function(e){var f=this._head;while(f!==null){if(f.data===e){return true}f=f.next}return false};c.prototype.element=function(){return this._head.data};c.prototype.get=function(e){if(e>-1&&e<this._length){var g=this._head,f=0;while(f++<e){g=g.next}return g.data}else{return null}};c.prototype.getFirst=function(){return this._head.data};c.prototype.getLast=function(){return this._tail.data};c.prototype.indexOf=function(f){var g=this._head;var e=0;while(g!==null){if(g.data===f){return e}g=g.next;e++}return -1};c.prototype.insertAfter=function(e,f){return this.add(e+1,f)};c.prototype.insertBefore=function(e,f){return this.add(e-1,f)};c.prototype.lastIndexOf=function(e){var f=this._head;var g=currentIndex=-1;while(f!==null){currentIndex++;if(f.data===e){g=currentIndex}f=f.next}return g};c.prototype.offer=function(e){return this.add(e)};c.prototype.offerFirst=function(e){return this.addFirst(e)};c.prototype.offerLast=function(e){return this.add(e)};c.prototype.peek=function(){return this._head.data};c.prototype.peekFirst=function(){return this._head.data};c.prototype.peekLast=function(){return this._tail.data};c.prototype.poll=function(){return this.removeFirst()};c.prototype.pollFirst=function(){return this.removeFirst()};c.prototype.pollLast=function(){return this.removeLast()};c.prototype.pop=function(){return this.removeFirst()};c.prototype.push=function(e){return this.addFirst(e)};c.prototype.remove=function(e){if(!e){e=0}if(e>-1&&e<this._length){var h=this._head,g,f=0;if(e===0){this._head=h.next}else{while(f++<e){g=h;h=h.next}g.next=h.next;if(e==this._length-1){this._tail=g}}this._length--;a++;return h.data}else{return null}};c.prototype.removeFirst=function(){var f=this._head;var e=f.next;this._head=e;this._length--;a++;return f.data};c.prototype.removeFirstOccurrence=function(e){if(this.remove(this.indexOf(e))){return true}else{return false}};c.prototype.removeLast=function(){var e=this._head;while(e.next!==null){previous=e;e=e.next}previous.next=null;this._tail=previous;this._length--;a++;return e.data};c.prototype.removeLastOccurrence=function(e){if(this.remove(this.lastIndexOf(e))){return true}else{return false}};c.prototype.set=function(e,j){var h={data:j,next:null};var k=this._head;var g;var f=0;while(f++<e){g=k;k=k.next}h.next=k.next;g.next=h;a++;return k.data};c.prototype.size=function(){return this._length};c.prototype.sort=function(f){if(this._length===0){return false}else{var h=false;while(!h){h=true;var i="";var j=this._head;do{if(j.next!==null){var e=j.next;var g=f(j.data,e.data);if(g===true||g>0){h=false;i=j.data;j.data=e.data;e.data=i}}j=j.next}while(j!==null);j=this._tail}a++;return true}};c.prototype.sortedAdd=function(h,e){var k={data:h,next:null};if(this._length===0){this._head=k;this._tail=k;a++;this._length++;return}var l=this._head;var j=null;var g=0;while(g++<this._length){var f=e(l.data,k.data);if(f===true||f>0){if(j===null){k.next=this._head;this._head=k;a++;this._length++;return}k.next=l;j.next=k;a++;this._length++;return}j=l;l=l.next}l=this._tail;l.next=k;this._tail=k;a++;this._length++;return};c.prototype.toArray=function(){var e=[];var f=this._head;while(f!==null){e.push(f.data);f=f.next}return e};c.prototype.toString=function(){var g="";if(this._length===0){g=null;return g}else{var f=current=this._head;do{g+="["+current.data;if(current.next!==null){var e=current.next;g+="], "}else{g+="]"}current=current.next}while(current!==null);return g}};if(typeof module==="object"&&module&&typeof module.exports==="object"){module.exports=c}else{b.LinkedList=c;if(typeof define==="function"&&define.amd){define("LinkedList",[],function(){return c})}}})(window);