/** * Adds the element to the back of the list. * * @param object Object being added. * @return The element it was placed inside of */ public Element<T> pushTail( T object ) { Element<T> e = requestNew(); e.object = object; if( last == null ) { first = last = e; } else { e.previous = last; last.next = e; last = e; } size++; return e; }
/** * Adds the element to the front of the list. * * @param object Object being added. * @return The element it was placed inside of */ public Element<T> pushHead( T object ) { Element<T> e = requestNew(); e.object = object; if( first == null ) { first = last = e; } else { e.next = first; first.previous = e; first = e; } size++; return e; }
/** * Inserts the object into a new element after the provided element. * * @param previous Element which will be before the new one * @param object The object which goes into the new element * @return The new element */ public Element<T> insertAfter( Element<T> previous , T object ) { Element<T> e = requestNew(); e.object = object; e.previous = previous; e.next = previous.next; if( e.next != null ) { e.next.previous = e; } else { last = e; } previous.next = e; size++; return e; }
/** * Inserts the object into a new element before the provided element. * * @param next Element which will be after the new one * @param object The object which goes into the new element * @return The new element */ public Element<T> insertBefore( Element<T> next , T object ) { Element<T> e = requestNew(); e.object = object; e.previous = next.previous; e.next = next; if( e.previous != null ) { e.previous.next = e; } else { first = e; } next.previous = e; size++; return e; }
/** * Adds the element to the front of the list. * * @param object Object being added. * @return The element it was placed inside of */ public Element<T> pushHead( T object ) { Element<T> e = requestNew(); e.object = object; if( first == null ) { first = last = e; } else { e.next = first; first.previous = e; first = e; } size++; return e; }
/** * Adds the element to the back of the list. * * @param object Object being added. * @return The element it was placed inside of */ public Element<T> pushTail( T object ) { Element<T> e = requestNew(); e.object = object; if( last == null ) { first = last = e; } else { e.previous = last; last.next = e; last = e; } size++; return e; }
/** * Inserts the object into a new element before the provided element. * * @param next Element which will be after the new one * @param object The object which goes into the new element * @return The new element */ public Element<T> insertBefore( Element<T> next , T object ) { Element<T> e = requestNew(); e.object = object; e.previous = next.previous; e.next = next; if( e.previous != null ) { e.previous.next = e; } else { first = e; } next.previous = e; size++; return e; }
/** * Inserts the object into a new element after the provided element. * * @param previous Element which will be before the new one * @param object The object which goes into the new element * @return The new element */ public Element<T> insertAfter( Element<T> previous , T object ) { Element<T> e = requestNew(); e.object = object; e.previous = previous; e.next = previous.next; if( e.next != null ) { e.next.previous = e; } else { last = e; } previous.next = e; size++; return e; }
/** * Adds the specified elements from array into this list * @param array The array * @param first First element to be added * @param length The number of elements to be added */ public void addAll( T[] array , int first , int length ) { if( length <= 0 ) return; Element<T> a = requestNew(); a.object = array[first]; if( this.first == null ) { this.first = a; } else if( last != null ) { last.next = a; a.previous = last; } for (int i = 1; i < length; i++) { Element<T> b = requestNew(); b.object = array[first+i]; a.next = b; b.previous = a; a = b; } last = a; size += length; }
/** * Adds the specified elements from array into this list * @param array The array * @param first First element to be added * @param length The number of elements to be added */ public void addAll( T[] array , int first , int length ) { if( length <= 0 ) return; Element<T> a = requestNew(); a.object = array[first]; if( this.first == null ) { this.first = a; } else if( last != null ) { last.next = a; a.previous = last; } for (int i = 1; i < length; i++) { Element<T> b = requestNew(); b.object = array[first+i]; a.next = b; b.previous = a; a = b; } last = a; size += length; }
/** * Add all elements in list into this linked list * @param list List */ public void addAll( List<T> list ) { if( list.isEmpty() ) return; Element<T> a = requestNew(); a.object = list.get(0); if( first == null ) { first = a; } else if( last != null ) { last.next = a; a.previous = last; } for (int i = 1; i < list.size(); i++) { Element<T> b = requestNew(); b.object = list.get(i); a.next = b; b.previous = a; a = b; } last = a; size += list.size(); }
/** * Add all elements in list into this linked list * @param list List */ public void addAll( List<T> list ) { if( list.isEmpty() ) return; Element<T> a = requestNew(); a.object = list.get(0); if( first == null ) { first = a; } else if( last != null ) { last.next = a; a.previous = last; } for (int i = 1; i < list.size(); i++) { Element<T> b = requestNew(); b.object = list.get(i); a.next = b; b.previous = a; a = b; } last = a; size += list.size(); }
@Test public void requestNew() { LinkedList alg = new LinkedList(); LinkedList.Element e0 = alg.requestNew(); assertTrue(e0 != null); LinkedList.Element e1 = alg.requestNew(); assertTrue(e1 != null); assertTrue(e1 != e0); alg.available.add(e0); LinkedList.Element e2 = alg.requestNew(); assertTrue(e2 == e0); LinkedList.Element e3 = alg.requestNew(); assertTrue(e3 != null); assertTrue(e3 != e0); assertTrue(e3 != e1); assertTrue(e3 != e2); }