/* * Main.java * * Created on February 6, 2008, 1:48 PM * * * MORE UP TO DATE VERSION IS IN LIST_DLL_purge with classname LIST_purge * * 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 public class LIST_DLL_purge { public static void main (String[] args) { LIST_DLL L = new LIST_DLL(); Object x; x = new int[] {1}; L.Insert(L.First(), x); x = new int[] {2}; L.Insert(L.First(), x); x = new int[] {3}; L.Insert(L.First(), x); x = new int[] {4}; L.Insert(L.First(), x); x = new int[] {3}; L.Insert(L.First(), x); L.Insert(L.End(), x); L.Insert(L.First(), x); L.Insert(L.End(), x); for (POSITION_DLL p = L.First(); !p.isEqual(L.End()); p = L.Next(p)) System.out.println(((int[])L.Select(p))[0]); System.out.println("Purge it!"); LIST_DLL_purge.Purge(L); for (POSITION_DLL p = L.First(); !p.isEqual(L.End()); p = L.Next(p)) System.out.println(((int[])L.Select(p))[0]); System.out.println("Same, in the reverse order"); for (POSITION_DLL p = L.End(); !p.isEqual(L.First()); p = L.Previous(p)) System.out.println(((int[])L.Select(L.Previous(p)))[0]); } public static void Purge(LIST_DLL L) { for (POSITION_DLL p = L.First(); !p.isEqual(L.End()); p = L.Next(p)) { Object x = L.Select(p); POSITION_DLL q = L.Next(p); while (!q.isEqual(L.End())) { Object y = L.Select(q); if (LIST_DLL_purge.isEqual(x, y)) L.Delete(q); else q = L.Next(q); } } } public static boolean isEqual(Object x, Object y) { return (((int[])x)[0] == ((int[])y)[0]); } }