/* * LIST.java * * Created on March 24, 2008, 1:04 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package list_purge; /** * * @author suchenek */ //Copyright Dr. Marek A. Suchenek, 2005, 2006, 2007, 2008 - 2015 //All rights reserved // SLL implementation // See class POSITION for interpretation of position. // Object of class LIST_DLL is implemented as POSITION_DLL: (first). // Object of class LIST is implemented as a pair of objects of // type POSITION: (first, end). // First position never changes during the lifetime of the list in question. // End position needs to be updated after insertion on the end position and // after deletion from the last position. // If the list is empty then first == end. public class LIST { private POSITION first; // points to header private POSITION end; // points to the last element public LIST() //constructs the empty LIST { cnt.incr(); POSITION temp = new POSITION(); temp.head = null; //never used temp.tail = null; first = temp; end = temp; } public void Insert(POSITION p, E x) { cnt.incr(); POSITION temp = new POSITION(); temp.head = x; temp.tail = p.tail; p.tail = temp; if (p.isEqual(end)) end = temp; // if insert to end position, new temp becomes end position // if (p == end) end = temp; //a faster alternative } public boolean Delete(POSITION p) // Returns true if delete was allowed and false otherwise { cnt.incr(); if((first == end) || (p == end)) return false; if(p.tail.isEqual(this.End())) end = p; // if delete from last position, p becomes end position // if(this.Next(p).isEqual(this.End())) end = p; // if delete from last position, p becomes end position // if (p.tail == end) end = p; // a faster alternative; if delete from last position, p becomes end position p.tail = p.tail.tail; return true; } public E Select(POSITION p) { cnt.incr(); return p.tail.head; } public void Replace(POSITION p, E x) { cnt.incr(); p.tail.head = x; } public POSITION First() { cnt.incr(); cnt3.incr(); // counting cost of reverse printing return first; } public POSITION Next(POSITION p) { cnt.incr(); cnt3.incr(); // counting cost of reverse printing if(p == end) return null; return p.tail; } public POSITION Previous(POSITION p) { cnt.incr(); cnt3.incr(); // counting cost of reverse printing if(p == first) return null; POSITION q; for (q = First(); !Next(q).isEqual(p); q = Next(q)) cnt.incr(); return q; } public POSITION Last() //Previous(End()) { cnt.incr(); POSITION q; for (q = First(); !Next(q).isEqual(End()); q = Next(q)) cnt.incr(); return q; } public POSITION End() { cnt.incr(); return end; } public Boolean isEmpty() { return first == end; } public boolean hasPosition(POSITION p) { cnt.incr(); if (p.isEqual(End())) return true; for (POSITION q = First(); !q.isEqual(End()); q = Next(q)) { cnt.incr(); if (p.isEqual(q)) return true; } return false; } public LIST cloneAbstract() // Deep clone { LIST toReturn = new LIST(); for (POSITION p = First(); !p.isEqual(End()); p = Next(p)) { toReturn.Insert(toReturn.End(), Select(p)); } return toReturn; } public LIST clone() // Deep clone { LIST toReturn = new LIST(); for (POSITION p = first; p != end; p = p.tail) { cnt.incr(); POSITION temp = new POSITION (); temp.head = p.tail.head; temp.tail = null; toReturn.end.tail = temp; toReturn.end = temp; } return toReturn; } // public LIST clone() // { // LIST toReturn = new LIST(); // for (POSITION p = first; p != end; p = p.tail) // { // toReturn.Insert(toReturn.End(), this.Select(p)); // } // return toReturn; // } }