/* * LIST.java * * Created on December 10, 2008, 4:38 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package graph; /** * * @author suchenek */ //Copyright Dr. Marek A. Suchenek, 2005, 2006, 2007, 2008 public class LIST { private Object[] element; private int end; // the number of elements of the LIST private int resize_factor; //in % of increase; default is 100 (or 2.0 times increase) public LIST() { this.element = new Object[10]; end = 0; resize_factor = 100; } public LIST(int r) { this.element = new Object[10]; end = 0; resize_factor = r; } public void Insert(POSITION p, Object x) { // System.out.println("From LIST: Inserting " + x); int len = element.length; int ind = p.index; if (end == len) { Object[] temp = new Object[(100 + resize_factor)*len/100]; System.out.println("New array of size " + ((100 + resize_factor)*len/100) + " created."); for (int i = 0; i < ind; i++) { temp[i] = element[i]; cnt.incr(); } temp[ind] = x; cnt.incr(); for (int i = ind; i < end; i++) { temp[i + 1] = element[i]; cnt.incr(); } element = temp; } else { for (int i = end; i > ind; i--) { element[i] = element[i - 1]; cnt.incr(); } element[ind] = x; cnt.incr(); } end++; } public void Delete(POSITION p) { cnt.incr(); int len = element.length; int ind = p.index; if ((len >= 10 + (resize_factor/10)) && ((100 + resize_factor)*end <= 50*len)) { // there is a discretion when to downsize Object[] temp = new Object[100*len/(100 + resize_factor)]; System.out.println("New array of size " + (100*len/(100 + resize_factor)) + " created."); for (int i = 0; i < ind; i++) { temp[i] = element[i]; cnt.incr(); } for (int i = ind +1; i < end; i++) { temp[i - 1] = element[i]; cnt.incr(); } element = temp; } else { for (int i = ind + 1; i < end; i++) { element[i - 1] = element[i]; cnt.incr(); } } end--; } public Object Select(POSITION p) { cnt.incr(); return element[p.index]; } public POSITION First() { cnt.incr(); return new POSITION(0); } public POSITION Next(POSITION p) { cnt.incr(); return new POSITION(p.index + 1); } public POSITION Previous(POSITION p) { cnt.incr(); return new POSITION(p.index - 1); } public POSITION Last() { cnt.incr(); return new POSITION(end - 1); } public POSITION End() { cnt.incr(); return new POSITION(end); } }