/* * LIST_DLL.java * * Created on February 6, 2008, 1:52 PM * * * MORE UP TO DATE VERSION IS IN LIST_DLL_purge with classname LIST * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package list_dll_purge; /** * * @author suchenek */ //Copyright Dr. Marek A. Suchenek, 2005, 2006, 2007, 2008 ,2009, 2010, 2011 - 2025 // See class POSITION_DLL for interpretation of position. // Object of class LIST_DLL is implemented as POSITION_DLL: (first). That position // never changes during the lifetime of the list in question. public class LIST_DLL { private POSITION_DLL first; //never changes because it is a reference to the header of the list public LIST_DLL() //constructor of the empty list { first = new POSITION_DLL(); first.head = null; first.tail = first; //self-reference first.nose = first; //self-reference // System.out.println("List created"); } public void Insert(POSITION_DLL p, Object x) { // Optimal method because one new position, assignment of x, // and update of 4 references (2 tails and 2 noses) are necessary to carry it on. POSITION_DLL temp = new POSITION_DLL(); temp.head = x; temp.tail = p.tail; temp.nose = p; temp.tail.nose = temp; //same as: p.tail.nose = temp; temp.nose.tail = temp; //same as: p.tail = temp; // System.out.println("Insert completed"); } public void Delete(POSITION_DLL p) { // Optimal method because update of 2 references (1 tail and 1 nose) are necessary to carry it on. p.tail.tail.nose = p; //fixing nose of the element after the deleted p.tail = p.tail.tail; //fixing tail of the element before deleted } public Object Select(POSITION_DLL p) { return p.tail.head; } public POSITION_DLL First() { return first; } public POSITION_DLL Next(POSITION_DLL p) { return p.tail; } public POSITION_DLL Previous(POSITION_DLL p) { return p.nose; } public POSITION_DLL End() { return first.nose; } public POSITION_DLL Last() //Previous(End()) { return first.nose.nose; } public boolean isEmpty() { return first.isEqual(first.nose); // return (this.First()).isEqual(this.End()); } public LIST_DLL clone() { POSITION_DLL p; LIST_DLL toReturn = new LIST_DLL(); // POSITION_DLL q = toReturn.First(); for (p = First(); !p.isEqual(End()); p = Next(p)) { toReturn.Insert(toReturn.End(), Select(p)); // q = toReturn.Next(q); } return toReturn; } public boolean hasPosition(POSITION_DLL p) { if (p.isEqual(End())) return true; for (POSITION_DLL q = First(); !q.isEqual(End()); q = Next(q)) if (p.isEqual(q)) return true; return false; } }