//***************************************************************************** // // Beenish Chaudry, Geetika Tewari, Elif Tosun and // Ileana Streinu // // Summer 1999, Fall 1999, Spring 2000 // //***************************************************************************** // // Linked LList Class: LList // // linked list of objects of type PointObject // //***************************************************************************** import java.awt.*; import java.io.*; import java.applet.*; import java.util.*; //**************************************************************************** // // Why do we need to create a node for the list: // Since we cannot impose InsertFront on a NULL LIST (ie in the beginning // when the // list has not been created), and in order to save us the trouble of having // to continually have 'if' statements whenever we want to insert, the list has // been designed in this way. // In order to avoid having a dangling pointer when the LList is declared as // the variable. (The pointer LList has to point to something!) // //**************************************************************************** public class LList{ PointObj data; LList next; //******************** // // LList constructors // //******************** LList() { data = null; next = null; }// end LList constructor 1 LList(PointObj obj) { data = obj; next = null; }// end LList constructor 2 /*********************/ // // InsertFront // /*********************/ LList InsertFront(PointObj obj) // insert a new list cell with the given object // in front of the (non-empty) list, // return new list { LList front = new LList(obj); front.next = this; return front; }// end InsertFront //************************************************** // // LDelete (Loose Delete) // // used ofr deleting a point by clicking on it // //************************************************** LList LDelete(PointObj obj) // delete an object from a non-empty list, // if it exists there // equality is tested "loosely": // deleted object is loosely equal to the one // in the list // I.e. coordinates are within certain bounds // and other properties do not matter { // special treatment for deletion of first element if (data.LEquals(obj)) { return next; } // otherwise, keep track of the previous cell before the removed one LList ptr = next; LList prev = this; while (ptr !=Constants.nullCell) { if (ptr.data.LEquals(obj)) {prev.next = ptr.next; return this; } prev = ptr; ptr = ptr.next; }// end while return this; }// end LDelete //*********************************************** // // LSearch (Loose Search) // // Used for moving the point by clicking on it // //*********************************************** LList LSearch(PointObj obj) // search for an object in a non-empty list, // return null (if not there) or part // of the list starting with it // // equality is tested "loosely": // searched object is loosely equal to the one // in the list // I.e. coordinates are within certain bounds // and other properties do not matter { // special treatment for first element if (data.LEquals(obj)) { return this; }; LList ptr = this; while (ptr != Constants.nullCell) { if (ptr.data.LEquals(obj)) { return ptr; } ptr = ptr.next; }// end while return null; }// end LSearch //****************************** // // Length of the current list // //****************************** int Length() // length of the current (non-empty) list { int l = 1; LList ptr = next; while (ptr != null) { l++; ptr = ptr.next; } return l; }// end Length }// end class LList