/* * LIST_purge.java * * Created on March 24, 2008, 1:03 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 public class LIST_purge { public static void main (String[] args) { LIST L = new LIST(); //Exercise: use Integer instead of Object and simplify this class Integer x; // The code below creates a list // of N consecutive numbers // with duplicity W repeated M times long length = 0; int K = 10; //number of values int M = 10; //degree of duplicity - macro int W = 10; //degree of duplicity - micro for (int k=0;k C = L.clone(); cnt.out("Clonning toook ", " steps"); System.out.println(" N + 1 = " + (N+1)); LIST D = L; System.out.println("Purge it!"); cnt.clr(); LIST_purge.Purge(L); for (POSITION p = L.First(); !p.isEqual(L.End()); p = L.Next(p)) System.out.print((L.Select(p)) + " "); System.out.println(); cnt3.clr(); System.out.println("Same, in the reverse order"); for (POSITION p = L.Last(); !p.isEqual(L.First()); p = L.Previous(p)) System.out.print((L.Select(p)) + " "); System.out.print((L.Select(L.First())) + " "); System.out.println(); cnt3.out(">>> Reverse printing of K = " + K + " elements toook ", " steps; K*(K + 3) = " + (K*(K + 3))); System.out.println(); System.out.print("N = " + N); cnt.out(" cnt = "); long cntval = cnt.get(); // cost of purging and forward and backward reaversal System.out.print(" cnt/N = " + (1.0*cntval/(N))); System.out.print(" cnt/N^2 = " + (1.0*cntval/(N * N))); System.out.print(" 4*N + 18 = "+ ((N)*4+18)); System.out.println(" 4.5*N^2 + 11.5*N + 6 = "+ (6 + ((N)*23 + (N)*(N)*9)/2)); System.out.println("Copied list:"); for (POSITION p = D.First(); !p.isEqual(D.End()); p = D.Next(p)) System.out.print((D.Select(p)) + " "); System.out.println(); System.out.println("Cloned list:"); for (POSITION p = C.First(); !p.isEqual(C.End()); p = C.Next(p)) System.out.print((C.Select(p)) + " "); System.out.println(); cnt3.clr(); System.out.println("Same, in the reverse order"); for (POSITION p = C.Last(); !p.isEqual(C.First()); p = C.Previous(p)) System.out.print((C.Select(p)) + " "); System.out.print((C.Select(C.First())) + " "); System.out.println(); cnt3.out(">>> Reverse printing of N = " + N + " elements toook ", " steps; N*(N + 3) = " + (N*(N + 3))); System.out.println(); Purge(C); System.out.println("Purged cloned list:"); for (POSITION p = C.First(); !p.isEqual(C.End()); p = C.Next(p)) System.out.print((C.Select(p)) + " "); System.out.println(); } public static void Purge(LIST L) { for (POSITION p = L.First(); !p.isEqual(L.End()); p = L.Next(p)) { Object x = L.Select(p); POSITION q = L.Next(p); while (!q.isEqual(L.End())) { Object y = L.Select(q); if (LIST_purge.isEqual(x, y)) L.Delete(q); else q = L.Next(q); } } } public static boolean isEqual(Object x, Object y) { return (((Integer)x).equals((Integer)y)); } }